src/Service/CartManager.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  4. use App\Repository\EventRepository;
  5. class CartManager {
  6.     private $eventRepository;
  7.     private $session;
  8.     public function __construct(EventRepository $eventRepositorySessionInterface $sessionInterface){
  9.         $this->eventRepository $eventRepository;
  10.         $this->session $sessionInterface;
  11.     }
  12.     public function getCart()
  13.     {
  14.         $cart $this->session->get("panier", []);
  15.         $dataCart = [];
  16.         $total 0;
  17.         foreach($cart as $id => $quantity){
  18.             $event $this->eventRepository->findOneBy(['uid' => $id]);
  19.             if($event->getPresent()){ 
  20.                 $price $event->getProduct()->getPresentPrice();
  21.             } else {
  22.                 $price $event->getProduct()->getVisioPrice();
  23.             }
  24.             $dataCart[] = [
  25.                 "event" => $event,
  26.                 "quantity" => $quantity,
  27.                 "price" => $price
  28.             ];
  29.             $total += $price $quantity;
  30.         }
  31.         return [
  32.             'dataCart' => $dataCart
  33.             'total' => $total
  34.         ];
  35.     }
  36.     public function emptyCart() {
  37.         $this->session->set("panier", []);
  38.     }
  39. }