src/Service/Cart/CartService.php line 64

Open in your IDE?
  1. <?php 
  2. namespace App\Service\Cart;
  3. use App\Repository\ArticleRepository;
  4. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  5. class CartService{
  6.     private $session;
  7.     private $articleRepository;
  8.     public function __construct(SessionInterface $sessionInterfaceArticleRepository $articleRepository)
  9.     {
  10.         $this->session =$sessionInterface;
  11.         $this->articleRepository $articleRepository;
  12.     }
  13.     public function addPost(int $idint $qty)
  14.     {
  15.         $panier $this->session->get('panier',[]);
  16.         $panier[$id]=$qty;
  17.         
  18.         $this->session->set('panier',$panier);
  19.         // dd($panier);
  20.         
  21.     }
  22.     public function add(int $id)
  23.     {
  24.         $panier $this->session->get('panier',[]);
  25.         if(!empty($panier[$id])){
  26.             $panier[$id]++;
  27.         }else{
  28.             $panier[$id] = 1;
  29.         }
  30.         
  31.         $this->session->set('panier',$panier);
  32.         
  33.     }
  34.     public function remove(int $id)
  35.     {
  36.         $panier $this->session->get('panier',[]);
  37.         if(!empty($panier[$id])){
  38.             $panier[$id]--;
  39.         }else{
  40.             $panier[$id] = 1;
  41.         }
  42.         
  43.         $this->session->set('panier',$panier);
  44.         
  45.     }
  46.     public function delete(int $id)
  47.     {
  48.         $panier $this->session->get('panier',[]);
  49.         if(!empty($panier[$id])){
  50.             unset($panier[$id]);
  51.         }
  52.         $this->session->set('panier',$panier);
  53.     }
  54.     public function clear()
  55.     {
  56.         $this->session->set('panier',[]);
  57.     }
  58.     public function getFullCart(): array
  59.     {
  60.         $panier $this->session->get('panier',[]);
  61.         $panierWithData = [];
  62.         foreach($panier as $id => $quantite){
  63.             $panierWithData[]=[
  64.                 'article'=>$this->articleRepository->find($id),
  65.                 'quantite'=>$quantite
  66.             ];
  67.         }
  68.         return $panierWithData;
  69.     }
  70.     public function getTotal():float
  71.     {
  72.         $total 0;
  73.         foreach ($this->getFullCart() as $item) {
  74.                 $total+= $item['article']->getNewPrice() * $item['quantite'];
  75.         }
  76.         // dd($this->getFullCart());
  77.         return $total;
  78.     }
  79. }