vendor/bitbag/wishlist-plugin/src/Controller/Action/AddProductToWishlistAction.php line 79

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\Entity\WishlistProductInterface;
  11. use BitBag\SyliusWishlistPlugin\Exception\WishlistNotFoundException;
  12. use BitBag\SyliusWishlistPlugin\Factory\WishlistProductFactoryInterface;
  13. use BitBag\SyliusWishlistPlugin\Resolver\WishlistCookieTokenResolverInterface;
  14. use BitBag\SyliusWishlistPlugin\Resolver\WishlistsResolverInterface;
  15. use Doctrine\Persistence\ObjectManager;
  16. use Sylius\Component\Channel\Context\ChannelContextInterface;
  17. use Sylius\Component\Channel\Context\ChannelNotFoundException;
  18. use Sylius\Component\Core\Model\ProductInterface;
  19. use Sylius\Component\Core\Repository\ProductRepositoryInterface;
  20. use Symfony\Component\HttpFoundation\RedirectResponse;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\RequestStack;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\HttpFoundation\Session\Session;
  25. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  26. use Symfony\Contracts\Translation\TranslatorInterface;
  27. final class AddProductToWishlistAction
  28. {
  29.     private ProductRepositoryInterface $productRepository;
  30.     private WishlistProductFactoryInterface $wishlistProductFactory;
  31.     private RequestStack $requestStack;
  32.     private TranslatorInterface $translator;
  33.     private WishlistsResolverInterface $wishlistsResolver;
  34.     private ObjectManager $wishlistManager;
  35.     private ChannelContextInterface $channelContext;
  36.     private WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver;
  37.     public function __construct(
  38.         ProductRepositoryInterface $productRepository,
  39.         WishlistProductFactoryInterface $wishlistProductFactory,
  40.         RequestStack $requestStack,
  41.         TranslatorInterface $translator,
  42.         WishlistsResolverInterface $wishlistsResolver,
  43.         ObjectManager $wishlistManager,
  44.         ChannelContextInterface $channelContext,
  45.         WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver
  46.     ) {
  47.         $this->productRepository $productRepository;
  48.         $this->wishlistProductFactory $wishlistProductFactory;
  49.         $this->requestStack $requestStack;
  50.         $this->translator $translator;
  51.         $this->wishlistsResolver $wishlistsResolver;
  52.         $this->wishlistManager $wishlistManager;
  53.         $this->channelContext $channelContext;
  54.         $this->wishlistCookieTokenResolver $wishlistCookieTokenResolver;
  55.     }
  56.     public function __invoke(Request $request): Response
  57.     {
  58.         /** @var ProductInterface|null $product */
  59.         $product $this->productRepository->find($request->get('productId'));
  60.         if (null === $product) {
  61.             throw new NotFoundHttpException();
  62.         }
  63.         $wishlists $this->wishlistsResolver->resolveAndCreate();
  64.         /** @var WishlistInterface $wishlist */
  65.         $wishlist array_shift($wishlists);
  66.         if (null === $wishlist) {
  67.             throw new WishlistNotFoundException(
  68.                 $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.wishlist_not_found')
  69.             );
  70.         }
  71.         try {
  72.             $channel $this->channelContext->getChannel();
  73.         } catch (ChannelNotFoundException $exception) {
  74.             $channel null;
  75.         }
  76.         if (null !== $channel && $wishlist->getChannel()->getId() !== $channel->getId()) {
  77.             throw new WishlistNotFoundException(
  78.                 $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.wishlist_for_channel_not_found')
  79.             );
  80.         }
  81.         /** @var WishlistProductInterface $wishlistProduct */
  82.         $wishlistProduct $this->wishlistProductFactory->createForWishlistAndProduct($wishlist$product);
  83.         $wishlist->addWishlistProduct($wishlistProduct);
  84.         $this->wishlistManager->flush();
  85.         /** @var Session $session */
  86.         $session $this->requestStack->getSession();
  87.         $session->getFlashBag()->add('success'$this->translator->trans('bitbag_sylius_wishlist_plugin.ui.added_wishlist_item'));
  88.         $referer $request->headers->get('referer');
  89.         $refererPathInfo Request::create($referer)->getPathInfo();
  90.         return new RedirectResponse($refererPathInfo);
  91.     }
  92. }