src/Classes/Cart.php line 66

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