src/Service/CartService.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Repository\ActivityRepository;
  4. use App\Entity\Activity;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use App\Service\CartItem;
  7. use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
  8. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  9. class CartService
  10. {
  11.     protected $session;
  12.     protected $activityRepository;
  13.     protected $activity;
  14.     protected $requestStack;
  15.     public function __construct( Activity $activity , ActivityRepository $activityRepository, RequestStack $requestStack, SessionInterface $session ) {
  16.         $this->session = $session;
  17.         $this->activityRepository = $activityRepository;
  18.         $this->activity = $activity;
  19.         $this->requestStack = $requestStack;
  20.     }
  21.     protected function getCart() : array {
  22.         return $this->session->get('cart', []);
  23.     }
  24.     protected function saveCart(array $cart)
  25.     {
  26.         return $this->session->set('cart', $cart);
  27.     }
  28.     public function empty() {
  29.         $this->saveCart([]);
  30.     }
  31.     public function add(int $id) {
  32.         //1 retrouver le panier dans la session
  33.         //2 tableau vide si pas existant
  34.         $cart = $this->getCart();
  35.         //3 voir si le produit id existe
  36.         //5 Sinon  quantité 1
  37.         if (!array_key_exists($id, $cart)) {
  38.             $cart[$id] = 0;
  39.         }
  40.         $cart[$id]++;
  41.         //6 enregisterr le tableua dans la session
  42.         $this->saveCart($cart);
  43.     }
  44.     public function remove(int $id)
  45.     {
  46.         // on récupère le panier actuel
  47.         $cart = $this->getCart();
  48.         unset($cart[$id]);
  49.         $this->saveCart($cart);
  50.     }
  51.     public function decrement(int $id)
  52.     {
  53.         $cart = $this->getCart();
  54.         if (!array_key_exists($id, $cart)) {
  55.             return;
  56.         }
  57.         //si le Activity est à 1 alors on supprime
  58.         if ($cart[$id] === 1) {
  59.             $this->remove($id);
  60.             return;
  61.         }
  62.         //sinon on le decremente
  63.         $cart[$id]--;
  64.         $this->saveCart($cart);
  65.     }
  66.     public function getTotal():int {
  67.         $total = 0;
  68.         foreach($this->getCart() as $id => $quantity) {
  69.             $activity = $this->activityRepository->find($id);
  70.             if(!$activity) {
  71.                 continue;
  72.             }
  73.             $total += $activity->getPrice() * $quantity;
  74.             //dd($total);
  75.         }
  76.         return $total;
  77.     }
  78.     public function getStock(): int {
  79.         // Utilisez la méthode getStockActivitie() du ActivityRepository pour récupérer la quantité  panier pour un produit donné
  80.         $stock = $this->activity->getStock();
  81.         if ($stock === null) {
  82.             return 0;
  83.         }
  84.         // Retournez le stock
  85.         return $stock;
  86.     }
  87.     public function validateOrder(): bool
  88.     {
  89.         $cartItems = $this->getDetailedCartItems();
  90.         $isValid = true;
  91.         foreach ($cartItems as $cartItem) {
  92.             $quantityOrdered = $cartItem->quantity;
  93.             $stockAvailable = $cartItem->stock;
  94.             if ($quantityOrdered > $stockAvailable) {
  95.                 $isValid = false;
  96.                 break; // Arrêter la boucle dès qu'une quantité dépasse le stock disponible
  97.             }
  98.         }
  99.         return $isValid;
  100.     }
  101.     /**
  102.      * @return CartItem[]
  103.      */
  104.     public function getDetailedCartItems(): array {
  105.         $detailedCart = [];
  106.         foreach ($this->getCart() as $id => $quantity) {
  107.             $activity = $this->activityRepository->find($id);
  108.             // si un Activity est supprimé, on continue la boucle
  109.             if(!$activity) {
  110.                 continue;
  111.             }
  112.             $stock = $activity->getStock();
  113.             // dd($detailedCart);
  114.             $categoryTitle = $activity->getCategory()->getTitleCategory(); // Déclaration de la variable $categoryTitle
  115.             $detailedCart[] = new CartItem($activity, $quantity , $stock,  $categoryTitle);
  116.         }
  117.         //dd($detailedCart);
  118.         return $detailedCart;
  119.     }
  120.     public function removeAll()
  121.     {
  122.         $this->session->set("cart", []);
  123.     }
  124. }