vendor/bitbag/wishlist-plugin/src/EventSubscriber/CreateNewWishlistSubscriber.php line 88

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\EventSubscriber;
  9. use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
  10. use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface;
  11. use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
  12. use BitBag\SyliusWishlistPlugin\Resolver\WishlistsResolverInterface;
  13. use Sylius\Component\Channel\Context\ChannelContextInterface;
  14. use Sylius\Component\Channel\Context\ChannelNotFoundException;
  15. use Sylius\Component\Core\Model\ChannelInterface;
  16. use Sylius\Component\Core\Model\ShopUserInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpFoundation\Cookie;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\HttpKernel\Event\RequestEvent;
  21. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  22. use Symfony\Component\HttpKernel\KernelEvents;
  23. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  24. final class CreateNewWishlistSubscriber implements EventSubscriberInterface
  25. {
  26.     private string $wishlistCookieToken;
  27.     private WishlistsResolverInterface $wishlistsResolver;
  28.     private WishlistFactoryInterface $wishlistFactory;
  29.     private WishlistRepositoryInterface $wishlistRepository;
  30.     private TokenStorageInterface $tokenStorage;
  31.     private ChannelContextInterface $channelContext;
  32.     public function __construct(
  33.         string $wishlistCookieToken,
  34.         WishlistsResolverInterface $wishlistsResolver,
  35.         WishlistFactoryInterface $wishlistFactory,
  36.         WishlistRepositoryInterface $wishlistRepository,
  37.         TokenStorageInterface $tokenStorage,
  38.         ChannelContextInterface $channelContext
  39.     ) {
  40.         $this->wishlistCookieToken $wishlistCookieToken;
  41.         $this->wishlistsResolver $wishlistsResolver;
  42.         $this->wishlistFactory $wishlistFactory;
  43.         $this->wishlistRepository $wishlistRepository;
  44.         $this->tokenStorage $tokenStorage;
  45.         $this->channelContext $channelContext;
  46.     }
  47.     public static function getSubscribedEvents(): array
  48.     {
  49.         return [
  50.             KernelEvents::REQUEST => [['onKernelRequest'1]],
  51.             KernelEvents::RESPONSE => [['onKernelResponse'0]],
  52.         ];
  53.     }
  54.     public function onKernelRequest(RequestEvent $event): void
  55.     {
  56.         if (!$event->isMasterRequest()) {
  57.             return;
  58.         }
  59.         /** @var WishlistInterface[] $wishlists */
  60.         $wishlists $this->wishlistsResolver->resolve();
  61.         $wishlistCookieToken $event->getRequest()->cookies->get($this->wishlistCookieToken);
  62.         if ($wishlistCookieToken && !empty($wishlists)) {
  63.             return;
  64.         }
  65.         /** @var WishlistInterface $wishlist */
  66.         $wishlist $this->createNewWishlist($wishlistCookieToken);
  67.         $event->getRequest()->attributes->set($this->wishlistCookieToken$wishlist->getToken());
  68.     }
  69.     public function onKernelResponse(ResponseEvent $event): void
  70.     {
  71.         if (!$event->isMasterRequest()) {
  72.             return;
  73.         }
  74.         if ($event->getRequest()->cookies->has($this->wishlistCookieToken)) {
  75.             return;
  76.         }
  77.         $response $event->getResponse();
  78.         $wishlistCookieToken $event->getRequest()->attributes->get($this->wishlistCookieToken);
  79.         if (!$wishlistCookieToken) {
  80.             return;
  81.         }
  82.         $this->setWishlistCookieToken($response$wishlistCookieToken);
  83.         $event->getRequest()->attributes->remove($this->wishlistCookieToken);
  84.     }
  85.     private function createNewWishlist(?string $wishlistCookieToken): WishlistInterface
  86.     {
  87.         $user $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
  88.         $wishlist $this->wishlistFactory->createNew();
  89.         try {
  90.             $channel $this->channelContext->getChannel();
  91.         } catch (ChannelNotFoundException $exception) {
  92.             $channel null;
  93.         }
  94.         if ($channel instanceof ChannelInterface) {
  95.             $wishlist->setChannel($channel);
  96.         }
  97.         if ($channel instanceof ChannelInterface &&
  98.             $user instanceof ShopUserInterface
  99.         ) {
  100.             $wishlist $this->wishlistFactory->createForUserAndChannel($user$channel);
  101.         } elseif ($user instanceof ShopUserInterface) {
  102.             $wishlist $this->wishlistFactory->createForUser($user);
  103.         }
  104.         if ($wishlistCookieToken) {
  105.             $wishlist->setToken($wishlistCookieToken);
  106.         }
  107.         $wishlist->setName('Wishlist');
  108.         $this->wishlistRepository->add($wishlist);
  109.         return $wishlist;
  110.     }
  111.     private function setWishlistCookieToken(Response $responsestring $wishlistCookieToken): void
  112.     {
  113.         $cookie = new Cookie($this->wishlistCookieToken$wishlistCookieTokenstrtotime('+1 year'));
  114.         $response->headers->setCookie($cookie);
  115.     }
  116. }