vendor/bitbag/wishlist-plugin/src/Controller/Action/ImportWishlistFromCsvAction.php line 28

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file was created by developers working at BitBag
  4.  * Do you need more information about us and what we do? Visit our https://bitbag.io website!
  5.  * 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
  6. */
  7. declare(strict_types=1);
  8. namespace BitBag\SyliusWishlistPlugin\Controller\Action;
  9. use BitBag\SyliusWishlistPlugin\Command\Wishlist\ImportWishlistFromCsv;
  10. use BitBag\SyliusWishlistPlugin\Entity\Wishlist;
  11. use BitBag\SyliusWishlistPlugin\Form\Type\ImportWishlistFromCsvType;
  12. use BitBag\SyliusWishlistPlugin\Resolver\WishlistsResolverInterface;
  13. use Symfony\Component\Form\FormFactoryInterface;
  14. use Symfony\Component\Form\FormInterface;
  15. use Symfony\Component\HttpFoundation\File\UploadedFile;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpFoundation\Session\Session;
  20. use Symfony\Component\Messenger\HandleTrait;
  21. use Symfony\Component\Messenger\MessageBusInterface;
  22. use Twig\Environment;
  23. final class ImportWishlistFromCsvAction
  24. {
  25.     use HandleTrait;
  26.     private FormFactoryInterface $formFactory;
  27.     private RequestStack $requestStack;
  28.     private Environment $twigEnvironment;
  29.     private WishlistsResolverInterface $wishlistsResolver;
  30.     public function __construct(
  31.         FormFactoryInterface $formFactory,
  32.         RequestStack $requestStack,
  33.         MessageBusInterface $messageBus,
  34.         Environment $twigEnvironment,
  35.         WishlistsResolverInterface $wishlistsResolver
  36.     ) {
  37.         $this->formFactory $formFactory;
  38.         $this->requestStack $requestStack;
  39.         $this->messageBus $messageBus;
  40.         $this->twigEnvironment $twigEnvironment;
  41.         $this->wishlistsResolver $wishlistsResolver;
  42.     }
  43.     public function __invoke(Request $request): Response
  44.     {
  45.         $form $this->createForm();
  46.         $form->handleRequest($request);
  47.         if ($form->isSubmitted() && $form->isValid()) {
  48.             return $this->handleCommand($form$request);
  49.         }
  50.         /** @var Session $session */
  51.         $session $this->requestStack->getSession();
  52.         foreach ($form->getErrors() as $error) {
  53.             $session->getFlashBag()->add('error'$error->getMessage());
  54.         }
  55.         return new Response(
  56.             $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/importWishlist.html.twig', [
  57.                 'form' => $form->createView(),
  58.             ])
  59.         );
  60.     }
  61.     private function createForm(): FormInterface
  62.     {
  63.         return $this->formFactory->create(ImportWishlistFromCsvType::class, [], [
  64.             'wishlists' => $this->wishlistsResolver->resolveAndCreate(),
  65.         ]);
  66.     }
  67.     private function handleCommand(FormInterface $formRequest $request): Response
  68.     {
  69.         /** @var UploadedFile $file */
  70.         $file $form->get('wishlist_file')->getData();
  71.         /** @var Wishlist $wishlist */
  72.         $wishlist $form->get('wishlists')->getData();
  73.         $command = new ImportWishlistFromCsv($file->getFileInfo(), $request$wishlist->getId());
  74.         return $this->handle($command);
  75.     }
  76. }