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

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.     /** @var SessionInterface */
  20.     private $session;
  21.     /** @var string */
  22.     private $sessionKeyName;
  23.     /** @var OrderRepositoryInterface */
  24.     private $orderRepository;
  25.     public function __construct(
  26.         SessionInterface $session,
  27.         string $sessionKeyName,
  28.         OrderRepositoryInterface $orderRepository
  29.     ) {
  30.         $this->session $session;
  31.         $this->sessionKeyName $sessionKeyName;
  32.         $this->orderRepository $orderRepository;
  33.     }
  34.     public function hasForChannel(ChannelInterface $channel): bool
  35.     {
  36.         return $this->session->has($this->getCartKeyName($channel));
  37.     }
  38.     public function getForChannel(ChannelInterface $channel): ?OrderInterface
  39.     {
  40.         if ($this->hasForChannel($channel)) {
  41.             $cartId $this->session->get($this->getCartKeyName($channel));
  42.             return $this->orderRepository->findCartByChannel($cartId$channel);
  43.         }
  44.         return null;
  45.     }
  46.     public function setForChannel(ChannelInterface $channelOrderInterface $cart): void
  47.     {
  48.         $this->session->set($this->getCartKeyName($channel), $cart->getId());
  49.     }
  50.     public function removeForChannel(ChannelInterface $channel): void
  51.     {
  52.         $this->session->remove($this->getCartKeyName($channel));
  53.     }
  54.     private function getCartKeyName(ChannelInterface $channel): string
  55.     {
  56.         return sprintf('%s.%s'$this->sessionKeyName$channel->getCode());
  57.     }
  58. }