]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Controller/PocketController.php
cs & tests for wllbg v1 import
[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{
13 /**
7019c7cf 14 * @Route("/pocket", name="import_pocket")
1f4408de 15 */
d51b38ed 16 public function indexAction()
1f4408de 17 {
c10fcb3b
TC
18 $pocket = $this->get('wallabag_import.pocket.import');
19 $form = $this->createFormBuilder($pocket)
20 ->add('read', CheckboxType::class, array(
21 'label' => 'Mark all as read',
22 'required' => false,
23 ))
24 ->getForm();
c10fcb3b 25
7019c7cf
JB
26 return $this->render('WallabagImportBundle:Pocket:index.html.twig', [
27 'import' => $this->get('wallabag_import.pocket.import'),
1e3ed714 28 'has_consumer_key' => '' == trim($this->get('craue_config')->get('pocket_consumer_key')) ? 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 {
252ebd60 38 $requestToken = $this->get('wallabag_import.pocket.import')
27e475a9 39 ->getRequestToken($this->generateUrl('import', array(), UrlGeneratorInterface::ABSOLUTE_URL));
252ebd60
JB
40
41 $this->get('session')->set('import.pocket.code', $requestToken);
c10fcb3b
TC
42 $markAsRead = $request->request->get('form')['read'];
43 $this->get('session')->set('read', $markAsRead);
1f4408de 44
252ebd60 45 return $this->redirect(
27e475a9 46 'https://getpocket.com/auth/authorize?request_token='.$requestToken.'&redirect_uri='.$this->generateUrl('import_pocket_callback', array(), UrlGeneratorInterface::ABSOLUTE_URL),
252ebd60
JB
47 301
48 );
1f4408de
NL
49 }
50
51 /**
7019c7cf 52 * @Route("/pocket/callback", name="import_pocket_callback")
1f4408de
NL
53 */
54 public function callbackAction()
55 {
252ebd60 56 $message = 'Import failed, please try again.';
0aa344dc 57 $pocket = $this->get('wallabag_import.pocket.import');
c10fcb3b 58 $markAsRead = $this->get('session')->get('read');
252ebd60
JB
59
60 // something bad happend on pocket side
61 if (false === $pocket->authorize($this->get('session')->get('import.pocket.code'))) {
62 $this->get('session')->getFlashBag()->add(
63 'notice',
64 $message
65 );
66
67 return $this->redirect($this->generateUrl('import_pocket'));
68 }
69
c10fcb3b 70 if (true === $pocket->setMarkAsRead($markAsRead)->import()) {
252ebd60 71 $summary = $pocket->getSummary();
b1d05721 72 $message = 'Import summary: '.$summary['imported'].' imported, '.$summary['skipped'].' already saved.';
252ebd60
JB
73 }
74
c10fcb3b
TC
75 $this->get('session')->remove('read');
76
252ebd60
JB
77 $this->get('session')->getFlashBag()->add(
78 'notice',
79 $message
80 );
1f4408de
NL
81
82 return $this->redirect($this->generateUrl('homepage'));
83 }
1f4408de 84}