X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FImportBundle%2FImport%2FPocketImport.php;h=e5c86f07b5d47d9b86258bdba4185bac003dbdfc;hb=7ec2897ee0ad190dcb9f77032d785f2f9661b754;hp=dd1c34abcba9179486c5e55e811289df00a326a8;hpb=303768dfe9b85f87d043eb225c5c8c3a88d8c051;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index dd1c34ab..e5c86f07 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -5,6 +5,7 @@ namespace Wallabag\ImportBundle\Import; use Doctrine\ORM\EntityManager; use GuzzleHttp\Client; use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Wallabag\CoreBundle\Entity\Entry; use Wallabag\CoreBundle\Entity\Tag; use Wallabag\CoreBundle\Tools\Utils; @@ -18,7 +19,7 @@ class PocketImport implements ImportInterface private $skippedEntries = 0; private $importedEntries = 0; - public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey) + public function __construct(TokenStorageInterface $tokenStorage, Session $session, EntityManager $em, $consumerKey) { $this->user = $tokenStorage->getToken()->getUser(); $this->session = $session; @@ -26,31 +27,100 @@ class PocketImport implements ImportInterface $this->consumerKey = $consumerKey; } + /** + * {@inheritdoc} + */ public function getName() { return 'Pocket'; } + /** + * {@inheritdoc} + */ public function getDescription() { return 'This importer will import all your Pocket data.'; } /** - * Create a new Client. + * {@inheritdoc} + */ + public function oAuthRequest($redirectUri, $callbackUri) + { + $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', + [ + 'body' => json_encode([ + 'consumer_key' => $this->consumerKey, + 'redirect_uri' => $redirectUri, + ]), + ] + ); + + $response = $this->client->send($request); + $values = $response->json(); + + // store code in session for callback method + $this->session->set('pocketCode', $values['code']); + + return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri; + } + + /** + * {@inheritdoc} + */ + public function oAuthAuthorize() + { + $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', + [ + 'body' => json_encode([ + 'consumer_key' => $this->consumerKey, + 'code' => $this->session->get('pocketCode'), + ]), + ] + ); + + $response = $this->client->send($request); + + return $response->json()['access_token']; + } + + /** + * {@inheritdoc} + */ + public function import($accessToken) + { + $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get', + [ + 'body' => json_encode([ + 'consumer_key' => $this->consumerKey, + 'access_token' => $accessToken, + 'detailType' => 'complete', + 'state' => 'all', + 'sort' => 'oldest', + ]), + ] + ); + + $response = $this->client->send($request); + $entries = $response->json(); + + $this->parsePocketEntries($entries['list']); + + $this->session->getFlashBag()->add( + 'notice', + $this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.' + ); + } + + /** + * Set the Guzzle client. * - * @return Client + * @param Client $client */ - private function createClient() + public function setClient(Client $client) { - return new Client([ - 'defaults' => [ - 'headers' => [ - 'content-type' => 'application/json', - 'X-Accept' => 'application/json', - ], - ], - ]); + $this->client = $client; } /** @@ -117,9 +187,9 @@ class PocketImport implements ImportInterface $existingEntry = $this->em ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUrlAndUserId($url, $this->user->getId()); + ->existByUrlAndUserId($url, $this->user->getId()); - if (count($existingEntry) > 0) { + if (false !== $existingEntry) { ++$this->skippedEntries; continue; } @@ -158,70 +228,4 @@ class PocketImport implements ImportInterface $this->em->flush(); } - - public function oAuthRequest($redirectUri, $callbackUri) - { - $client = $this->createClient(); - $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', - [ - 'body' => json_encode([ - 'consumer_key' => $this->consumerKey, - 'redirect_uri' => $redirectUri, - ]), - ] - ); - - $response = $client->send($request); - $values = $response->json(); - - // store code in session for callback method - $this->session->set('pocketCode', $values['code']); - - return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri; - } - - public function oAuthAuthorize() - { - $client = $this->createClient(); - - $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', - [ - 'body' => json_encode([ - 'consumer_key' => $this->consumerKey, - 'code' => $this->session->get('pocketCode'), - ]), - ] - ); - - $response = $client->send($request); - - return $response->json()['access_token']; - } - - public function import($accessToken) - { - $client = $this->createClient(); - - $request = $client->createRequest('POST', 'https://getpocket.com/v3/get', - [ - 'body' => json_encode([ - 'consumer_key' => $this->consumerKey, - 'access_token' => $accessToken, - 'detailType' => 'complete', - 'state' => 'all', - 'sort' => 'oldest', - ]), - ] - ); - - $response = $client->send($request); - $entries = $response->json(); - - $this->parsePocketEntries($entries['list']); - - $this->session->getFlashBag()->add( - 'notice', - $this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.' - ); - } }