]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Controller/WallabagController.php
Add a real configuration for CS-Fixer
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Controller / WallabagController.php
1 <?php
2
3 namespace Wallabag\ImportBundle\Controller;
4
5 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6 use Symfony\Component\HttpFoundation\Request;
7 use Wallabag\ImportBundle\Form\Type\UploadImportType;
8
9 /**
10 * Define Wallabag import for v1 and v2, since there are very similar.
11 */
12 abstract class WallabagController extends Controller
13 {
14 /**
15 * Handle import request.
16 *
17 * @param Request $request
18 *
19 * @return Response|RedirectResponse
20 */
21 public function indexAction(Request $request)
22 {
23 $form = $this->createForm(UploadImportType::class);
24 $form->handleRequest($request);
25
26 $wallabag = $this->getImportService();
27 $wallabag->setUser($this->getUser());
28
29 if ($form->isSubmitted() && $form->isValid()) {
30 $file = $form->get('file')->getData();
31 $markAsRead = $form->get('mark_as_read')->getData();
32 $name = $this->getUser()->getId() . '.json';
33
34 if (null !== $file && in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes'), true) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) {
35 $res = $wallabag
36 ->setFilepath($this->getParameter('wallabag_import.resource_dir') . '/' . $name)
37 ->setMarkAsRead($markAsRead)
38 ->import();
39
40 $message = 'flashes.import.notice.failed';
41
42 if (true === $res) {
43 $summary = $wallabag->getSummary();
44 $message = $this->get('translator')->trans('flashes.import.notice.summary', [
45 '%imported%' => $summary['imported'],
46 '%skipped%' => $summary['skipped'],
47 ]);
48
49 if (0 < $summary['queued']) {
50 $message = $this->get('translator')->trans('flashes.import.notice.summary_with_queue', [
51 '%queued%' => $summary['queued'],
52 ]);
53 }
54
55 unlink($this->getParameter('wallabag_import.resource_dir') . '/' . $name);
56 }
57
58 $this->get('session')->getFlashBag()->add(
59 'notice',
60 $message
61 );
62
63 return $this->redirect($this->generateUrl('homepage'));
64 }
65
66 $this->get('session')->getFlashBag()->add(
67 'notice',
68 'flashes.import.notice.failed_on_file'
69 );
70 }
71
72 return $this->render($this->getImportTemplate(), [
73 'form' => $form->createView(),
74 'import' => $wallabag,
75 ]);
76 }
77
78 /**
79 * Return the service to handle the import.
80 *
81 * @return \Wallabag\ImportBundle\Import\ImportInterface
82 */
83 abstract protected function getImportService();
84
85 /**
86 * Return the template used for the form.
87 *
88 * @return string
89 */
90 abstract protected function getImportTemplate();
91 }