aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle/Controller
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2016-03-28 16:43:33 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-03-28 16:43:33 +0200
commitb787a7757ea73b9d10c14cb21758feb07dfc5885 (patch)
tree13989f7a843332cdee095d456863535a2854862c /src/Wallabag/ImportBundle/Controller
parent0e49487bb0a004d526eb41e7d3fb44b566441e34 (diff)
downloadwallabag-b787a7757ea73b9d10c14cb21758feb07dfc5885.tar.gz
wallabag-b787a7757ea73b9d10c14cb21758feb07dfc5885.tar.zst
wallabag-b787a7757ea73b9d10c14cb21758feb07dfc5885.zip
Refacto wallabag import
Use an abstract class to store all common action from wallabag vX import. Move specificity in v1 & v2 import.
Diffstat (limited to 'src/Wallabag/ImportBundle/Controller')
-rw-r--r--src/Wallabag/ImportBundle/Controller/WallabagController.php85
-rw-r--r--src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php68
-rw-r--r--src/Wallabag/ImportBundle/Controller/WallabagV2Controller.php68
3 files changed, 121 insertions, 100 deletions
diff --git a/src/Wallabag/ImportBundle/Controller/WallabagController.php b/src/Wallabag/ImportBundle/Controller/WallabagController.php
new file mode 100644
index 00000000..01883d4a
--- /dev/null
+++ b/src/Wallabag/ImportBundle/Controller/WallabagController.php
@@ -0,0 +1,85 @@
1<?php
2
3namespace Wallabag\ImportBundle\Controller;
4
5use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6use Symfony\Component\HttpFoundation\Request;
7use Wallabag\ImportBundle\Form\Type\UploadImportType;
8
9/**
10 * Define Wallabag import for v1 and v2, since there are very similar.
11 */
12abstract class WallabagController extends Controller
13{
14 /**
15 * Return the service to handle the import.
16 *
17 * @return \Wallabag\ImportBundle\Import\ImportInterface
18 */
19 abstract protected function getImportService();
20
21 /**
22 * Return the template used for the form.
23 *
24 * @return string
25 */
26 abstract protected function getImportTemplate();
27
28 /**
29 * Handle import request.
30 *
31 * @param Request $request
32 *
33 * @return Response|RedirectResponse
34 */
35 public function indexAction(Request $request)
36 {
37 $form = $this->createForm(UploadImportType::class);
38 $form->handleRequest($request);
39
40 $wallabag = $this->getImportService();
41
42 if ($form->isValid()) {
43 $file = $form->get('file')->getData();
44 $markAsRead = $form->get('mark_as_read')->getData();
45 $name = $this->getUser()->getId().'.json';
46
47 if (in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) {
48 $res = $wallabag
49 ->setUser($this->getUser())
50 ->setFilepath($this->getParameter('wallabag_import.resource_dir').'/'.$name)
51 ->setMarkAsRead($markAsRead)
52 ->import();
53
54 $message = 'flashes.import.notice.failed';
55
56 if (true === $res) {
57 $summary = $wallabag->getSummary();
58 $message = $this->get('translator')->trans('flashes.import.notice.summary', array(
59 '%imported%' => $summary['imported'],
60 '%skipped%' => $summary['skipped'],
61 ));
62
63 unlink($this->getParameter('wallabag_import.resource_dir').'/'.$name);
64 }
65
66 $this->get('session')->getFlashBag()->add(
67 'notice',
68 $message
69 );
70
71 return $this->redirect($this->generateUrl('homepage'));
72 } else {
73 $this->get('session')->getFlashBag()->add(
74 'notice',
75 'flashes.import.notice.failed_on_file'
76 );
77 }
78 }
79
80 return $this->render($this->getImportTemplate(), [
81 'form' => $form->createView(),
82 'import' => $wallabag,
83 ]);
84 }
85}
diff --git a/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php b/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php
index 1bc9696d..3e748d57 100644
--- a/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php
+++ b/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php
@@ -2,64 +2,32 @@
2 2
3namespace Wallabag\ImportBundle\Controller; 3namespace Wallabag\ImportBundle\Controller;
4 4
5use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 5use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
7use Symfony\Component\HttpFoundation\Request; 6use Symfony\Component\HttpFoundation\Request;
8use Wallabag\ImportBundle\Form\Type\UploadImportType;
9 7
10class WallabagV1Controller extends Controller 8class WallabagV1Controller extends WallabagController
11{ 9{
12 /** 10 /**
13 * @Route("/wallabag-v1", name="import_wallabag_v1") 11 * {@inheritdoc}
14 */ 12 */
15 public function indexAction(Request $request) 13 protected function getImportService()
16 { 14 {
17 $form = $this->createForm(UploadImportType::class); 15 return $this->get('wallabag_import.wallabag_v1.import');
18 $form->handleRequest($request); 16 }
19
20 $wallabag = $this->get('wallabag_import.wallabag_v1.import');
21
22 if ($form->isValid()) {
23 $file = $form->get('file')->getData();
24 $markAsRead = $form->get('mark_as_read')->getData();
25 $name = $this->getUser()->getId().'.json';
26
27 if (in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) {
28 $res = $wallabag
29 ->setUser($this->getUser())
30 ->setFilepath($this->getParameter('wallabag_import.resource_dir').'/'.$name)
31 ->setMarkAsRead($markAsRead)
32 ->import();
33
34 $message = 'flashes.import.notice.failed';
35
36 if (true === $res) {
37 $summary = $wallabag->getSummary();
38 $message = $this->get('translator')->trans('flashes.import.notice.summary', array(
39 '%imported%' => $summary['imported'],
40 '%skipped%' => $summary['skipped'],
41 ));
42
43 unlink($this->getParameter('wallabag_import.resource_dir').'/'.$name);
44 }
45
46 $this->get('session')->getFlashBag()->add(
47 'notice',
48 $message
49 );
50 17
51 return $this->redirect($this->generateUrl('homepage')); 18 /**
52 } else { 19 * {@inheritdoc}
53 $this->get('session')->getFlashBag()->add( 20 */
54 'notice', 21 protected function getImportTemplate()
55 'flashes.import.notice.failed_on_file' 22 {
56 ); 23 return 'WallabagImportBundle:WallabagV1:index.html.twig';
57 } 24 }
58 }
59 25
60 return $this->render('WallabagImportBundle:WallabagV1:index.html.twig', [ 26 /**
61 'form' => $form->createView(), 27 * @Route("/wallabag-v1", name="import_wallabag_v1")
62 'import' => $wallabag, 28 */
63 ]); 29 public function indexAction(Request $request)
30 {
31 return parent::indexAction($request);
64 } 32 }
65} 33}
diff --git a/src/Wallabag/ImportBundle/Controller/WallabagV2Controller.php b/src/Wallabag/ImportBundle/Controller/WallabagV2Controller.php
index 3e6428a0..c2a42165 100644
--- a/src/Wallabag/ImportBundle/Controller/WallabagV2Controller.php
+++ b/src/Wallabag/ImportBundle/Controller/WallabagV2Controller.php
@@ -2,64 +2,32 @@
2 2
3namespace Wallabag\ImportBundle\Controller; 3namespace Wallabag\ImportBundle\Controller;
4 4
5use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 5use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
7use Symfony\Component\HttpFoundation\Request; 6use Symfony\Component\HttpFoundation\Request;
8use Wallabag\ImportBundle\Form\Type\UploadImportType;
9 7
10class WallabagV2Controller extends Controller 8class WallabagV2Controller extends WallabagController
11{ 9{
12 /** 10 /**
13 * @Route("/wallabag-v2", name="import_wallabag_v2") 11 * {@inheritdoc}
14 */ 12 */
15 public function indexAction(Request $request) 13 protected function getImportService()
16 { 14 {
17 $form = $this->createForm(UploadImportType::class); 15 return $this->get('wallabag_import.wallabag_v2.import');
18 $form->handleRequest($request); 16 }
19
20 $wallabag = $this->get('wallabag_import.wallabag_v2.import');
21
22 if ($form->isValid()) {
23 $file = $form->get('file')->getData();
24 $markAsRead = $form->get('mark_as_read')->getData();
25 $name = $this->getUser()->getId().'.json';
26
27 if (in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) {
28 $res = $wallabag
29 ->setUser($this->getUser())
30 ->setFilepath($this->getParameter('wallabag_import.resource_dir').'/'.$name)
31 ->setMarkAsRead($markAsRead)
32 ->import();
33
34 $message = 'flashes.import.notice.failed';
35
36 if (true === $res) {
37 $summary = $wallabag->getSummary();
38 $message = $this->get('translator')->trans('flashes.import.notice.summary', array(
39 '%imported%' => $summary['imported'],
40 '%skipped%' => $summary['skipped'],
41 ));
42
43 unlink($this->getParameter('wallabag_import.resource_dir').'/'.$name);
44 }
45
46 $this->get('session')->getFlashBag()->add(
47 'notice',
48 $message
49 );
50 17
51 return $this->redirect($this->generateUrl('homepage')); 18 /**
52 } else { 19 * {@inheritdoc}
53 $this->get('session')->getFlashBag()->add( 20 */
54 'notice', 21 protected function getImportTemplate()
55 'flashes.import.notice.failed_on_file' 22 {
56 ); 23 return 'WallabagImportBundle:WallabagV2:index.html.twig';
57 } 24 }
58 }
59 25
60 return $this->render('WallabagImportBundle:WallabagV2:index.html.twig', [ 26 /**
61 'form' => $form->createView(), 27 * @Route("/wallabag-v2", name="import_wallabag_v2")
62 'import' => $wallabag, 28 */
63 ]); 29 public function indexAction(Request $request)
30 {
31 return parent::indexAction($request);
64 } 32 }
65} 33}