src/Classes/Cart.php line 74

Open in your IDE?
  1. <?php 
  2. namespace App\Classes;
  3. use App\Entity\Product;
  4. use App\Entity\User;
  5. use App\Entity\ShoppingCart;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  8. use Symfony\Component\Security\Core\Security;
  9. class Cart{
  10.     private $session;
  11.     private $entityManager;
  12.     private $security;
  13.     public function __construct(EntityManagerInterface $entityManagerSessionInterface $sessionSecurity $security){
  14.         $this->session $session;
  15.         $this->entityManager $entityManager;
  16.         $this->security $security;
  17.     }
  18.    
  19.     public function add($id)
  20.     {
  21.         if ($this->security->isGranted('IS_AUTHENTICATED_FULLY')) {
  22.             $user $this->security->getUser();
  23.             $userId $user->getId();
  24.             $shoppingCart = new ShoppingCart($this->entityManager$this->security);
  25.             $cartItems $shoppingCart->getUserCart();
  26.             $productExists false;
  27.             foreach ($cartItems as $cartItem) {
  28.                 if ($cartItem->getProductId() == $id) {
  29.                     $newQuantity $cartItem->getQty() + 1;
  30.                     $cartItem->setQty($newQuantity);
  31.                     $this->entityManager->persist($cartItem);
  32.                     $productExists true;
  33.                     break;
  34.                 }
  35.             }
  36.             if (!$productExists) {
  37.                 $product $this->entityManager->getRepository(Product::class)->find($id);
  38.                 if ($product) {
  39.                     $shoppingCart = new ShoppingCart($this->entityManager$this->security);
  40.                     $shoppingCart->setProductId($product->getId());
  41.                     $shoppingCart->setProductName($product->getName());
  42.                     $shoppingCart->setProductPrice($product->getPrice());
  43.                     $shoppingCart->setQty(1);
  44.                     $shoppingCart->setProductImage($product->getIllustration());
  45.                     $shoppingCart->setTotal($product->getPrice());
  46.                     $shoppingCart->setUserId($userId);
  47.                     $shoppingCart->setCreatedAt(new \DateTime());
  48.                     $shoppingCart->setUpdatedAt(new \DateTime());
  49.                     $this->entityManager->persist($shoppingCart);
  50.                 }
  51.             }
  52.             $this->entityManager->flush();
  53.         } else {
  54.             $cart $this->session->get('cart', []);
  55.             if (!empty($cart[$id])) {
  56.                 $cart[$id]++;
  57.             } else {
  58.                 $cart[$id] = 1;
  59.             }
  60.             $this->session->set('cart'$cart);
  61.         }
  62.     }
  63.     public function get(){
  64.         return $this->session->get('cart');
  65.     }
  66.     public function remove(){
  67.         return $this->session->get('cart');
  68.     }
  69.     public function delete($id)
  70.     {
  71.         if ($this->security->isGranted('IS_AUTHENTICATED_FULLY')) {
  72.             // Récupérez le panier de l'utilisateur connecté
  73.             $user $this->security->getUser();
  74.             $userId $user->getId();
  75.     
  76.             $shoppingCart = new ShoppingCart($this->entityManager$this->security);
  77.     
  78.             $cartItems $shoppingCart->getUserCart();
  79.     
  80.             foreach ($cartItems as $cartItem) {
  81.                 if ($cartItem->getProductId() == $id) {
  82.                     $this->entityManager->remove($cartItem);
  83.                     $this->entityManager->flush();
  84.                     break;
  85.                 }
  86.             }
  87.         } else {
  88.             $cart $this->session->get('cart');
  89.     
  90.             if (isset($cart[$id])) {
  91.                 unset($cart[$id]);
  92.                 $this->session->set('cart'$cart);
  93.             }
  94.         }
  95.     }    
  96.     public function decrease($id){
  97.         $cart $this->session->get('cart', []);
  98.         if ($this->security->isGranted('IS_AUTHENTICATED_FULLY')) { 
  99.             // Récupérer le panier de l'utilisateur connecté dans l'entité "ShoppingCart"   
  100.             $shoppingCart = new ShoppingCart($this->entityManager$this->security);
  101.             $cartItems $shoppingCart->getUserCart();
  102.             // Mise à jour de la quantité
  103.             foreach ($cartItems as $cartItem) {
  104.                 if ($cartItem->getProductId() == $id) {
  105.                     if ($cartItem->getQty() > 1) {
  106.                         $newQuantity $cartItem->getQty() - 1;
  107.                         $cartItem->setQty($newQuantity);
  108.                         $this->entityManager->persist($cartItem);
  109.                     } else {
  110.                         $this->entityManager->remove($cartItem);
  111.                     }
  112.                     $this->entityManager->flush();
  113.                     break;
  114.                 }
  115.             }       
  116.         } else{
  117.             // Vérifier si le produit à une qté supérieur à 1
  118.             if($cart[$id] > 1){
  119.                 // Retirer une qté
  120.                 $cart[$id]--;
  121.             } else{
  122.                 // On supprimer le produit
  123.                 unset($cart[$id]);
  124.             }
  125.         }        
  126.         return $this->session->set('cart'$cart);
  127.     }
  128.     public function getFull(): array
  129.     {
  130.         $cartComplete = [];
  131.     
  132.         if ($this->security && $this->security->getUser()) {
  133.             $userId $this->security->getUser()->getId();
  134.             
  135.             $cartItems $this->entityManager->getRepository(ShoppingCart::class)->findBy([
  136.                 'userId' => $userId,
  137.             ]);
  138.     
  139.             foreach ($cartItems as $cartItem) {
  140.                 $productData = [
  141.                     'id' => $cartItem->getId(),
  142.                     'productId' => $cartItem->getProductId(),
  143.                     'name' => $cartItem->getProductName(),
  144.                     'price' => $cartItem->getProductPrice(),
  145.                     'qty' => $cartItem->getQty(),
  146.                     'illustration' => $cartItem->getProductImage(),
  147.                     'total' => $cartItem->getTotal(),
  148.                 ];
  149.     
  150.                 $cartComplete[] = [
  151.                     'product' => $productData,
  152.                     'quantity' => $cartItem->getQty()
  153.                 ];
  154.             }
  155.         }
  156.     
  157.         return $cartComplete;
  158.     }    
  159. }