aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle/Controller
diff options
context:
space:
mode:
authorNicolas LÅ“uillet <nicolas@loeuillet.org>2016-03-28 18:52:22 +0200
committerNicolas LÅ“uillet <nicolas@loeuillet.org>2016-03-28 18:52:22 +0200
commite2dda631529b1299b544d35bc3df1b59963a9d9b (patch)
tree2a013dcc670deddcbe7d1515b697d72d8ab0734c /src/Wallabag/ImportBundle/Controller
parent0b05dae37722246b447766c6bb09bc8f747e8e58 (diff)
parentb787a7757ea73b9d10c14cb21758feb07dfc5885 (diff)
downloadwallabag-e2dda631529b1299b544d35bc3df1b59963a9d9b.tar.gz
wallabag-e2dda631529b1299b544d35bc3df1b59963a9d9b.tar.zst
wallabag-e2dda631529b1299b544d35bc3df1b59963a9d9b.zip
Merge pull request #1820 from wallabag/import-refacto
Refacto wallabag 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}