<?php
namespace App\Controller\App;
use App\Entity\App\Product;
use App\Entity\Security\Order;
use App\Entity\Security\Status;
use App\Repository\App\ProductRepository;
use App\Service\CartService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;
class ProductController extends AbstractController
{
public function __construct(
private readonly ProductRepository $productRepository,
private readonly CartService $cartService,
) {
}
#[Route('/products', name: 'product_index')]
public function index(Request $request): Response
{
$products = $this->productRepository->findAllOrderedByDate($request);
return $this->render('app/product/index.html.twig', [
'products' => $products,
]);
}
#[Route('/product/add/{id}', name: 'product_add_cart')]
public function addToCart(int $id, Request $request): RedirectResponse
{
$this->cartService->addToCart($id, $request);
$this->addFlash('success', 'Le produit a été ajouté à votre panier');
return $this->redirectToRoute('product_index');
}
#[Route('/product/change-quantity/{productId}', methods: ['GET'], name: 'product_change_quantity')]
public function changeQuantity(Request $request, $productId): JsonResponse
{
$quantity = $request->query->get('quantity');
$total = $this->cartService->updateQuantityForProduct($productId, $quantity);
return new JsonResponse($total);
}
#[Route('/cart', name: 'product_cart')]
public function seeCart(): Response
{
$cartProducts = $this->cartService->getProductCart();
$total = 0;
$products = [];
foreach ($cartProducts as $cartProduct) {
$product = $this->productRepository->findOneById($cartProduct['id']);
$total += $product->getPrice() * $cartProduct['quantity'];
$products[] = [
'item' => $product,
'quantity' => $cartProduct['quantity'],
'customization' => $cartProduct['customization'],
];
}
$totalProducts = \count($cartProducts);
return $this->render('app/product/cart.html.twig', [
'products' => $products,
'total' => $total,
'totalProducts' => $totalProducts,
]);
}
#[Route('/product/remove-all/{id}', name: 'product_remove_all_cart')]
public function removeAllFromCart(int $id): RedirectResponse
{
$this->cartService->removeAllProductsForId($id);
$this->addFlash('success', 'Le produit a été supprimé de votre panier');
return $this->redirectToRoute('product_cart');
}
#[Route('/pay/cart/', name: 'product_cart_pay')]
public function payCart(
EntityManagerInterface $entityManager,
): RedirectResponse {
$order = new Order();
$products = $this->cartService->getProductCart();
$price = 0;
foreach ($products as $productId => $productDetails) {
$product = $this->productRepository->findOneById($productId);
if ($product instanceof Product) {
$order->addProduct($product);
$price += $product->getPrice() * $productDetails['quantity'];
}
}
$order->setUser($this->getUser());
$order->setOrderAt(new \DateTimeImmutable());
$order->setPrice($price);
$order->setStatus(Status::IN_PROGRESS->value);
$entityManager->persist($order);
$entityManager->flush();
$this->cartService->removeCart();
$this->addFlash('success', 'La commande a été validée avec succès !');
return $this->redirectToRoute('product_index');
}
}