src/Controller/App/ProductController.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Controller\App;
  3. use App\Entity\App\Product;
  4. use App\Entity\Security\Order;
  5. use App\Entity\Security\Status;
  6. use App\Repository\App\ProductRepository;
  7. use App\Service\CartService;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. use Symfony\Component\Mime\Email;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. class ProductController extends AbstractController
  18. {
  19.     public function __construct(
  20.         private readonly ProductRepository $productRepository,
  21.         private readonly CartService $cartService,
  22.     ) {
  23.     }
  24.     #[Route('/products'name'product_index')]
  25.     public function index(Request $request): Response
  26.     {
  27.         $products $this->productRepository->findAllOrderedByDate($request);
  28.         return $this->render('app/product/index.html.twig', [
  29.             'products' => $products,
  30.         ]);
  31.     }
  32.     #[Route('/product/add/{id}'name'product_add_cart')]
  33.     public function addToCart(int $idRequest $request): RedirectResponse
  34.     {
  35.         $this->cartService->addToCart($id$request);
  36.         $this->addFlash('success''Le produit a été ajouté à votre panier');
  37.         return $this->redirectToRoute('product_index');
  38.     }
  39.     #[Route('/product/change-quantity/{productId}'methods: ['GET'], name'product_change_quantity')]
  40.     public function changeQuantity(Request $request$productId): JsonResponse
  41.     {
  42.         $quantity $request->query->get('quantity');
  43.         $total $this->cartService->updateQuantityForProduct($productId$quantity);
  44.         return new JsonResponse($total);
  45.     }
  46.     #[Route('/cart'name'product_cart')]
  47.     public function seeCart(): Response
  48.     {
  49.         $cartProducts $this->cartService->getProductCart();
  50.         $total 0;
  51.         $products = [];
  52.         foreach ($cartProducts as $cartProduct) {
  53.             $product $this->productRepository->findOneById($cartProduct['id']);
  54.             $total += $product->getPrice() * $cartProduct['quantity'];
  55.             $products[] = [
  56.                 'item' => $product,
  57.                 'quantity' => $cartProduct['quantity'],
  58.                 'customization' => $cartProduct['customization'],
  59.             ];
  60.         }
  61.         $totalProducts \count($cartProducts);
  62.         return $this->render('app/product/cart.html.twig', [
  63.             'products' => $products,
  64.             'total' => $total,
  65.             'totalProducts' => $totalProducts,
  66.         ]);
  67.     }
  68.     #[Route('/product/remove-all/{id}'name'product_remove_all_cart')]
  69.     public function removeAllFromCart(int $id): RedirectResponse
  70.     {
  71.         $this->cartService->removeAllProductsForId($id);
  72.         $this->addFlash('success''Le produit a été supprimé de votre panier');
  73.         return $this->redirectToRoute('product_cart');
  74.     }
  75.     #[Route('/pay/cart/'name'product_cart_pay')]
  76.     public function payCart(
  77.         EntityManagerInterface $entityManager,
  78.     ): RedirectResponse {
  79.         $order = new Order();
  80.         $products $this->cartService->getProductCart();
  81.         $price 0;
  82.         foreach ($products as $productId => $productDetails) {
  83.             $product $this->productRepository->findOneById($productId);
  84.             if ($product instanceof Product) {
  85.                 $order->addProduct($product);
  86.                 $price += $product->getPrice() * $productDetails['quantity'];
  87.             }
  88.         }
  89.         $order->setUser($this->getUser());
  90.         $order->setOrderAt(new \DateTimeImmutable());
  91.         $order->setPrice($price);
  92.         $order->setStatus(Status::IN_PROGRESS->value);
  93.         $entityManager->persist($order);
  94.         $entityManager->flush();
  95.         $this->cartService->removeCart();
  96.         $this->addFlash('success''La commande a été validée avec succès !');
  97.         return $this->redirectToRoute('product_index');
  98.     }
  99. }