src/EventSubscriber/CreateNewWishlistSubscriber.php line 67

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 App\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.         // décommenter si le site n'a pas de wishlist
  57. //         return;
  58.         if (!$event->isMainRequest()) {
  59.             return;
  60.         }
  61.         $url $event->getRequest()->getRequestUri();
  62.         if (!str_starts_with($url'/wishlist')) {
  63.             return;
  64.         }
  65.         /** @var WishlistInterface[] $wishlists */
  66.         $wishlists $this->wishlistsResolver->resolve();
  67.         $wishlistCookieToken $event->getRequest()->cookies->get($this->wishlistCookieToken);
  68.         if ($wishlistCookieToken && !empty($wishlists)) {
  69.             return;
  70.         }
  71.         /** @var WishlistInterface $wishlist */
  72.         $wishlist $this->createNewWishlist($wishlistCookieToken);
  73.         $event->getRequest()->attributes->set($this->wishlistCookieToken$wishlist->getToken());
  74.     }
  75.     public function onKernelResponse(ResponseEvent $event): void
  76.     {
  77.         // décommenter si le site n'a pas de wishlist
  78. //         return;
  79.         if (!$event->isMainRequest()) {
  80.             return;
  81.         }
  82.         if ($event->getRequest()->cookies->has($this->wishlistCookieToken)) {
  83.             return;
  84.         }
  85.         $response $event->getResponse();
  86.         $wishlistCookieToken $event->getRequest()->attributes->get($this->wishlistCookieToken);
  87.         if (!$wishlistCookieToken) {
  88.             return;
  89.         }
  90.         $this->setWishlistCookieToken($response$wishlistCookieToken);
  91.         $event->getRequest()->attributes->remove($this->wishlistCookieToken);
  92.     }
  93.     private function createNewWishlist(?string $wishlistCookieToken): WishlistInterface
  94.     {
  95.         $user $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
  96.         $wishlist $this->wishlistFactory->createNew();
  97.         try {
  98.             $channel $this->channelContext->getChannel();
  99.         } catch (ChannelNotFoundException $exception) {
  100.             $channel null;
  101.         }
  102.         if ($channel instanceof ChannelInterface) {
  103.             $wishlist->setChannel($channel);
  104.         }
  105.         if ($channel instanceof ChannelInterface &&
  106.             $user instanceof ShopUserInterface
  107.         ) {
  108.             $wishlist $this->wishlistFactory->createForUserAndChannel($user$channel);
  109.         } elseif ($user instanceof ShopUserInterface) {
  110.             $wishlist $this->wishlistFactory->createForUser($user);
  111.         }
  112.         if ($wishlistCookieToken) {
  113.             $wishlist->setToken($wishlistCookieToken);
  114.         }
  115.         $wishlist->setName('Wishlist');
  116.         $this->wishlistRepository->add($wishlist);
  117.         return $wishlist;
  118.     }
  119.     private function setWishlistCookieToken(Response $responsestring $wishlistCookieToken): void
  120.     {
  121.         $cookie = new Cookie($this->wishlistCookieToken$wishlistCookieTokenstrtotime('+1 year'));
  122.         $response->headers->setCookie($cookie);
  123.     }
  124. }