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

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\CoreBundle\Storage;
  12. use Sylius\Component\Core\Model\ChannelInterface;
  13. use Sylius\Component\Core\Model\OrderInterface;
  14. use Sylius\Component\Core\Repository\OrderRepositoryInterface;
  15. use Sylius\Component\Core\Storage\CartStorageInterface;
  16. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  17. final class CartSessionStorage implements CartStorageInterface
  18. {
  19.     private SessionInterface $session;
  20.     private string $sessionKeyName;
  21.     private OrderRepositoryInterface $orderRepository;
  22.     public function __construct(
  23.         SessionInterface $session,
  24.         string $sessionKeyName,
  25.         OrderRepositoryInterface $orderRepository
  26.     ) {
  27.         $this->session $session;
  28.         $this->sessionKeyName $sessionKeyName;
  29.         $this->orderRepository $orderRepository;
  30.     }
  31.     public function hasForChannel(ChannelInterface $channel): bool
  32.     {
  33.         return $this->session->has($this->getCartKeyName($channel));
  34.     }
  35.     public function getForChannel(ChannelInterface $channel): ?OrderInterface
  36.     {
  37.         if ($this->hasForChannel($channel)) {
  38.             $cartId $this->session->get($this->getCartKeyName($channel));
  39.             return $this->orderRepository->findCartByChannel($cartId$channel);
  40.         }
  41.         return null;
  42.     }
  43.     public function setForChannel(ChannelInterface $channelOrderInterface $cart): void
  44.     {
  45.         $this->session->set($this->getCartKeyName($channel), $cart->getId());
  46.     }
  47.     public function removeForChannel(ChannelInterface $channel): void
  48.     {
  49.         $this->session->remove($this->getCartKeyName($channel));
  50.     }
  51.     private function getCartKeyName(ChannelInterface $channel): string
  52.     {
  53.         return sprintf('%s.%s'$this->sessionKeyName$channel->getCode());
  54.     }
  55. }