]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Controller/PocketController.php
56be5cbfd8e56215f5b5a5bc22e063922ee0e221
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Controller / PocketController.php
1 <?php
2
3 namespace Wallabag\ImportBundle\Controller;
4
5 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
7 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
10
11 class PocketController extends Controller
12 {
13 /**
14 * Return Pocket Import Service with or without RabbitMQ enabled.
15 *
16 * @return \Wallabag\ImportBundle\Import\PocketImport
17 */
18 private function getPocketImportService()
19 {
20 $pocket = $this->get('wallabag_import.pocket.import');
21 $pocket->setUser($this->getUser());
22
23 if ($this->get('craue_config')->get('import_with_rabbitmq')) {
24 $pocket->setProducer($this->get('old_sound_rabbit_mq.import_pocket_producer'));
25 } elseif ($this->get('craue_config')->get('import_with_redis')) {
26 $pocket->setProducer($this->get('wallabag_import.producer.redis.pocket'));
27 }
28
29 return $pocket;
30 }
31
32 /**
33 * @Route("/pocket", name="import_pocket")
34 */
35 public function indexAction()
36 {
37 $pocket = $this->getPocketImportService();
38 $form = $this->createFormBuilder($pocket)
39 ->add('mark_as_read', CheckboxType::class, [
40 'label' => 'import.form.mark_as_read_label',
41 'required' => false,
42 ])
43 ->getForm();
44
45 return $this->render('WallabagImportBundle:Pocket:index.html.twig', [
46 'import' => $this->getPocketImportService(),
47 'has_consumer_key' => '' === trim($this->getUser()->getConfig()->getPocketConsumerKey()) ? false : true,
48 'form' => $form->createView(),
49 ]);
50 }
51
52 /**
53 * @Route("/pocket/auth", name="import_pocket_auth")
54 */
55 public function authAction(Request $request)
56 {
57 $requestToken = $this->getPocketImportService()
58 ->getRequestToken($this->generateUrl('import', [], UrlGeneratorInterface::ABSOLUTE_URL));
59
60 if (false === $requestToken) {
61 $this->get('session')->getFlashBag()->add(
62 'notice',
63 'flashes.import.notice.failed'
64 );
65
66 return $this->redirect($this->generateUrl('import_pocket'));
67 }
68
69 $this->get('session')->set('import.pocket.code', $requestToken);
70 $this->get('session')->set('mark_as_read', $request->request->get('form')['mark_as_read']);
71
72 return $this->redirect(
73 'https://getpocket.com/auth/authorize?request_token='.$requestToken.'&redirect_uri='.$this->generateUrl('import_pocket_callback', [], UrlGeneratorInterface::ABSOLUTE_URL),
74 301
75 );
76 }
77
78 /**
79 * @Route("/pocket/callback", name="import_pocket_callback")
80 */
81 public function callbackAction()
82 {
83 $message = 'flashes.import.notice.failed';
84 $pocket = $this->getPocketImportService();
85
86 $markAsRead = $this->get('session')->get('mark_as_read');
87 $this->get('session')->remove('mark_as_read');
88
89 // something bad happend on pocket side
90 if (false === $pocket->authorize($this->get('session')->get('import.pocket.code'))) {
91 $this->get('session')->getFlashBag()->add(
92 'notice',
93 $message
94 );
95
96 return $this->redirect($this->generateUrl('import_pocket'));
97 }
98
99 if (true === $pocket->setMarkAsRead($markAsRead)->import()) {
100 $summary = $pocket->getSummary();
101 $message = $this->get('translator')->trans('flashes.import.notice.summary', [
102 '%imported%' => $summary['imported'],
103 '%skipped%' => $summary['skipped'],
104 ]);
105
106 if (0 < $summary['queued']) {
107 $message = $this->get('translator')->trans('flashes.import.notice.summary_with_queue', [
108 '%queued%' => $summary['queued'],
109 ]);
110 }
111 }
112
113 $this->get('session')->getFlashBag()->add(
114 'notice',
115 $message
116 );
117
118 return $this->redirect($this->generateUrl('homepage'));
119 }
120 }