From ff7b031d5792f7b6fd43b508d89397775bd1433c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 23 Oct 2015 14:01:27 +0200 Subject: [PATCH] refactor pocket import --- src/Wallabag/CoreBundle/Tools/Utils.php | 1 + .../Controller/PocketController.php | 112 +-------------- .../DependencyInjection/Configuration.php | 17 +++ .../WallabagImportExtension.php | 25 ++++ .../ImportBundle/Import/ImportInterface.php | 10 ++ .../ImportBundle/Import/PocketImport.php | 134 ++++++++++++++++++ .../Resources/config/services.yml | 8 ++ 7 files changed, 202 insertions(+), 105 deletions(-) create mode 100644 src/Wallabag/ImportBundle/DependencyInjection/Configuration.php create mode 100644 src/Wallabag/ImportBundle/DependencyInjection/WallabagImportExtension.php create mode 100644 src/Wallabag/ImportBundle/Import/ImportInterface.php create mode 100644 src/Wallabag/ImportBundle/Import/PocketImport.php create mode 100644 src/Wallabag/ImportBundle/Resources/config/services.yml diff --git a/src/Wallabag/CoreBundle/Tools/Utils.php b/src/Wallabag/CoreBundle/Tools/Utils.php index b146d98b9..b501ce659 100644 --- a/src/Wallabag/CoreBundle/Tools/Utils.php +++ b/src/Wallabag/CoreBundle/Tools/Utils.php @@ -28,6 +28,7 @@ class Utils /** * @param $words + * * @return float */ public static function convertWordsToMinutes($words) diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php index ffd0c9abb..76d8417be 100644 --- a/src/Wallabag/ImportBundle/Controller/PocketController.php +++ b/src/Wallabag/ImportBundle/Controller/PocketController.php @@ -4,10 +4,7 @@ namespace Wallabag\ImportBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; -use Symfony\Component\HttpFoundation\Request; -use GuzzleHttp\Client; -use Wallabag\CoreBundle\Entity\Entry; -use Wallabag\CoreBundle\Tools\Utils; +use Wallabag\ImportBundle\Import\PocketImport; class PocketController extends Controller { @@ -19,49 +16,15 @@ class PocketController extends Controller return $this->render('WallabagImportBundle:Pocket:index.html.twig', array()); } - /** - * Create a new Client. - * - * @return Client - */ - private function createClient() - { - return new Client([ - 'defaults' => [ - 'headers' => [ - 'content-type' => 'application/json', - 'X-Accept' => 'application/json', - ], - ], - ]); - } - /** * @Route("/auth-pocket", name="authpocket") */ public function authAction() { - $client = $this->createClient(); - $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', - [ - 'body' => json_encode([ - 'consumer_key' => $this->container->getParameter('pocket_consumer_key'), - 'redirect_uri' => $this->generateUrl('import', array(), true), - ]), - ] - ); + $pocket = new PocketImport($this->get('security.token_storage'), $this->get('session'), $this->getDoctrine()->getManager(), $this->container->getParameter('pocket_consumer_key')); + $authUrl = $pocket->oAuthRequest($this->generateUrl('import', array(), true), $this->generateUrl('callbackpocket', array(), true)); - $response = $client->send($request); - $values = $response->json(); - $code = $values['code']; - - // store code in session for callback method - $session = $this->get('session'); - $session->set('pocketCode', $code); - - $url = 'https://getpocket.com/auth/authorize?request_token='.$code.'&redirect_uri='.$this->generateUrl('callbackpocket', array(), true); - - return $this->redirect($url, 301); + return $this->redirect($authUrl, 301); } /** @@ -69,71 +32,10 @@ class PocketController extends Controller */ public function callbackAction() { - $client = $this->createClient(); - - $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', - [ - 'body' => json_encode([ - 'consumer_key' => $this->container->getParameter('pocket_consumer_key'), - 'code' => $this->get('session')->get('pocketCode'), - ]), - ] - ); - - $response = $client->send($request); - $values = $response->json(); - $accessToken = $values['access_token']; - - $request = $client->createRequest('POST', 'https://getpocket.com/v3/get', - [ - 'body' => json_encode([ - 'consumer_key' => $this->container->getParameter('pocket_consumer_key'), - 'access_token' => $accessToken, - 'detailType' => 'complete', - ]), - ] - ); - - $response = $client->send($request); - $entries = $response->json(); - - $this->parsePocketEntries($entries['list']); - - $this->get('session')->getFlashBag()->add( - 'notice', - count($entries['list']).' entries imported' - ); + $pocket = new PocketImport($this->get('security.token_storage'), $this->get('session'), $this->getDoctrine()->getManager(), $this->container->getParameter('pocket_consumer_key')); + $accessToken = $pocket->oAuthAuthorize(); + $pocket->import($accessToken); return $this->redirect($this->generateUrl('homepage')); } - - /** - * @param $entries - */ - private function parsePocketEntries($entries) - { - $em = $this->getDoctrine()->getManager(); - - foreach ($entries as $entry) { - $newEntry = new Entry($this->getUser()); - $newEntry->setUrl($entry['given_url']); - $newEntry->setTitle(isset($entry['resolved_title']) ? $entry['resolved_title'] : (isset($entry['given_title']) ? $entry['given_title'] : 'Untitled')); - - if (isset($entry['excerpt'])) { - $newEntry->setContent($entry['excerpt']); - } - - if (isset($entry['has_image']) && $entry['has_image'] > 0) { - $newEntry->setPreviewPicture($entry['image']['src']); - } - - if (isset($entry['word_count'])) { - $newEntry->setReadingTime(Utils::convertWordsToMinutes($entry['word_count'])); - } - - $em->persist($newEntry); - } - - $em->flush(); - } } diff --git a/src/Wallabag/ImportBundle/DependencyInjection/Configuration.php b/src/Wallabag/ImportBundle/DependencyInjection/Configuration.php new file mode 100644 index 000000000..bacaff31c --- /dev/null +++ b/src/Wallabag/ImportBundle/DependencyInjection/Configuration.php @@ -0,0 +1,17 @@ +root('wallabag_import'); + + return $treeBuilder; + } +} diff --git a/src/Wallabag/ImportBundle/DependencyInjection/WallabagImportExtension.php b/src/Wallabag/ImportBundle/DependencyInjection/WallabagImportExtension.php new file mode 100644 index 000000000..4efcaace9 --- /dev/null +++ b/src/Wallabag/ImportBundle/DependencyInjection/WallabagImportExtension.php @@ -0,0 +1,25 @@ +processConfiguration($configuration, $configs); + + $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('services.yml'); + } + + public function getAlias() + { + return 'wallabag_import'; + } +} diff --git a/src/Wallabag/ImportBundle/Import/ImportInterface.php b/src/Wallabag/ImportBundle/Import/ImportInterface.php new file mode 100644 index 000000000..88de3fa8b --- /dev/null +++ b/src/Wallabag/ImportBundle/Import/ImportInterface.php @@ -0,0 +1,10 @@ +user = $tokenStorage->getToken()->getUser(); + $this->session = $session; + $this->em = $em; + $this->consumerKey = $consumerKey; + } + + /** + * Create a new Client. + * + * @return Client + */ + private function createClient() + { + return new Client([ + 'defaults' => [ + 'headers' => [ + 'content-type' => 'application/json', + 'X-Accept' => 'application/json', + ], + ], + ]); + } + + /** + * @param $entries + */ + private function parsePocketEntries($entries) + { + foreach ($entries as $entry) { + $newEntry = new Entry($this->user); + $newEntry->setUrl($entry['given_url']); + $newEntry->setTitle(isset($entry['resolved_title']) ? $entry['resolved_title'] : (isset($entry['given_title']) ? $entry['given_title'] : 'Untitled')); + + if (isset($entry['excerpt'])) { + $newEntry->setContent($entry['excerpt']); + } + + if (isset($entry['has_image']) && $entry['has_image'] > 0) { + $newEntry->setPreviewPicture($entry['image']['src']); + } + + if (isset($entry['word_count'])) { + $newEntry->setReadingTime(Utils::convertWordsToMinutes($entry['word_count'])); + } + + $this->em->persist($newEntry); + } + + $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', + ]), + ] + ); + + $response = $client->send($request); + $entries = $response->json(); + + $this->parsePocketEntries($entries['list']); + + $this->session->getFlashBag()->add( + 'notice', + count($entries['list']).' entries imported' + ); + } +} diff --git a/src/Wallabag/ImportBundle/Resources/config/services.yml b/src/Wallabag/ImportBundle/Resources/config/services.yml new file mode 100644 index 000000000..82628f082 --- /dev/null +++ b/src/Wallabag/ImportBundle/Resources/config/services.yml @@ -0,0 +1,8 @@ +services: + wallabag_import.import.pocket_import: + class: Wallabag\ImportBundle\Import\PocketImport + arguments: + - @security.token_storage + - @session + - @doctrine.orm.entity_manager + - %pocket_consumer_key%