|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file was created by developers working at BitBag |
|
5
|
|
|
* Do you need more information about us and what we do? Visit our https://bitbag.io website! |
|
6
|
|
|
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace BitBag\SyliusCmsPlugin\Controller\Action\Admin; |
|
12
|
|
|
|
|
13
|
|
|
use BitBag\SyliusCmsPlugin\Controller\Helper\FormErrorsFlashHelperInterface; |
|
14
|
|
|
use BitBag\SyliusCmsPlugin\Exception\ImportFailedException; |
|
15
|
|
|
use BitBag\SyliusCmsPlugin\Form\Type\ImportType; |
|
16
|
|
|
use BitBag\SyliusCmsPlugin\Processor\ImportProcessorInterface; |
|
17
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
|
23
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
24
|
|
|
use Twig\Environment; |
|
25
|
|
|
|
|
26
|
|
|
final class ImportDataAction |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var ImportProcessorInterface */ |
|
29
|
|
|
private $importProcessor; |
|
30
|
|
|
|
|
31
|
|
|
/** @var FormFactoryInterface */ |
|
32
|
|
|
private $formFactory; |
|
33
|
|
|
|
|
34
|
|
|
/** @var FlashBagInterface */ |
|
35
|
|
|
private $flashBag; |
|
36
|
|
|
|
|
37
|
|
|
/** @var FormErrorsFlashHelperInterface */ |
|
38
|
|
|
private $formErrorsFlashHelper; |
|
39
|
|
|
|
|
40
|
|
|
/** @var TranslatorInterface */ |
|
41
|
|
|
private $translator; |
|
42
|
|
|
|
|
43
|
|
|
/** @var Environment */ |
|
44
|
|
|
private $twig; |
|
45
|
|
|
|
|
46
|
|
|
public function __construct( |
|
47
|
|
|
ImportProcessorInterface $importProcessor, |
|
48
|
|
|
FormFactoryInterface $formFactory, |
|
49
|
|
|
FlashBagInterface $flashBag, |
|
50
|
|
|
FormErrorsFlashHelperInterface $formErrorsFlashHelper, |
|
51
|
|
|
TranslatorInterface $translator, |
|
52
|
|
|
Environment $twig |
|
53
|
|
|
) { |
|
54
|
|
|
$this->importProcessor = $importProcessor; |
|
55
|
|
|
$this->formFactory = $formFactory; |
|
56
|
|
|
$this->flashBag = $flashBag; |
|
57
|
|
|
$this->formErrorsFlashHelper = $formErrorsFlashHelper; |
|
58
|
|
|
$this->translator = $translator; |
|
59
|
|
|
$this->twig = $twig; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function __invoke(Request $request): Response |
|
63
|
|
|
{ |
|
64
|
|
|
$form = $this->formFactory->create(ImportType::class); |
|
65
|
|
|
$referer = (string) $request->headers->get('referer'); |
|
66
|
|
|
|
|
67
|
|
|
$form->handleRequest($request); |
|
68
|
|
|
|
|
69
|
|
|
if ($request->isMethod('POST') && $form->isSubmitted()) { |
|
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
|
|
|
$this->flashBag->set('success', $this->translator->trans('bitbag_sylius_cms_plugin.ui.successfully_imported')); |
|
79
|
|
|
} catch (ImportFailedException $exception) { |
|
80
|
|
|
$this->flashBag->set('error', $exception->getMessage()); |
|
81
|
|
|
} |
|
82
|
|
|
} else { |
|
83
|
|
|
$this->formErrorsFlashHelper->addFlashErrors($form); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return new RedirectResponse($referer); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return new Response($this->twig->render('@BitBagSyliusCmsPlugin/Grid/Form/_importForm.html.twig', [ |
|
90
|
|
|
'form' => $form->createView(), |
|
91
|
|
|
])); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|