vendor/bitbag/wishlist-plugin/src/Controller/Action/ListWishlistProductsAction.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\Entity\WishlistInterface;
  10. use BitBag\SyliusWishlistPlugin\Form\Type\WishlistCollectionType;
  11. use BitBag\SyliusWishlistPlugin\Processor\WishlistCommandProcessorInterface;
  12. use BitBag\SyliusWishlistPlugin\Resolver\WishlistsResolverInterface;
  13. use Sylius\Component\Order\Context\CartContextInterface;
  14. use Sylius\Component\Order\Context\CartNotFoundException;
  15. use Symfony\Component\Form\FormFactoryInterface;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpFoundation\Session\Session;
  20. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  21. use Symfony\Contracts\Translation\TranslatorInterface;
  22. use Twig\Environment;
  23. final class ListWishlistProductsAction
  24. {
  25.     private CartContextInterface $cartContext;
  26.     private FormFactoryInterface $formFactory;
  27.     private Environment $twigEnvironment;
  28.     private WishlistCommandProcessorInterface $wishlistCommandProcessor;
  29.     private WishlistsResolverInterface $wishlistsResolver;
  30.     private TranslatorInterface $translator;
  31.     private UrlGeneratorInterface $generator;
  32.     public function __construct(
  33.         CartContextInterface $cartContext,
  34.         FormFactoryInterface $formFactory,
  35.         Environment $twigEnvironment,
  36.         WishlistCommandProcessorInterface $wishlistCommandProcessor,
  37.         WishlistsResolverInterface $wishlistsResolver,
  38.         TranslatorInterface $translator,
  39.         UrlGeneratorInterface $generator
  40.     ) {
  41.         $this->cartContext $cartContext;
  42.         $this->formFactory $formFactory;
  43.         $this->twigEnvironment $twigEnvironment;
  44.         $this->wishlistCommandProcessor $wishlistCommandProcessor;
  45.         $this->wishlistsResolver $wishlistsResolver;
  46.         $this->translator $translator;
  47.         $this->generator $generator;
  48.     }
  49.     public function __invoke(Request $request): Response
  50.     {
  51.         $wishlists $this->wishlistsResolver->resolveAndCreate();
  52.         /** @var WishlistInterface $wishlist */
  53.         $wishlist array_shift($wishlists);
  54.         if (null === $wishlist) {
  55.             $homepageUrl $this->generator->generate('sylius_shop_homepage');
  56.             /** @var Session $session */
  57.             $session $request->getSession();
  58.             $session->getFlashBag()->add('error'$this->translator->trans('bitbag_sylius_wishlist_plugin.ui.go_to_wishlist_failure'));
  59.             return new RedirectResponse($homepageUrl);
  60.         }
  61.         try {
  62.             $cart $this->cartContext->getCart();
  63.         } catch (CartNotFoundException $exception) {
  64.             $cart null;
  65.         }
  66.         $commandsArray $this->wishlistCommandProcessor->createWishlistItemsCollection($wishlist->getWishlistProducts());
  67.         $form $this->formFactory->create(WishlistCollectionType::class, ['items' => $commandsArray], [
  68.             'cart' => $cart,
  69.         ]);
  70.         return new Response(
  71.             $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistDetails/index.html.twig', [
  72.                 'wishlist' => $wishlist,
  73.                 'form' => $form->createView(),
  74.             ])
  75.         );
  76.     }
  77. }