src/Service/Cart/CartService.php line 96

Open in your IDE?
  1. <?php
  2. namespace App\Service\Cart;
  3. use App\Entity\KitPromotionnel;
  4. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  5. use App\Repository\TrProduitsRepository;
  6. use App\Repository\TrCoeurRepository;
  7. use App\Repository\KitPromotionnelRepository;
  8. class CartService
  9. {
  10.     protected $session;
  11.     protected $TrProduitsRepository;
  12.     protected $TrCoeurRepository;
  13.     protected $kitPromotionnelRepository;
  14.     public function __construct(
  15.         SessionInterface $session,
  16.         TrProduitsRepository $TrProduitsRepository,
  17.         TrCoeurRepository $TrCoeurRepository,
  18.         KitPromotionnelRepository $kitPromotionnelRepository
  19.     )
  20.     {
  21.         $this->session = $session;
  22.         $this->TrProduitsRepository = $TrProduitsRepository;
  23.         $this->TrCoeurRepository = $TrCoeurRepository;
  24.         $this->kitPromotionnelRepository = $kitPromotionnelRepository;
  25.     }
  26.     public function add(int $id)
  27.     {
  28.         $panier = $this->session->get('panier', []);
  29.         if (!empty($panier[$id])) {
  30.             $panier[$id]++;
  31.         } else {
  32.             $panier[$id] = 1;
  33.         }
  34.         $this->session->set('panier', $panier);
  35.     }
  36.     public function subtraction(int $id)
  37.     {
  38.         $panier = $this->session->get('panier', []);
  39.         if (!empty($panier[$id])) {
  40.             if ($panier[$id] > 1) {
  41.                 $panier[$id]--;
  42.             } else {
  43.                 unset($panier[$id]);
  44.             }
  45.         }
  46.         $this->session->set('panier', $panier);
  47.     }
  48.     public function remove(int $id)
  49.     {
  50.         $panier = $this->session->get('panier', []);
  51.         if (!empty($panier[$id])) {
  52.             unset($panier[$id]);
  53.         }
  54.         $this->session->set('panier', $panier);
  55.     }
  56.     public function getFullCart(): array
  57.     {
  58.         $panier = $this->session->get('panier', []);
  59.         $panierwithData = [];
  60.         foreach ($panier as $id => $quantity) {
  61.             $panierwithData[] = [
  62.                 'product' => $this->TrProduitsRepository->find($id),
  63.                 'quantity' => $quantity
  64.             ];
  65.         }
  66.         return $panierwithData;
  67.     }
  68.     public function getTotal(): float
  69.     {
  70.         return $this->getProductsTotal() + $this->getKitsTotal();
  71.     }
  72.     public function getProductsTotal(): float
  73.     {
  74.         $total = 0;
  75.         foreach ($this->getFullCart() as $item) {
  76.             $total += $item['product']->getPrixActuel() * $item['quantity'];
  77.         }
  78.         return $total;
  79.     }
  80.     public function getKitsTotal(): float
  81.     {
  82.         $total = 0;
  83.         foreach ($this->getFullKitCart() as $item) {
  84.             $total += $item['kit']->getPrixKit() * $item['quantity'];
  85.         }
  86.         return $total;
  87.     }
  88.     public function getQuantite(): int
  89.     {
  90.         return $this->getProductLineCount() + $this->getKitLineCount();
  91.     }
  92.     public function getProductLineCount(): int
  93.     {
  94.         $quantite = 0;
  95.         foreach ($this->getFullCart() as $item) {
  96.             $quantite++;
  97.         }
  98.         return $quantite;
  99.     }
  100.     public function getKitLineCount(): int
  101.     {
  102.         $quantite = 0;
  103.         foreach ($this->getFullKitCart() as $item) {
  104.             $quantite++;
  105.         }
  106.         return $quantite;
  107.     }
  108.     public function addKit(int $id): bool
  109.     {
  110.         $kit = $this->kitPromotionnelRepository->findOneForApi($id);
  111.         if (!$kit instanceof KitPromotionnel || !$this->isKitOrderable($kit)) {
  112.             return false;
  113.         }
  114.         $panierKits = $this->session->get('panier_kits', []);
  115.         if (!empty($panierKits[$id])) {
  116.             $panierKits[$id]++;
  117.         } else {
  118.             $panierKits[$id] = 1;
  119.         }
  120.         $this->session->set('panier_kits', $panierKits);
  121.         return true;
  122.     }
  123.     public function subtractionKit(int $id): void
  124.     {
  125.         $panierKits = $this->session->get('panier_kits', []);
  126.         if (!empty($panierKits[$id])) {
  127.             if ($panierKits[$id] > 1) {
  128.                 $panierKits[$id]--;
  129.             } else {
  130.                 unset($panierKits[$id]);
  131.             }
  132.         }
  133.         $this->session->set('panier_kits', $panierKits);
  134.     }
  135.     public function removeKit(int $id): void
  136.     {
  137.         $panierKits = $this->session->get('panier_kits', []);
  138.         if (!empty($panierKits[$id])) {
  139.             unset($panierKits[$id]);
  140.         }
  141.         $this->session->set('panier_kits', $panierKits);
  142.     }
  143.     public function clearKitCart(): void
  144.     {
  145.         $this->session->remove('panier_kits');
  146.     }
  147.     public function getFullKitCart(): array
  148.     {
  149.         $panierKits = $this->session->get('panier_kits', []);
  150.         $kitCartWithData = [];
  151.         foreach ($panierKits as $id => $quantity) {
  152.             $kit = $this->kitPromotionnelRepository->findOneForApi((int) $id);
  153.             if (!$kit instanceof KitPromotionnel) {
  154.                 continue;
  155.             }
  156.             $kitCartWithData[] = [
  157.                 'kit' => $kit,
  158.                 'quantity' => $quantity,
  159.                 'is_orderable' => $this->isKitOrderable($kit),
  160.             ];
  161.         }
  162.         return $kitCartWithData;
  163.     }
  164.     public function isKitOrderable(KitPromotionnel $kit): bool
  165.     {
  166.         if (!$kit->isDisponible()) {
  167.             return false;
  168.         }
  169.         foreach ($kit->getItems() as $item) {
  170.             if (null === $item->getProduit()) {
  171.                 return false;
  172.             }
  173.         }
  174.         return true;
  175.     }
  176.     public function validateKitCart(): array
  177.     {
  178.         $errors = [];
  179.         foreach ($this->getFullKitCart() as $item) {
  180.             if (!$item['is_orderable']) {
  181.                 $errors[] = sprintf('Le kit "%s" n est plus disponible.', $item['kit']->getNom());
  182.             }
  183.         }
  184.         return $errors;
  185.     }
  186.     public function getCoeur(): array
  187.     {
  188.         return $this->TrCoeurRepository->findAll();
  189.     }
  190. }