vendor/shopware/core/Checkout/Cart/SalesChannel/CartLoadRoute.php line 64

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Cart\SalesChannel;
  3. use Shopware\Core\Checkout\Cart\Cart;
  4. use Shopware\Core\Checkout\Cart\CartCalculator;
  5. use Shopware\Core\Checkout\Cart\CartPersisterInterface;
  6. use Shopware\Core\Checkout\Cart\Event\CartCreatedEvent;
  7. use Shopware\Core\Checkout\Cart\Exception\CartTokenNotFoundException;
  8. use Shopware\Core\Framework\Log\Package;
  9. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  10. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  11. use Shopware\Core\Framework\Routing\Annotation\Since;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  16. /**
  17.  * @Route(defaults={"_routeScope"={"store-api"}})
  18.  */
  19. #[Package('checkout')]
  20. class CartLoadRoute extends AbstractCartLoadRoute
  21. {
  22.     private CartPersisterInterface $persister;
  23.     private EventDispatcherInterface $eventDispatcher;
  24.     private CartCalculator $cartCalculator;
  25.     /**
  26.      * @internal
  27.      */
  28.     public function __construct(
  29.         CartPersisterInterface $persister,
  30.         EventDispatcherInterface $eventDispatcher,
  31.         CartCalculator $cartCalculator
  32.     ) {
  33.         $this->persister $persister;
  34.         $this->eventDispatcher $eventDispatcher;
  35.         $this->cartCalculator $cartCalculator;
  36.     }
  37.     public function getDecorated(): AbstractCartLoadRoute
  38.     {
  39.         throw new DecorationPatternException(self::class);
  40.     }
  41.     /**
  42.      * @Since("6.3.0.0")
  43.      * @Route("/store-api/checkout/cart", name="store-api.checkout.cart.read", methods={"GET", "POST"})
  44.      */
  45.     public function load(Request $requestSalesChannelContext $context): CartResponse
  46.     {
  47.         $name $request->get('name'CartService::SALES_CHANNEL);
  48.         $token $request->get('token'$context->getToken());
  49.         try {
  50.             $cart $this->persister->load($token$context);
  51.         } catch (CartTokenNotFoundException $e) {
  52.             $cart $this->createNew($token$name);
  53.         }
  54.         return new CartResponse($this->cartCalculator->calculate($cart$context));
  55.     }
  56.     private function createNew(string $tokenstring $name): Cart
  57.     {
  58.         $cart = new Cart($name$token);
  59.         $this->eventDispatcher->dispatch(new CartCreatedEvent($cart));
  60.         return $cart;
  61.     }
  62. }