Linux SRV-16 6.18.44-paas #1 SMP PREEMPT_DYNAMIC Thu Aug 13 10:08:12 UTC 2026 x86_64
/
srv
/
data
/
trash
/
vhost_www.traiteur-75.fr_1689065491
/
htdocs
/
src
/
Services
/
//srv/data/trash/vhost_www.traiteur-75.fr_1689065491/htdocs/src/Services/CartService.php
<?php namespace App\Services; use App\Entity\Product; use App\Entity\User; use App\Repository\ConfigurationRepository; use App\Repository\ProductRepository; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Session\SessionInterface; /** * Class CartService */ class CartService { private $productRepository; private $session; private $configurationRepo; public function __construct(ProductRepository $productRepository, SessionInterface $session, ConfigurationRepository $configurationRepo) { $this->productRepository = $productRepository; $this->session = $session; $this->configurationRepo = $configurationRepo; } public function setCart(array $carts = null, int $id, int $qtt = 0) { if (null !== $carts) { $find = false; foreach ($carts as &$cart) { if ($cart['id'] == $id) { $find = true; $cart['qtt'] = $qtt; break; } } if (!$find) { $carts[] = [ 'id' => $id, 'qtt' => $qtt, ]; } } else { $carts = []; $carts[] = [ 'id' => $id, 'qtt' => $qtt, ]; } return $carts; } public function getCart() { $carts = $this->session->get('carts'); if (!empty($carts)) { foreach ($carts as &$cart) { $cart['object'] = $this->productRepository->find($cart['id']); } } return $carts; } public function getVat() { $carts = $this->getCart(); if (!empty($carts)) { $totalVat = 0; foreach ($carts as $cart) { $priceTotalProduct = $cart['object']->getPrice() * $cart['qtt']; $vatByProduct = $priceTotalProduct * ($cart['object']->getVat()->getValue()/100); $totalVat += $vatByProduct; } $totalVat = number_format($totalVat, 2, ',', ' '); return $totalVat; } return 0; } public function countCart() { $countCart = 0; $carts = $this->session->get('carts'); if (!empty($carts)) { foreach ($carts as $cart) { $countCart += $cart['qtt']; } } else { $countCart = 0; } return $countCart; } public function getTotal() { $carts = $this->getCart(); if (!empty($carts)) { $total = 0; foreach ($carts as $cart) { $total += $cart['object']->getPrice() * $cart['qtt']; } /* $total = number_format($total, 2, '.', ' '); */ return $total; } return 0; } // FONCTION GENERATE TOKEN public function getToken($length) { $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, $charactersLength - 1)]; } return $randomString; } }