src/Service/Cart/CartService.php line 66

  1. <?php
  2. namespace App\Service\Cart;
  3. use App\Entity\Product;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. class CartService
  7. {
  8.     private RequestStack $session;
  9.     private EntityManagerInterface $manager;
  10.     public function __construct(RequestStack $sessionEntityManagerInterface $manager)
  11.     {
  12.         $this->session $session;
  13.         $this->manager $manager;
  14.     }
  15.     public function add(int $idint $quantity): bool
  16.     {
  17.         $cart $this->session->getSession()->get('cart', []);
  18.         $product $this->manager->getRepository(Product::class)->find($id);
  19.         if(!$product) {
  20.             return false;
  21.         }
  22.         $maxQuantity $product->getProductQuantity();
  23.         $currentQuantity $cart[$id] ?? 0;
  24.         $quantityToAdd $quantity;
  25.         if ($currentQuantity $quantityToAdd $maxQuantity) {
  26.             return false;
  27.         }
  28.         if($quantityToAdd 0){
  29.             if(array_key_exists($id$cart)) {
  30.                 $cart[$id] += $quantityToAdd;
  31.             } else {
  32.                 $cart[$id] = $quantityToAdd;
  33.             }
  34.             $this->session->getSession()->set('cart'$cart);
  35.             return true;
  36.         }else{
  37.             return false;
  38.         }
  39.     }
  40.     public function remove(int $id): void
  41.     {
  42.         $cart $this->session->getSession()->get('cart', []);
  43.         if(array_key_exists($id$cart)) {
  44.             unset($cart[$id]);
  45.         }
  46.         $this->session->getSession()->set('cart'$cart);
  47.     }
  48.     public function getCart(): array
  49.     {
  50.         $cart $this->session->getSession()->get('cart', []);
  51.         $cartWithData = [];
  52.         foreach ($cart as $id => $quantity) {
  53.             $product $this->manager->getRepository(Product::class)->find($id);
  54.             $images $product->getImages();
  55.             if ($images !== null && count($images) > 0) {
  56.                 $img $images[0]->getImageName();
  57.             } else {
  58.                 $img "default.png";
  59.             }
  60.             $cartWithData[] = [
  61.                 'product' => $product,
  62.                 'quantity' => $quantity,
  63.                 'image' => $img
  64.             ];
  65.         }
  66.         return $cartWithData;
  67.     }
  68.     public function getTotal(): float
  69.     {
  70.         $cartTotal 0;
  71.         foreach ($this->getCart() as $item) {
  72.             $cartTotal += $item['product']->getProductPrice() * $item['quantity'];
  73.         }
  74.         return $cartTotal;
  75.     }
  76.     public function emptyCart(): void
  77.     {
  78.         $this->session->getSession()->remove('cart');
  79.     }
  80.     public function updateQuantity(int $productId$quantity): void
  81.     {
  82.         $cart $this->session->getSession()->get('cart', []);
  83.         if(array_key_exists($productId$cart)) {
  84.             $cart[$productId] = $quantity;
  85.         }
  86.         $this->session->getSession()->set('cart'$cart);
  87.     }
  88. }