src/Controller/Frontend/CartController.php line 145

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Business\Cart\Calculator\CartTotalCalculatorInterface;
  4. use App\Business\Cart\CartConstants;
  5. use App\Business\Cart\Mapper\CartAreaResponseMapperInterface;
  6. use App\Business\Cart\Session\CartSessionHandlerInterface;
  7. use App\Business\Cart\Transfer\CartAreaTransfer;
  8. use App\Entity\CustomerAdvertisingArea;
  9. use App\Form\Frontend\CustomerAdvertisingAreaSelectionType;
  10. use App\Service\Utils\AreaContributionPriceCalculatorInterface;
  11. use App\Service\Utils\ToStringService;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. class CartController extends AbstractController
  20. {
  21.     private AreaContributionPriceCalculatorInterface $contributionPriceCalculator;
  22.     private CartSessionHandlerInterface $cartSessionHandler;
  23.     private EntityManagerInterface $entityManager;
  24.     private CartAreaResponseMapperInterface $cartAreaResponseMapper;
  25.     private CartTotalCalculatorInterface $cartTotalCalculator;
  26.     /**
  27.      * @param EntityManagerInterface $entityManager
  28.      * @param CartSessionHandlerInterface $cartSessionHandler
  29.      * @param AreaContributionPriceCalculatorInterface $contributionPriceCalculator
  30.      * @param CartAreaResponseMapperInterface $cartAreaResponseMapper
  31.      * @param CartTotalCalculatorInterface $cartTotalCalculator
  32.      */
  33.     public function __construct(
  34.         EntityManagerInterface $entityManager,
  35.         CartSessionHandlerInterface $cartSessionHandler,
  36.         AreaContributionPriceCalculatorInterface $contributionPriceCalculator,
  37.         CartAreaResponseMapperInterface $cartAreaResponseMapper,
  38.         CartTotalCalculatorInterface $cartTotalCalculator
  39.     ) {
  40.         $this->contributionPriceCalculator $contributionPriceCalculator;
  41.         $this->cartSessionHandler $cartSessionHandler;
  42.         $this->entityManager $entityManager;
  43.         $this->cartAreaResponseMapper $cartAreaResponseMapper;
  44.         $this->cartTotalCalculator $cartTotalCalculator;
  45.     }
  46.     /**
  47.      * @return Response
  48.      */
  49.     #[Route('/cart'name'app_frontend_cart')]
  50.     public function cart(): Response
  51.     {
  52.         $areas = [];
  53.         /** @var CartAreaTransfer $item */
  54.         foreach ($this->cartSessionHandler->getItems() as $item) {
  55.             $area $this->entityManager->getRepository(CustomerAdvertisingArea::class)
  56.                 ->findOneBy(['uuid' => $item->getUuidCustomerAdvertisingArea()]);
  57.             $areas[] = $this->cartAreaResponseMapper->mapFromEntity($area$item->isWithinSetupPrice());
  58.         }
  59.         return $this->render('frontend/cart/cart.html.twig', [
  60.             'areas' => $areas,
  61.             'cartTotal' => ToStringService::toString($this->cartTotalCalculator->calculate()),
  62.         ]);
  63.     }
  64.     /**
  65.      * @param TranslatorInterface $translator
  66.      * @param Request $request
  67.      * @param string $uuidArea
  68.      *
  69.      * @return Response
  70.      */
  71.     #[Route('/cart/modal/add/{uuidArea}'name'app_frontend_cart_modal_add')]
  72.     public function addToCartModal(
  73.         TranslatorInterface $translator,
  74.         Request $request,
  75.         string $uuidArea
  76.     ): Response {
  77.         $customerAdvertisingArea $this->entityManager
  78.             ->getRepository(CustomerAdvertisingArea::class)
  79.             ->findOneBy(['uuid' => $uuidArea]);
  80.         $form $this->createForm(CustomerAdvertisingAreaSelectionType::class, $customerAdvertisingArea)
  81.             ->handleRequest($request);
  82.         /* @todo handle form submit */
  83.         $withinSetup $form->get(CustomerAdvertisingAreaSelectionType::FIELD_WITHIN_SETUP_PRICE)->getData();
  84.         $pricePerMonth $this->contributionPriceCalculator->calculateByCustomerConfiguration($customerAdvertisingArea);
  85.         $settings $customerAdvertisingArea->getSettings();
  86.         $params = [
  87.             'uuid' => $customerAdvertisingArea->getUuid(),
  88.             'description' => $customerAdvertisingArea->getSettings()->getDescription(),
  89.             'unitWidth' => $customerAdvertisingArea->getSettings()->getUnitWidth(),
  90.             'unitHeight' => $customerAdvertisingArea->getSettings()->getUnitHeight(),
  91.             'priceUnitYear' => sprintf(
  92.                 '%s %s',
  93.                 number_format($pricePerMonth 122',''.'),
  94.                 $translator->trans('app.currency_sign')
  95.             ),
  96.             'setupPriceUnit' => sprintf(
  97.                 '%s %s',
  98.                 number_format($settings->getSetupPriceUnit(), 2',''.'),
  99.                 $translator->trans('app.currency_sign')
  100.             ),
  101.             'minDuration' => $settings->getMinDuration(),
  102.             'priceDuration' => sprintf(
  103.                 '%s %s',
  104.                 number_format($pricePerMonth $settings->getMinDuration(), 2',''.'),
  105.                 $translator->trans('app.currency_sign')
  106.             ),
  107.         ];
  108.         $total = ($withinSetup)
  109.             ? $pricePerMonth $settings->getMinDuration() + $settings->getSetupPriceUnit()
  110.             : $pricePerMonth $settings->getMinDuration();
  111.         $params array_merge($params, [
  112.             'total' => sprintf(
  113.                 '%s %s',
  114.                 number_format($total2',''.'),
  115.                 $translator->trans('app.currency_sign')
  116.             ),
  117.         ]);
  118.         return $this->render('frontend/cart/modal/add-to-cart__modal-body.twig'array_merge($params, [
  119.             'form' => $form->createView(),
  120.         ]));
  121.     }
  122.     /**
  123.      * @param Request $request
  124.      *
  125.      * @return Response
  126.      */
  127.     #[Route('/cart/add'name'app_frontend_cart_add_item')]
  128.     public function addToCart(Request $request): Response
  129.     {
  130.         $uuidCustomerAdvertisingArea $request->get(CartConstants::UUID_AREA);
  131.         $withinSetupPrice $request->get(CartConstants::WITHIN_SETUP) === 'true';
  132.         $cartAreaTransfer = (new CartAreaTransfer())
  133.             ->setUuidCustomerAdvertisingArea($uuidCustomerAdvertisingArea)
  134.             ->setWithinSetupPrice($withinSetupPrice);
  135.         $this->cartSessionHandler->addToCart($cartAreaTransfer);
  136.         return new JsonResponse(array_merge([
  137.             'cartTotal' => ToStringService::toString($this->cartTotalCalculator->calculate())
  138.         ], $this->cartAreaResponseMapper->mapFromTransfer($cartAreaTransfer)));
  139.     }
  140.     /**
  141.      * @param string $uuidCustomerAdvertisingArea
  142.      *
  143.      * @return Response
  144.      */
  145.     #[Route('/cart/remove/{uuidCustomerAdvertisingArea}'name'app_frontend_cart_remove_item')]
  146.     public function removeFromCart(string $uuidCustomerAdvertisingArea): Response
  147.     {
  148.         $this->cartSessionHandler->removeFromCart($uuidCustomerAdvertisingArea);
  149.         return new JsonResponse([
  150.             'uuid' => $uuidCustomerAdvertisingArea,
  151.             'cartTotal' => ToStringService::toString($this->cartTotalCalculator->calculate())
  152.         ]);
  153.     }
  154.     /**
  155.      * @param EntityManagerInterface $entityManager
  156.      *
  157.      * @return Response
  158.      */
  159.     #[Route('/cart/shopping-cart'name'app_frontend_cart_shopping_cart')]
  160.     public function shoppingCart(EntityManagerInterface $entityManager): Response
  161.     {
  162.         $areas = [];
  163.         /** @var CartAreaTransfer $item */
  164.         foreach ($this->cartSessionHandler->getItems() as $item) {
  165.             $area $entityManager->getRepository(CustomerAdvertisingArea::class)
  166.                 ->findOneBy(['uuid' => $item->getUuidCustomerAdvertisingArea()]);
  167.             $areas[] = $this->cartAreaResponseMapper->mapFromEntity($area$item->isWithinSetupPrice());
  168.         }
  169.         return new JsonResponse([
  170.             'cartTotal' => ToStringService::toString($this->cartTotalCalculator->calculate()),
  171.             'areas' => $areas
  172.         ]);
  173.     }
  174.     /**
  175.      * @param Request $request
  176.      *
  177.      * @return Response
  178.      */
  179.     #[Route('/cart/toggle-area-production-and-setup'name'app_frontend_cart_toogle-production-and-setup')]
  180.     public function toggleAreaProductionAndSetup(Request $request): Response
  181.     {
  182.         $areaTransfer $this->cartSessionHandler->getItem($request->get(CartConstants::UUID_AREA));
  183.         if ($areaTransfer === null) {
  184.             return new JsonResponse(false);
  185.         }
  186.         $this->cartSessionHandler->toggleAreaProductionAndSetup($areaTransfer->getUuidCustomerAdvertisingArea());
  187.         $areaTransfer $this->cartSessionHandler->getItem($request->get(CartConstants::UUID_AREA));
  188.         return new JsonResponse([
  189.             'cartTotal' => ToStringService::toString($this->cartTotalCalculator->calculate()),
  190.             'area' => $this->cartAreaResponseMapper->mapFromTransfer($areaTransfer),
  191.         ]);
  192.     }
  193.     /**
  194.      * @return Response
  195.      */
  196.     #[Route('/cart/refresh'name'app_frontend_cart_refresh-cart-total-and-item-count')]
  197.     public function refreshCartTotalAndItemCount(): Response
  198.     {
  199.         return new JsonResponse([
  200.             'cartTotal' => ToStringService::toString($this->cartTotalCalculator->calculate()),
  201.             'areaCount' => count($this->cartSessionHandler->getItems()),
  202.         ]);
  203.     }
  204. }