]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Controller/PocketController.php
Display how many messages are queue
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Controller / PocketController.php
CommitLineData
1f4408de
NL
1<?php
2
3namespace Wallabag\ImportBundle\Controller;
4
5use Symfony\Bundle\FrameworkBundle\Controller\Controller;
1d76102a 6use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1f4408de 7use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
c10fcb3b
TC
8use Symfony\Component\HttpFoundation\Request;
9use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
10
1f4408de
NL
11class 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
b3437d58
JB
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'));
ef75e122
JB
27 }
28
29 return $pocket;
30 }
31
1f4408de 32 /**
7019c7cf 33 * @Route("/pocket", name="import_pocket")
1f4408de 34 */
d51b38ed 35 public function indexAction()
1f4408de 36 {
ef75e122 37 $pocket = $this->getPocketImportService();
c10fcb3b 38 $form = $this->createFormBuilder($pocket)
4094ea47 39 ->add('mark_as_read', CheckboxType::class, [
0d42217e 40 'label' => 'import.form.mark_as_read_label',
79d0e38e 41 'required' => false,
4094ea47 42 ])
c10fcb3b 43 ->getForm();
c10fcb3b 44
7019c7cf 45 return $this->render('WallabagImportBundle:Pocket:index.html.twig', [
ef75e122 46 'import' => $this->getPocketImportService(),
1e3ed714 47 'has_consumer_key' => '' == trim($this->get('craue_config')->get('pocket_consumer_key')) ? false : true,
c10fcb3b 48 'form' => $form->createView(),
7019c7cf 49 ]);
1f4408de
NL
50 }
51
1f4408de 52 /**
7019c7cf 53 * @Route("/pocket/auth", name="import_pocket_auth")
1f4408de 54 */
c10fcb3b 55 public function authAction(Request $request)
1f4408de 56 {
ef75e122 57 $requestToken = $this->getPocketImportService()
4094ea47 58 ->getRequestToken($this->generateUrl('import', [], UrlGeneratorInterface::ABSOLUTE_URL));
252ebd60 59
4d0ec0e7
JB
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
252ebd60 69 $this->get('session')->set('import.pocket.code', $requestToken);
0d42217e 70 $this->get('session')->set('mark_as_read', $request->request->get('form')['mark_as_read']);
1f4408de 71
252ebd60 72 return $this->redirect(
4094ea47 73 'https://getpocket.com/auth/authorize?request_token='.$requestToken.'&redirect_uri='.$this->generateUrl('import_pocket_callback', [], UrlGeneratorInterface::ABSOLUTE_URL),
252ebd60
JB
74 301
75 );
1f4408de
NL
76 }
77
78 /**
7019c7cf 79 * @Route("/pocket/callback", name="import_pocket_callback")
1f4408de
NL
80 */
81 public function callbackAction()
82 {
4204a06b 83 $message = 'flashes.import.notice.failed';
ef75e122 84 $pocket = $this->getPocketImportService();
4204a06b 85
0d42217e
JB
86 $markAsRead = $this->get('session')->get('mark_as_read');
87 $this->get('session')->remove('mark_as_read');
252ebd60
JB
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
c10fcb3b 99 if (true === $pocket->setMarkAsRead($markAsRead)->import()) {
252ebd60 100 $summary = $pocket->getSummary();
4094ea47 101 $message = $this->get('translator')->trans('flashes.import.notice.summary', [
4204a06b
JB
102 '%imported%' => $summary['imported'],
103 '%skipped%' => $summary['skipped'],
4094ea47 104 ]);
252ebd60
JB
105 }
106
107 $this->get('session')->getFlashBag()->add(
108 'notice',
109 $message
110 );
1f4408de
NL
111
112 return $this->redirect($this->generateUrl('homepage'));
113 }
1f4408de 114}