vendor/sylius/sylius/src/Sylius/Bundle/CoreBundle/Context/CustomerAndChannelBasedCartContext.php line 41

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\Repository\OrderRepositoryInterface;
  15. use Sylius\Component\Customer\Context\CustomerContextInterface;
  16. use Sylius\Component\Order\Context\CartContextInterface;
  17. use Sylius\Component\Order\Context\CartNotFoundException;
  18. use Sylius\Component\Order\Model\OrderInterface;
  19. final class CustomerAndChannelBasedCartContext implements CartContextInterface
  20. {
  21.     public function __construct(
  22.         private CustomerContextInterface $customerContext,
  23.         private ChannelContextInterface $channelContext,
  24.         private OrderRepositoryInterface $orderRepository,
  25.     ) {
  26.     }
  27.     public function getCart(): OrderInterface
  28.     {
  29.         try {
  30.             $channel $this->channelContext->getChannel();
  31.         } catch (ChannelNotFoundException) {
  32.             throw new CartNotFoundException('Sylius was not able to find the cart, as there is no current channel.');
  33.         }
  34.         $customer $this->customerContext->getCustomer();
  35.         if (null === $customer) {
  36.             throw new CartNotFoundException('Sylius was not able to find the cart, as there is no logged in user.');
  37.         }
  38.         $cart $this->orderRepository->findLatestNotEmptyCartByChannelAndCustomer($channel$customer);
  39.         if (null === $cart) {
  40.             throw new CartNotFoundException('Sylius was not able to find the cart for currently logged in user.');
  41.         }
  42.         return $cart;
  43.     }
  44. }