src/Services/Panier/PanierService.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Services\Panier;
  3. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  4. use App\Repository\ProduitsRepository;
  5. class PanierService
  6. {
  7.     private $session;
  8.     private $produitRepository;
  9.     public function __construct(SessionInterface $sessionProduitsRepository $produitsRepository)
  10.     {
  11.         $this->session $session;
  12.         $this->produitRepository $produitsRepository;
  13.     }
  14.     public function ajout($id)
  15.     {
  16.         $panier $this->session->get('panier', []);
  17.         //dd($id);
  18.         if (empty($panier[$id])) {
  19.             $panier[$id] = 1;
  20.         } else {
  21.             $panier[$id]++;
  22.         }
  23.         return $this->session->set('panier'$panier);
  24.     }
  25.     public function get()
  26.     {
  27.         return $this->session->get('panier');
  28.     }
  29.     public function vider()
  30.     {
  31.         return $this->session->remove('panier');
  32.     }
  33.     public function supprimer($id)
  34.     {
  35.         $panier $this->session->get('panier', []);
  36.         unset($panier[$id]);
  37.         return $this->session->set('panier'$panier);
  38.     }
  39.     public function dimunier($id)
  40.     {
  41.         $panier $this->session->get('panier', []);
  42.         //dd($id);
  43.         if ($panier[$id] > 1) {
  44.             $panier[$id]--;
  45.         } else {
  46.             unset($panier[$id]);
  47.         }
  48.         return $this->session->set('panier'$panier);
  49.     }
  50.     public function getFull()
  51.     {
  52.         $panierdata = [];
  53.         foreach ($this->get() as $id => $quantity) {
  54.             $panierdata[] = [
  55.                 'produit' => $this->produitRepository->find($id),
  56.                 'quantite' => $quantity
  57.             ];
  58.         }
  59.         $total 0;
  60.         foreach ($panierdata as $item) {
  61.             $subtotal = (int)$item['produit']->getPrix() * $item['quantite'];
  62.             $total += $subtotal;
  63.         }
  64.         return $panierdata;
  65.     }
  66. }