Completed
Pull Request — master (#169)
by Mikołaj
05:25
created

ImportDataAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 5
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusCmsPlugin\Controller\Action\Admin;
14
15
use BitBag\SyliusCmsPlugin\Exception\ImportFailedException;
16
use BitBag\SyliusCmsPlugin\Form\Type\ImportType;
17
use BitBag\SyliusCmsPlugin\Processor\ImportProcessorInterface;
18
use FOS\RestBundle\View\View;
19
use FOS\RestBundle\View\ViewHandler;
20
use Symfony\Component\Form\FormFactoryInterface;
21
use Symfony\Component\Form\FormInterface;
22
use Symfony\Component\HttpFoundation\File\UploadedFile;
23
use Symfony\Component\HttpFoundation\RedirectResponse;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpFoundation\Response;
26
use Symfony\Component\HttpFoundation\Session\Session;
27
use Symfony\Component\Translation\TranslatorInterface;
28
29
final class ImportDataAction
30
{
31
    /** @var ImportProcessorInterface */
32
    private $importProcessor;
33
34
    /** @var FormFactoryInterface */
35
    private $formFactory;
36
37
    /** @var Session */
38
    private $session;
39
40
    /** @var TranslatorInterface */
41
    private $translator;
42
43
    /** @var ViewHandler */
44
    private $viewHandler;
45
46
    public function __construct(
47
        ImportProcessorInterface $importProcessor,
48
        FormFactoryInterface $formFactory,
49
        Session $session,
50
        TranslatorInterface $translator,
51
        ViewHandler $viewHandler
52
    ) {
53
        $this->importProcessor = $importProcessor;
54
        $this->formFactory = $formFactory;
55
        $this->session = $session;
56
        $this->translator = $translator;
57
        $this->viewHandler = $viewHandler;
58
    }
59
60
    public function __invoke(Request $request): Response
61
    {
62
        $form = $this->formFactory->create(ImportType::class);
63
        $referer = (string) $request->headers->get('referer');
64
65
        $form->handleRequest($request);
66
67
        if ($request->isMethod('POST') && $form->isSubmitted()) {
68
            $flashBag = $this->session->getFlashBag();
69
70
            if ($form->isValid()) {
71
                /** @var UploadedFile $file */
72
                $file = $form->get('file')->getData();
73
                $resourceName = $request->get('resourceName');
74
75
                try {
76
                    $this->importProcessor->process($resourceName, $file->getPathname());
77
78
                    $flashBag->set('success', $this->translator->trans('bitbag_sylius_cms_plugin.ui.successfully_imported'));
79
                } catch (ImportFailedException $exception) {
80
                    $flashBag->set('error', $exception->getMessage());
81
                }
82
            } else {
83
                $flashBag->set('error', rtrim(implode($this->getFormErrors($form), ', '), ', '));
84
            }
85
86
            return new RedirectResponse($referer);
87
        }
88
89
        $view = View::create()
90
            ->setData([
91
                'form' => $form->createView(),
92
            ])
93
            ->setTemplate('@BitBagSyliusCmsPlugin/Grid/Form/_importForm.html.twig')
94
        ;
95
96
        return $this->viewHandler->handle($view);
97
    }
98
99
    private function getFormErrors(FormInterface $form): array
100
    {
101
        $errors = [];
102
103
        foreach ($form->getErrors(true) as $error) {
104
            $errors[] = $error->getMessage();
105
        }
106
107
        return $errors;
108
    }
109
}
110