Completed
Push — master ( 699d35...4f061e )
by Mikołaj
01:56
created

ImportDataAction::getFormErrors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
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\Controller\Helper\FormErrorsFlashHelperInterface;
16
use BitBag\SyliusCmsPlugin\Exception\ImportFailedException;
17
use BitBag\SyliusCmsPlugin\Form\Type\ImportType;
18
use BitBag\SyliusCmsPlugin\Processor\ImportProcessorInterface;
19
use FOS\RestBundle\View\View;
20
use FOS\RestBundle\View\ViewHandler;
21
use Symfony\Component\Form\FormFactoryInterface;
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\Flash\FlashBagInterface;
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 FlashBagInterface */
38
    private $flashBag;
39
40
    /** @var FormErrorsFlashHelperInterface */
41
    private $formErrorsFlashHelper;
42
43
    /** @var TranslatorInterface */
44
    private $translator;
45
46
    /** @var ViewHandler */
47
    private $viewHandler;
48
49
    public function __construct(
50
        ImportProcessorInterface $importProcessor,
51
        FormFactoryInterface $formFactory,
52
        FlashBagInterface $flashBag,
53
        FormErrorsFlashHelperInterface $formErrorsFlashHelper,
54
        TranslatorInterface $translator,
55
        ViewHandler $viewHandler
56
    ) {
57
58
        $this->importProcessor = $importProcessor;
59
        $this->formFactory = $formFactory;
60
        $this->flashBag = $flashBag;
61
        $this->formErrorsFlashHelper = $formErrorsFlashHelper;
62
        $this->translator = $translator;
63
        $this->viewHandler = $viewHandler;
64
    }
65
66
    public function __invoke(Request $request): Response
67
    {
68
        $form = $this->formFactory->create(ImportType::class);
69
        $referer = (string) $request->headers->get('referer');
70
71
        $form->handleRequest($request);
72
73
        if ($request->isMethod('POST') && $form->isSubmitted()) {
74
75
            if ($form->isValid()) {
76
                /** @var UploadedFile $file */
77
                $file = $form->get('file')->getData();
78
                $resourceName = $request->get('resourceName');
79
80
                try {
81
                    $this->importProcessor->process($resourceName, $file->getPathname());
82
83
                    $this->flashBag->set('success', $this->translator->trans('bitbag_sylius_cms_plugin.ui.successfully_imported'));
84
                } catch (ImportFailedException $exception) {
85
                    $this->flashBag->set('error', $exception->getMessage());
86
                }
87
            } else {
88
                $this->formErrorsFlashHelper->addFlashErrors($form);
89
            }
90
91
            return new RedirectResponse($referer);
92
        }
93
94
        $view = View::create()
95
            ->setData([
96
                'form' => $form->createView(),
97
            ])
98
            ->setTemplate('@BitBagSyliusCmsPlugin/Grid/Form/_importForm.html.twig')
99
        ;
100
101
        return $this->viewHandler->handle($view);
102
    }
103
}
104