src/Controller/Front/Api/Cart/CartController.php line 89

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Front\Api\Cart;
  4. use App\Application\Service\Cart\CartService;
  5. use App\Application\Service\Session\SessionService;
  6. use App\Entity\System\Customer;
  7. use App\Service\Cart\CartService as NewCartService;
  8. use App\Factory\Front\Api\Cart\CartFactory;
  9. use App\Manager\System\CartProductManager;
  10. use Doctrine\ORM\NonUniqueResultException;
  11. use Doctrine\ORM\NoResultException;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  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\Component\Serializer\Encoder\JsonEncoder;
  19. use Symfony\Component\Serializer\SerializerInterface;
  20. use function Sentry\captureException;
  21. class CartController extends AbstractController
  22. {
  23. public function __construct(
  24. private readonly CartProductManager $cartProductManager,
  25. private readonly CartFactory $cartFactory,
  26. private readonly SessionService $sessionService,
  27. private readonly NewCartService $newCartService,
  28. private readonly SerializerInterface $serializer,
  29. private readonly CartService $cartService,
  30. ) {
  31. }
  32. /**
  33. * @Route("/front/api/cart/products-count", name="front_api_cart_products_count", methods={"GET"})
  34. */
  35. public function getProductsCount(): Response
  36. {
  37. $cartId = (int)$this->sessionService->get('id_cart');
  38. try {
  39. $cartProductsCount = $cartId === 0 ? 0 : $this->cartProductManager->getNumberProductsInCart($cartId);
  40. } catch (NoResultException|NonUniqueResultException $exception) {
  41. $cartProductsCount = 0;
  42. }
  43. return new Response(
  44. \json_encode(['cartProductsCount' => $cartProductsCount]),
  45. Response::HTTP_OK,
  46. ['content-type' => 'application/json']
  47. );
  48. }
  49. /**
  50. * @IsGranted("IS_AUTHENTICATED_FULLY")
  51. *
  52. * @Route("/front/api/cart/details", name="front_api_cart_details", methods={"GET"})
  53. */
  54. public function getCartDetails(): JsonResponse
  55. {
  56. try {
  57. /** @var Customer $customer */
  58. $customer = $this->getUser();
  59. $cartId = (int)$this->sessionService->get('id_cart');
  60. $languageIsoCode = $this->sessionService->getLocale();
  61. $summaryCart = $this->cartService->getSummaryCartRequest($cartId, $languageIsoCode, $customer);
  62. $cartProductsResponse = $this->cartFactory->createCartDetails($summaryCart, $customer, $languageIsoCode);
  63. return new JsonResponse($this->serializer->serialize($cartProductsResponse, JsonEncoder::FORMAT), Response::HTTP_OK, [], true);
  64. } catch (\LogicException $LogicException) {
  65. return new JsonResponse(null, Response::HTTP_NO_CONTENT);
  66. } catch (\Throwable $exception) {
  67. captureException($exception);
  68. return new JsonResponse($exception->getMessage(), Response::HTTP_BAD_REQUEST);
  69. }
  70. }
  71. /**
  72. * @IsGranted("IS_AUTHENTICATED_FULLY")
  73. *
  74. * @Route("/front/api/cart-services/details", name="front_api_cart_services_details", methods={"GET"})
  75. */
  76. public function getCartServicesDetails(): JsonResponse
  77. {
  78. try {
  79. /** @var Customer $customer */
  80. $customer = $this->getUser();
  81. $cartId = (int)$this->sessionService->get('id_cart_services');
  82. $languageIsoCode = $this->sessionService->getLocale();
  83. $summaryCart = $this->cartService->getSummaryCartRequest($cartId, $languageIsoCode, $customer);
  84. $cartProductsResponse = $this->cartFactory->createCartServiceDetails($summaryCart, $languageIsoCode, $customer);
  85. return new JsonResponse($this->serializer->serialize($cartProductsResponse, JsonEncoder::FORMAT), Response::HTTP_OK, [], true);
  86. } catch (\LogicException $LogicException) {
  87. return new JsonResponse(null, Response::HTTP_NO_CONTENT);
  88. } catch (\Throwable $exception) {
  89. captureException($exception);
  90. return new JsonResponse($exception->getMessage(), Response::HTTP_BAD_REQUEST);
  91. }
  92. }
  93. /**
  94. * @IsGranted("IS_AUTHENTICATED_FULLY")
  95. *
  96. * @Route("/front/api/cart/update", name="front_api_cart_update", methods={"POST"})
  97. */
  98. public function updateCart(Request $request): Response
  99. {
  100. try {
  101. $cartRequest = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
  102. $cartId = (int)$this->sessionService->get('id_cart');
  103. $cartProducts = $this->newCartService->updateCart($cartId, $cartRequest);
  104. return new JsonResponse($this->serializer->serialize($cartProducts, JsonEncoder::FORMAT), Response::HTTP_OK, [], true);
  105. } catch (\Throwable $exception) {
  106. captureException($exception);
  107. return new JsonResponse('', Response::HTTP_BAD_REQUEST);
  108. }
  109. }
  110. /**
  111. * @IsGranted("IS_AUTHENTICATED_FULLY")
  112. *
  113. * @Route("/front/api/cart/shipping-costs", name="front_api_cart_shipping_costs", methods={"GET"})
  114. */
  115. public function shippingCostsCart(Request $request): Response
  116. {
  117. try {
  118. $isoCode = $request->get('isoCode');
  119. $postalCode = $request->get('postalCode');
  120. if (!$isoCode || !$postalCode) {
  121. return new Response('IsoCode and PostalCode are required', Response::HTTP_BAD_REQUEST);
  122. }
  123. $cartId = (int)$this->sessionService->get('id_cart');
  124. /** @var Customer $customer */
  125. $customer = $this->getUser();
  126. $languageIsoCode = $this->sessionService->getLocale();
  127. $summaryCart = $this->cartService->getSummaryCartRequest($cartId, $languageIsoCode, $customer);
  128. $shippingCostsResponse = $this->cartFactory->createShippingCosts(
  129. $summaryCart,
  130. $isoCode,
  131. $postalCode,
  132. $languageIsoCode
  133. );
  134. return new JsonResponse($this->serializer->serialize($shippingCostsResponse->carrierDataResponse, JsonEncoder::FORMAT), Response::HTTP_OK, [], true);
  135. } catch (\LogicException $LogicException) {
  136. return new JsonResponse($LogicException->getMessage(), Response::HTTP_NO_CONTENT);
  137. } catch (\Throwable $exception) {
  138. captureException($exception);
  139. return new JsonResponse($exception->getMessage(), Response::HTTP_BAD_REQUEST);
  140. }
  141. }
  142. }