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