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