vendor/sylius/sylius/src/Sylius/Bundle/CoreBundle/Collector/CartCollector.php line 79

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\Collector;
  12. use Sylius\Component\Core\Model\OrderInterface;
  13. use Sylius\Component\Core\Model\OrderItemInterface;
  14. use Sylius\Component\Order\Context\CartContextInterface;
  15. use Sylius\Component\Order\Context\CartNotFoundException;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  19. /**
  20.  * @internal
  21.  */
  22. final class CartCollector extends DataCollector
  23. {
  24.     public function __construct(private CartContextInterface $cartContext)
  25.     {
  26.         $this->data = [];
  27.     }
  28.     public function hasCart(): bool
  29.     {
  30.         return $this->data !== [];
  31.     }
  32.     public function getId(): ?int
  33.     {
  34.         return $this->data['id'];
  35.     }
  36.     public function getTotal(): ?int
  37.     {
  38.         return $this->data['total'];
  39.     }
  40.     public function getSubtotal(): ?int
  41.     {
  42.         return $this->data['subtotal'];
  43.     }
  44.     public function getCurrency(): ?string
  45.     {
  46.         return $this->data['currency'];
  47.     }
  48.     public function getLocale(): ?string
  49.     {
  50.         return $this->data['locale'];
  51.     }
  52.     public function getQuantity(): ?int
  53.     {
  54.         return $this->data['quantity'];
  55.     }
  56.     public function getItems(): ?array
  57.     {
  58.         return $this->data['items'];
  59.     }
  60.     public function getStates(): ?array
  61.     {
  62.         return $this->data['states'];
  63.     }
  64.     public function collect(Request $requestResponse $response\Throwable $exception null): void
  65.     {
  66.         try {
  67.             /** @var OrderInterface $cart */
  68.             $cart $this->cartContext->getCart();
  69.             $itemsData $cart->getItems()->map(static function (OrderItemInterface $item): array {
  70.                 $variant $item->getVariant();
  71.                 $product $variant->getProduct();
  72.                 return [
  73.                     'id' => $item->getId(),
  74.                     'variantName' => $variant->getName(),
  75.                     'variantId' => $variant->getId(),
  76.                     'variantCode' => $variant->getCode(),
  77.                     'quantity' => $item->getQuantity(),
  78.                     'productName' => $product->getName(),
  79.                     'productCode' => $product->getCode(),
  80.                     'productId' => $product->getId(),
  81.                 ];
  82.             })->toArray();
  83.             $this->data = [
  84.                 'id' => $cart->getId(),
  85.                 'total' => $cart->getTotal(),
  86.                 'subtotal' => $cart->getItemsTotal(),
  87.                 'currency' => $cart->getCurrencyCode(),
  88.                 'locale' => $cart->getLocaleCode(),
  89.                 'quantity' => count($cart->getItems()),
  90.                 'items' => $itemsData,
  91.                 'states' => [
  92.                     'main' => $cart->getState(),
  93.                     'checkout' => $cart->getCheckoutState(),
  94.                     'shipping' => $cart->getShippingState(),
  95.                     'payment' => $cart->getPaymentState(),
  96.                 ],
  97.             ];
  98.         } catch (CartNotFoundException) {
  99.             $this->data = [];
  100.         }
  101.     }
  102.     public function reset(): void
  103.     {
  104.         $this->data = [];
  105.     }
  106.     public function getName(): string
  107.     {
  108.         return 'sylius_cart';
  109.     }
  110. }