src/Controller/App/BuyController.php line 31
<?php
namespace App\Controller\App;
use App\Entity\Order;
use App\Entity\Product;
use App\Entity\enums\OrderStatus;
use App\Repository\ProductRepository;
use App\Repository\CouponRepository;
use App\Service\NotificationService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
#[Route('/app/buy')]
#[IsGranted('IS_AUTHENTICATED_FULLY')]
class BuyController extends AbstractController
{
private NotificationService $notificationService;
public function __construct(private TranslatorInterface $translator, NotificationService $notificationService)
{
$this->notificationService = $notificationService;
}
#[Route('/{product}', name: 'app_buy_index')]
public function index(Product $product, CouponRepository $couponRepo, EntityManagerInterface $entityManager): Response
{
$order = new Order();
$order->setProduct($product)
->setName('Pack ' . $product->getName() . ' - ' . $product->getDuration() . ' Jours')
->setCustomer($this->getUser())
->setPrice($product->getPrice())
->setQuantity(1)
->setTotal($product->getPrice());
$entityManager->persist($order);
$entityManager->flush();
return $this->redirectToRoute('app_buy_order', ['order' => $order->getId()], Response::HTTP_SEE_OTHER);
}
#[Route('/order/{order}', name: 'app_buy_order')]
public function order(Request $request, Order $order, CouponRepository $couponRepo, EntityManagerInterface $entityManager): Response
{
if ($order->getStatus() === OrderStatus::CONFIRMED) {
return $this->redirectToRoute('app_order_pay', ['id' => $order->getId()], Response::HTTP_SEE_OTHER);
}
$coupon = $request->query->get('coupon');
$quantity = 12;
$OrderCoupon = $couponRepo->getValid($coupon);
$order->setQuantity($quantity)->setPrice($order->getProduct()->getPrice());
$order->setTotal($order->getProduct()->getPrice() * $quantity);
if ($OrderCoupon) {
if ($OrderCoupon->getUsageCount() < $OrderCoupon->getUsageLimit()) {
$order->setCoupon($OrderCoupon);
$this->addFlash('success', $this->translator->trans('Coupon accepté !!'));
} else {
$this->addFlash('error', $this->translator->trans('Coupon expiré !!'));
}
} else {
if ($coupon)
$this->addFlash('error', $this->translator->trans('Coupon non valid !!'));
}
$entityManager->persist($order);
$entityManager->flush();
return $this->render('app/buy/index.html.twig', [
'controller_name' => 'BuyController',
'product' => $order->getProduct(),
'order' => $order,
]);
}
#[Route('/current/license', name: 'app_buy_license')]
public function license(EntityManagerInterface $manager, ProductRepository $prodRepo): Response
{
return $this->render('app/buy/license.html.twig', [
'controller_name' => 'BuyController',
'products' => $prodRepo->findBy([], ['price' => 'ASC'])
]);
}
}