]>
Commit | Line | Data |
---|---|---|
1f4408de NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\ImportBundle\Controller; | |
4 | ||
5 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
1d76102a | 6 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
1f4408de | 7 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
c10fcb3b TC |
8 | use Symfony\Component\HttpFoundation\Request; |
9 | use Symfony\Component\Form\Extension\Core\Type\CheckboxType; | |
10 | ||
1f4408de NL |
11 | class PocketController extends Controller |
12 | { | |
ef75e122 JB |
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('rabbitmq')) { | |
c98db1b6 | 24 | $pocket->setRabbitmqProducer($this->get('old_sound_rabbit_mq.import_pocket_producer')); |
ef75e122 JB |
25 | } |
26 | ||
27 | return $pocket; | |
28 | } | |
29 | ||
1f4408de | 30 | /** |
7019c7cf | 31 | * @Route("/pocket", name="import_pocket") |
1f4408de | 32 | */ |
d51b38ed | 33 | public function indexAction() |
1f4408de | 34 | { |
ef75e122 | 35 | $pocket = $this->getPocketImportService(); |
c10fcb3b | 36 | $form = $this->createFormBuilder($pocket) |
4094ea47 | 37 | ->add('mark_as_read', CheckboxType::class, [ |
0d42217e | 38 | 'label' => 'import.form.mark_as_read_label', |
79d0e38e | 39 | 'required' => false, |
4094ea47 | 40 | ]) |
c10fcb3b | 41 | ->getForm(); |
c10fcb3b | 42 | |
7019c7cf | 43 | return $this->render('WallabagImportBundle:Pocket:index.html.twig', [ |
ef75e122 | 44 | 'import' => $this->getPocketImportService(), |
1e3ed714 | 45 | 'has_consumer_key' => '' == trim($this->get('craue_config')->get('pocket_consumer_key')) ? false : true, |
c10fcb3b | 46 | 'form' => $form->createView(), |
7019c7cf | 47 | ]); |
1f4408de NL |
48 | } |
49 | ||
1f4408de | 50 | /** |
7019c7cf | 51 | * @Route("/pocket/auth", name="import_pocket_auth") |
1f4408de | 52 | */ |
c10fcb3b | 53 | public function authAction(Request $request) |
1f4408de | 54 | { |
ef75e122 | 55 | $requestToken = $this->getPocketImportService() |
4094ea47 | 56 | ->getRequestToken($this->generateUrl('import', [], UrlGeneratorInterface::ABSOLUTE_URL)); |
252ebd60 | 57 | |
4d0ec0e7 JB |
58 | if (false === $requestToken) { |
59 | $this->get('session')->getFlashBag()->add( | |
60 | 'notice', | |
61 | 'flashes.import.notice.failed' | |
62 | ); | |
63 | ||
64 | return $this->redirect($this->generateUrl('import_pocket')); | |
65 | } | |
66 | ||
252ebd60 | 67 | $this->get('session')->set('import.pocket.code', $requestToken); |
0d42217e | 68 | $this->get('session')->set('mark_as_read', $request->request->get('form')['mark_as_read']); |
1f4408de | 69 | |
252ebd60 | 70 | return $this->redirect( |
4094ea47 | 71 | 'https://getpocket.com/auth/authorize?request_token='.$requestToken.'&redirect_uri='.$this->generateUrl('import_pocket_callback', [], UrlGeneratorInterface::ABSOLUTE_URL), |
252ebd60 JB |
72 | 301 |
73 | ); | |
1f4408de NL |
74 | } |
75 | ||
76 | /** | |
7019c7cf | 77 | * @Route("/pocket/callback", name="import_pocket_callback") |
1f4408de NL |
78 | */ |
79 | public function callbackAction() | |
80 | { | |
4204a06b | 81 | $message = 'flashes.import.notice.failed'; |
ef75e122 | 82 | $pocket = $this->getPocketImportService(); |
4204a06b | 83 | |
0d42217e JB |
84 | $markAsRead = $this->get('session')->get('mark_as_read'); |
85 | $this->get('session')->remove('mark_as_read'); | |
252ebd60 JB |
86 | |
87 | // something bad happend on pocket side | |
88 | if (false === $pocket->authorize($this->get('session')->get('import.pocket.code'))) { | |
89 | $this->get('session')->getFlashBag()->add( | |
90 | 'notice', | |
91 | $message | |
92 | ); | |
93 | ||
94 | return $this->redirect($this->generateUrl('import_pocket')); | |
95 | } | |
96 | ||
c10fcb3b | 97 | if (true === $pocket->setMarkAsRead($markAsRead)->import()) { |
252ebd60 | 98 | $summary = $pocket->getSummary(); |
4094ea47 | 99 | $message = $this->get('translator')->trans('flashes.import.notice.summary', [ |
4204a06b JB |
100 | '%imported%' => $summary['imported'], |
101 | '%skipped%' => $summary['skipped'], | |
4094ea47 | 102 | ]); |
252ebd60 JB |
103 | } |
104 | ||
105 | $this->get('session')->getFlashBag()->add( | |
106 | 'notice', | |
107 | $message | |
108 | ); | |
1f4408de NL |
109 | |
110 | return $this->redirect($this->generateUrl('homepage')); | |
111 | } | |
1f4408de | 112 | } |