src/Tools/Cart/CartTools.php line 89

Open in your IDE?
  1. <?php
  2. namespace App\Tools\Cart;
  3. use App\Repository\ArticlesRepository;
  4. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  5. class CartTools
  6. {
  7.     protected $session;
  8.     protected $articlesRepository;
  9.     public $tva 0;
  10.     /**
  11.      * Constructeur de la class panier outils
  12.      *
  13.      * @param SessionInterface $session
  14.      * @param ProductRepository $productRepository
  15.      */
  16.     public function __construct(SessionInterface $sessionArticlesRepository $articlesRepository)
  17.     {
  18.         $this->session $session;
  19.         $this->articlesRepository $articlesRepository;
  20.     }
  21.     /**
  22.      * Ajouter produit(s) au panier
  23.      *
  24.      * @param integer $id
  25.      * @return void
  26.      */
  27.     public function add(int $id)
  28.     {
  29.         $panier $this->session->get('panier', []);
  30.         if (!empty($panier[$id])) {
  31.             $panier[$id]++;
  32.         } else {
  33.             $panier[$id] = 1;
  34.         }
  35.         $this->session->set('panier'$panier);
  36.     }
  37.     /**
  38.      * retirer produit(s) au panier
  39.      *
  40.      * @param integer $id
  41.      * @return void
  42.      */
  43.     public function retirer(int $id)
  44.     {
  45.         $panier $this->session->get('panier', []);
  46.         if (!empty($panier[$id])) {
  47.             $panier[$id]--;
  48.         } else {
  49.             $panier[$id] = 1;
  50.         }
  51.         $this->session->set('panier'$panier);
  52.     }
  53.     /**
  54.      * Supprime élément(s) du panier
  55.      *
  56.      * @param integer $id
  57.      * @return void
  58.      */
  59.     public function remove(int $id)
  60.     {
  61.         $panier $this->session->get('panier', []);
  62.         if (!empty($panier[$id])) {
  63.             unset($panier[$id]);
  64.         }
  65.         $this->session->set('panier'$panier);
  66.     }
  67.     /**
  68.      * Contenu du panier 
  69.      *
  70.      * @return array
  71.      */
  72.     public function getFullCart(): array
  73.     {
  74.         $panier $this->session->get('panier', []);
  75.         $panierWithData = [];
  76.         foreach ($panier as $id => $quantity) {
  77.             $panierWithData[] = [
  78.                 'articles' => $this->articlesRepository->find($id),
  79.                 'quantite' => $quantity
  80.             ];
  81.         }
  82.         return $panierWithData;
  83.     }
  84.     /**
  85.      * Total du panier
  86.      *
  87.      * @return float
  88.      */
  89.     public function getTotalTTC(): float
  90.     {
  91.         $total 0;
  92.         foreach ($this->getFullCart() as $item) {
  93.             $total += $item['articles']->getPrixdeVente() * $item['quantite'];
  94.         }
  95.         return $total;
  96.     }
  97.     /**
  98.      * Nb d'article du panier
  99.      *
  100.      * @return int
  101.      */
  102.     public function getTotalItem()
  103.     {
  104.         $total 0;
  105.         foreach ($this->getFullCart() as $item) {
  106.             $total += $item['quantite'];
  107.         }
  108.         return $total;
  109.     }
  110.     /**
  111.      * return TVA
  112.      *
  113.      * @return void
  114.      */
  115.     public function getTva()
  116.     {
  117.         return $this->tva;
  118.     }
  119.     /**
  120.      * Calcul TVA
  121.      *
  122.      * @return float
  123.      */
  124.     public function getTotalTVA()
  125.     {
  126.         $total $this->getTotalTTC();
  127.         return number_format($total $this->getTva() / 1002);
  128.     }
  129. }