vendor/sylius/sylius/src/Sylius/Bundle/CoreBundle/Storage/CartSessionStorage.php line 40

  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\CoreBundle\Storage;
  12. use Sylius\Bundle\CoreBundle\Provider\SessionProvider;
  13. use Sylius\Component\Core\Model\ChannelInterface;
  14. use Sylius\Component\Core\Model\OrderInterface;
  15. use Sylius\Component\Core\Repository\OrderRepositoryInterface;
  16. use Sylius\Component\Core\Storage\CartStorageInterface;
  17. use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  20. final class CartSessionStorage implements CartStorageInterface
  21. {
  22.     public function __construct(
  23.         private RequestStack|SessionInterface $requestStackOrSession,
  24.         private string $sessionKeyName,
  25.         private OrderRepositoryInterface $orderRepository,
  26.     ) {
  27.         if ($requestStackOrSession instanceof SessionInterface) {
  28.             trigger_deprecation('sylius/core-bundle''1.12'sprintf('Passing an instance of %s as constructor argument for %s is deprecated as of Sylius 1.12 and will be removed in 2.0. Pass an instance of %s instead.'SessionInterface::class, self::class, RequestStack::class));
  29.         }
  30.     }
  31.     public function hasForChannel(ChannelInterface $channel): bool
  32.     {
  33.         try {
  34.             return SessionProvider::getSession($this->requestStackOrSession)->has($this->getCartKeyName($channel));
  35.         } catch (SessionNotFoundException) {
  36.             return false;
  37.         }
  38.     }
  39.     public function getForChannel(ChannelInterface $channel): ?OrderInterface
  40.     {
  41.         if ($this->hasForChannel($channel)) {
  42.             $cartId SessionProvider::getSession($this->requestStackOrSession)->get($this->getCartKeyName($channel));
  43.             return $this->orderRepository->findCartByChannel($cartId$channel);
  44.         }
  45.         return null;
  46.     }
  47.     public function setForChannel(ChannelInterface $channelOrderInterface $cart): void
  48.     {
  49.         SessionProvider::getSession($this->requestStackOrSession)->set($this->getCartKeyName($channel), $cart->getId());
  50.     }
  51.     public function removeForChannel(ChannelInterface $channel): void
  52.     {
  53.         SessionProvider::getSession($this->requestStackOrSession)->remove($this->getCartKeyName($channel));
  54.     }
  55.     private function getCartKeyName(ChannelInterface $channel): string
  56.     {
  57.         return sprintf('%s.%s'$this->sessionKeyName$channel->getCode());
  58.     }
  59. }