vendor/sylius/sylius/src/Sylius/Bundle/ShopBundle/EventListener/SessionCartSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ShopBundle\EventListener;
  12. use Sylius\Component\Core\Model\OrderInterface;
  13. use Sylius\Component\Core\Storage\CartStorageInterface;
  14. use Sylius\Component\Order\Context\CartContextInterface;
  15. use Sylius\Component\Order\Context\CartNotFoundException;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. use Webmozart\Assert\Assert;
  20. final class SessionCartSubscriber implements EventSubscriberInterface
  21. {
  22.     public function __construct(private CartContextInterface $cartContext, private CartStorageInterface $cartStorage)
  23.     {
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             KernelEvents::RESPONSE => ['onKernelResponse'],
  29.         ];
  30.     }
  31.     public function onKernelResponse(ResponseEvent $event): void
  32.     {
  33.         if (\method_exists($event'isMainRequest')) {
  34.             $isMainRequest $event->isMainRequest();
  35.         } else {
  36.             $isMainRequest $event->isMasterRequest();
  37.         }
  38.         if (!$isMainRequest) {
  39.             return;
  40.         }
  41.         $request $event->getRequest();
  42.         if (!$request->hasSession() || !$request->getSession()->isStarted()) {
  43.             return;
  44.         }
  45.         try {
  46.             $cart $this->cartContext->getCart();
  47.             /** @var OrderInterface $cart */
  48.             Assert::isInstanceOf($cartOrderInterface::class);
  49.         } catch (CartNotFoundException) {
  50.             return;
  51.         }
  52.         if (null !== $cart->getId() && null !== $cart->getChannel()) {
  53.             $this->cartStorage->setForChannel($cart->getChannel(), $cart);
  54.         }
  55.     }
  56. }