|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sylius package. |
| 5 | + * |
| 6 | + * (c) Sylius Sp. z o.o. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Sylius\ImportExport\Controller; |
| 15 | + |
| 16 | +use Sylius\ImportExport\Messenger\Command\CreateImportProcess; |
| 17 | +use Sylius\Resource\Metadata\RegistryInterface; |
| 18 | +use Symfony\Component\Form\FormFactoryInterface; |
| 19 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
| 20 | +use Symfony\Component\HttpFoundation\Request; |
| 21 | +use Symfony\Component\HttpFoundation\Response; |
| 22 | +use Symfony\Component\HttpFoundation\Session\Session; |
| 23 | +use Symfony\Component\Messenger\MessageBusInterface; |
| 24 | + |
| 25 | +final class ImportAction |
| 26 | +{ |
| 27 | + public function __construct( |
| 28 | + private RegistryInterface $metadataRegistry, |
| 29 | + private FormFactoryInterface $formFactory, |
| 30 | + private MessageBusInterface $commandBus, |
| 31 | + private string $importForm, |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
| 35 | + public function __invoke(Request $request, string $grid): Response |
| 36 | + { |
| 37 | + $request->attributes->set('_sylius', array_merge($request->attributes->get('_sylius', []), ['grid' => $grid])); |
| 38 | + |
| 39 | + $form = $this->formFactory->create($this->importForm); |
| 40 | + $form->handleRequest($request); |
| 41 | + |
| 42 | + if (!$form->isSubmitted() || !$form->isValid()) { |
| 43 | + /** @var Session $session */ |
| 44 | + $session = $request->getSession(); |
| 45 | + $session->getFlashBag()->add('error', 'sylius_import_export.import_form_invalid'); |
| 46 | + |
| 47 | + return new RedirectResponse($request->headers->get('referer') ?? '/'); |
| 48 | + } |
| 49 | + |
| 50 | + $data = $form->getData(); |
| 51 | + $format = $data['format']; |
| 52 | + $filePath = $data['filePath']; |
| 53 | + $resourceClass = $data['resourceClass']; |
| 54 | + |
| 55 | + // Get metadata to find the resource alias |
| 56 | + $metadata = $this->metadataRegistry->getByClass($resourceClass); |
| 57 | + |
| 58 | + try { |
| 59 | + $this->commandBus->dispatch(new CreateImportProcess( |
| 60 | + resource: $resourceClass, // Pass the actual class, not the alias |
| 61 | + format: $format, |
| 62 | + filePath: $filePath, |
| 63 | + parameters: [], // Empty for now |
| 64 | + )); |
| 65 | + |
| 66 | + /** @var Session $session */ |
| 67 | + $session = $request->getSession(); |
| 68 | + $session->getFlashBag()->add('success', 'sylius_import_export.import_started'); |
| 69 | + } catch (\Throwable $e) { |
| 70 | + /** @var Session $session */ |
| 71 | + $session = $request->getSession(); |
| 72 | + $session->getFlashBag()->add('error', 'sylius_import_export.import_failed: ' . $e->getMessage()); |
| 73 | + } |
| 74 | + |
| 75 | + return new RedirectResponse($request->headers->get('referer') ?? '/'); |
| 76 | + } |
| 77 | +} |
0 commit comments