src/Controller/App/BuyController.php line 31

  1. <?php
  2. namespace App\Controller\App;
  3. use App\Entity\Order;
  4. use App\Entity\Product;
  5. use App\Entity\enums\OrderStatus;
  6. use App\Repository\ProductRepository;
  7. use App\Repository\CouponRepository;
  8. use App\Service\NotificationService;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. #[Route('/app/buy')]
  17. #[IsGranted('IS_AUTHENTICATED_FULLY')]
  18. class BuyController extends AbstractController
  19. {
  20.     private NotificationService $notificationService;
  21.     public function __construct(private TranslatorInterface $translatorNotificationService $notificationService)
  22.     {
  23.         $this->notificationService $notificationService;
  24.     }
  25.     #[Route('/{product}'name'app_buy_index')]
  26.     public function index(Product $productCouponRepository $couponRepoEntityManagerInterface $entityManager): Response
  27.     {
  28.         $order = new Order();
  29.         $order->setProduct($product)
  30.             ->setName('Pack ' $product->getName() . ' - ' $product->getDuration() . ' Jours')
  31.             ->setCustomer($this->getUser())
  32.             ->setPrice($product->getPrice())
  33.             ->setQuantity(1)
  34.             ->setTotal($product->getPrice());
  35.         $entityManager->persist($order);
  36.         $entityManager->flush();
  37.         return $this->redirectToRoute('app_buy_order', ['order' => $order->getId()], Response::HTTP_SEE_OTHER);
  38.     }
  39.     #[Route('/order/{order}'name'app_buy_order')]
  40.     public function order(Request $requestOrder $orderCouponRepository $couponRepoEntityManagerInterface $entityManager): Response
  41.     {
  42.         if ($order->getStatus() === OrderStatus::CONFIRMED) {
  43.             return $this->redirectToRoute('app_order_pay', ['id' => $order->getId()], Response::HTTP_SEE_OTHER);
  44.         }
  45.         $coupon $request->query->get('coupon');
  46.         $quantity 12;
  47.         $OrderCoupon $couponRepo->getValid($coupon);
  48.         $order->setQuantity($quantity)->setPrice($order->getProduct()->getPrice());
  49.         $order->setTotal($order->getProduct()->getPrice() * $quantity);
  50.         if ($OrderCoupon) {
  51.             if ($OrderCoupon->getUsageCount() < $OrderCoupon->getUsageLimit()) {
  52.                 $order->setCoupon($OrderCoupon);
  53.                 $this->addFlash('success'$this->translator->trans('Coupon accepté !!'));
  54.             } else {
  55.                 $this->addFlash('error'$this->translator->trans('Coupon expiré !!'));
  56.             }
  57.         } else {
  58.             if ($coupon)
  59.                 $this->addFlash('error'$this->translator->trans('Coupon non valid !!'));
  60.         }
  61.         $entityManager->persist($order);
  62.         $entityManager->flush();
  63.         return $this->render('app/buy/index.html.twig', [
  64.             'controller_name' => 'BuyController',
  65.             'product' => $order->getProduct(),
  66.             'order' => $order,
  67.         ]);
  68.     }
  69.     #[Route('/current/license'name'app_buy_license')]
  70.     public function license(EntityManagerInterface $managerProductRepository $prodRepo): Response
  71.     {
  72.         return $this->render('app/buy/license.html.twig', [
  73.             'controller_name' => 'BuyController',
  74.             'products' => $prodRepo->findBy([], ['price' => 'ASC'])
  75.         ]);
  76.     }
  77. }