vendor/sylius/sylius/src/Sylius/Bundle/CoreBundle/Context/SessionAndChannelBasedCartContext.php line 40

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\Context;
  12. use Sylius\Component\Channel\Context\ChannelContextInterface;
  13. use Sylius\Component\Channel\Context\ChannelNotFoundException;
  14. use Sylius\Component\Core\Storage\CartStorageInterface;
  15. use Sylius\Component\Order\Context\CartContextInterface;
  16. use Sylius\Component\Order\Context\CartNotFoundException;
  17. use Sylius\Component\Order\Model\OrderInterface;
  18. final class SessionAndChannelBasedCartContext implements CartContextInterface
  19. {
  20.     /** @var CartStorageInterface */
  21.     private $cartStorage;
  22.     /** @var ChannelContextInterface */
  23.     private $channelContext;
  24.     public function __construct(CartStorageInterface $cartStorageChannelContextInterface $channelContext)
  25.     {
  26.         $this->cartStorage $cartStorage;
  27.         $this->channelContext $channelContext;
  28.     }
  29.     public function getCart(): OrderInterface
  30.     {
  31.         try {
  32.             $channel $this->channelContext->getChannel();
  33.         } catch (ChannelNotFoundException $exception) {
  34.             throw new CartNotFoundException(null$exception);
  35.         }
  36.         if (!$this->cartStorage->hasForChannel($channel)) {
  37.             throw new CartNotFoundException('Sylius was not able to find the cart in session');
  38.         }
  39.         $cart $this->cartStorage->getForChannel($channel);
  40.         if (null === $cart) {
  41.             $this->cartStorage->removeForChannel($channel);
  42.             throw new CartNotFoundException('Sylius was not able to find the cart in session');
  43.         }
  44.         return $cart;
  45.     }
  46. }