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

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