src/Service/CartService.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\App\Product;
  4. use App\Repository\App\ProductRepository;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  8. class CartService
  9. {
  10.     public function __construct(
  11.         private readonly RequestStack $requestStack,
  12.         private readonly ProductRepository $productRepository,
  13.     ) {
  14.     }
  15.     public function getProductCart(): array
  16.     {
  17.         return $this->getSession()->get('products') ?? [];
  18.     }
  19.     public function addToCart(int $idRequest $request): void
  20.     {
  21.         $customization $request->request->get('customization') ?? 'default';
  22.         $productsCart $this->getProductCart();
  23.         if (!empty($productsCart[$id])) {
  24.             ++$productsCart[$id]['quantity'];
  25.         } else {
  26.             $productsCart[$id] = [
  27.                 'id' => $id,
  28.                 'quantity' => 1,
  29.                 'customization' => $customization,
  30.             ];
  31.         }
  32.         $this->getSession()->set('products'$productsCart);
  33.     }
  34.     public function updateQuantityForProduct(int $productIdint $quantity): int
  35.     {
  36.         $productsCart $this->getProductCart();
  37.         $price 0;
  38.         $total 0;
  39.         foreach ($productsCart as $productIdKey => &$productDetails) {
  40.             /** @var Product $product */
  41.             $product $this->productRepository->findOneById($productDetails['id']);
  42.             if ($productDetails['id'] === $productId) {
  43.                 $productDetails['quantity'] = $quantity;
  44.             }
  45.             $price $product->getPrice() * $productDetails['quantity'];
  46.             $total += $price;
  47.         }
  48.         unset($productDetails);
  49.         $this->getSession()->set('products'$productsCart);
  50.         return $total;
  51.     }
  52.     public function removeAllProductsForId(int $id): void
  53.     {
  54.         $productsCart $this->getProductCart();
  55.         if (!empty($productsCart[$id])) {
  56.             unset($productsCart[$id]);
  57.         }
  58.         $this->getSession()->set('products'$productsCart);
  59.     }
  60.     public function getProducts(): array
  61.     {
  62.         $productsCart $this->getProductCart();
  63.         $products = [];
  64.         foreach ($productsCart as $productId => $quantity) {
  65.             $product $this->productRepository->findOneById($productId);
  66.             $products[] = [
  67.                 'product' => $product,
  68.                 'quantity' => $quantity,
  69.             ];
  70.         }
  71.         return $products;
  72.     }
  73.     public function removeCart(): void
  74.     {
  75.         $this->getSession()->clear();
  76.     }
  77.     /*private function generateCustomizationKey(int $id, string $customization): string
  78.     {
  79.         return $id.'-'.md5(json_encode($customization));
  80.     }*/
  81.     private function getSession(): SessionInterface
  82.     {
  83.         return $this->requestStack->getSession();
  84.     }
  85. }