<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use App\Repository\ProductRepository;
use App\Repository\ProductCategoryRepository;
use App\Service\FileHandler;
use App\Service\BasketService;
use App\Service\OrderService;
use App\Entity\BasketItem;
use App\Entity\Product;
use App\Service\ConfigService;
use Exception;
use Symfony\Contracts\Translation\TranslatorInterface;
#[Route('/cart')]
class CartController extends AbstractController
{
private $entityManager;
private $productRepository;
private $productCategoryRepository;
private $fileHandler;
private $basketService;
private $orderService;
private $translator;
public function __construct(EntityManagerInterface $entityManager,
ProductRepository $productRepository,
ProductCategoryRepository $productCategoryRepository,
BasketService $basketService,
OrderService $orderService,
FileHandler $fileHandler,
TranslatorInterface $translator
)
{
$this->entityManager = $entityManager;
$this->productRepository = $productRepository;
$this->productCategoryRepository = $productCategoryRepository;
$this->basketService = $basketService;
$this->orderService = $orderService;
$this->fileHandler = $fileHandler;
$this->translator = $translator;
}
#[Route('/', name: 'app_cart_index')]
public function index(): Response
{
$error = null;
$basket = $this->basketService->getBasket();
$totalCost = $this->basketService->getTotalCostWithBasket($basket);
$totalCostWithTVA = $this->basketService->getTotalCostAndTVAWithBasket($basket);
$propositions = $this->productRepository->findPropositionsProduit();
return $this->render('cart/cart_index.html.twig', [
'basket' => $basket,
'error' => $error,
'totalCost' => $totalCost,
'totalCostWithTVA' => $totalCostWithTVA,
'propositions' => $propositions,
'parrain' => $this->orderService->getParrain(),
]);
}
#[Route('/add', name: 'app_cart_add', methods:"POST")]
public function add(Request $request): Response
{
try{
$productId = $request->get('product_id', '-1');
$quantity = $request->get('quantity', 0);
$product = $this->productRepository->find($productId);
if($product == null) throw new \Exception("Le produit n'existe pas.");
$basketItem = new BasketItem($product, $quantity);
$this->basketService->add($basketItem);
$cartUrl = $this->generateUrl('app_cart_index');
$message = $quantity.' '.$product->getName(). ' '.$this->translator->trans('ajouté(s) au panier').'. <a href="'.$cartUrl.'">Cliquer ici pour voir le panier.</a>';
$this->addFlash('success', $message);
return $this->redirectToRoute('app_shop_index');
} catch(\Exception $ex){
if ($ex->getCode() === Product::EXCEPTION_CODE['INSUFFICIENT_STOCK']) {
$this->addFlash('danger', 'Stock insuffisant pour le produit “'. $product->getName().'“');
return $this->redirectToRoute('app_shop_index');
}
throw $ex;
}
}
#[Route('/update', name: 'app_cart_update', methods:"PUT")]
public function update(Request $request): JsonResponse
{
try{
$basketItems = json_decode( $request->getContent(), true);
for($i=0; $i<count($basketItems);$i++){
$productId = $basketItems[$i]['productId'];
$quantity = $basketItems[$i]['quantity'];
$product = $this->productRepository->find($productId);
$basketItem = new BasketItem($product, $quantity);
$this->basketService->update($productId, $basketItem);
}
$this->addFlash('success', $this->translator->trans('Votre panier a été mis à jour').'.');
return new JsonResponse(array('basket' => $this->basketService->getBasket()));
} catch(\Exception $ex){
return new JsonResponse(array('message' => $ex->getMessage()), 500);
}
}
#[Route('/updateOne', name: 'app_cart_update_one', methods:"PUT")]
public function updateOne(Request $request): JsonResponse
{
try{
$data = json_decode( $request->getContent(), true);
$productId = $data['productId'];
$quantity = $data['quantity'];
$product = $this->productRepository->find($productId);
$basketItem = new BasketItem($product, $quantity);
$this->basketService->update($productId, $basketItem);
$this->addFlash('success', $quantity.' '.$product->getName().' '.$this->translator->trans('au panier').'.');
return new JsonResponse(array('basket' => $this->basketService->getBasket()));
} catch(\Exception $ex){
return new JsonResponse(array('message' => $ex->getMessage()), 500);
}
}
#[Route('/remove/{productId}', name: 'app_cart_remove', methods:"DELETE")]
public function remove(Request $request, $productId): JsonResponse
{
try{
$product = $this->productRepository->find($productId);
$this->basketService->remove($productId);
$this->addFlash('success', $product->getName().' '.$this->translator->trans('a été supprimé du panier').'.');
return new JsonResponse(array('basket' => $this->basketService->getBasket()));
} catch(\Exception $ex){
return new JsonResponse(array('message' => $ex->getMessage()), 500);
}
}
}