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

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\Cart\SummaryCartService;
  6. use App\Application\Service\Session\SessionService;
  7. use App\Entity\System\Customer;
  8. use App\Factory\Front\Api\Cart\CartDetailsResponseFactory;
  9. use App\Factory\Front\Api\Cart\CartServiceDetailsResponseFactory;
  10. use App\Manager\System\CartManager;
  11. use App\Service\Cart\CartService as NewCartService;
  12. use App\Factory\Front\Api\Cart\ShippingCostResponseFactory;
  13. use App\Manager\System\CartProductManager;
  14. use Doctrine\ORM\NonUniqueResultException;
  15. use Doctrine\ORM\NoResultException;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\HttpFoundation\JsonResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  23. use Symfony\Component\Serializer\SerializerInterface;
  24. use function Sentry\captureException;
  25. class CartController extends AbstractController
  26. {
  27. public function __construct(
  28. private readonly CartProductManager $cartProductManager,
  29. private readonly ShippingCostResponseFactory $shippingCostResponseFactory,
  30. private readonly SessionService $sessionService,
  31. private readonly NewCartService $newCartService,
  32. private readonly SerializerInterface $serializer,
  33. private readonly CartService $cartService,
  34. private readonly SummaryCartService $summaryCartService,
  35. private readonly CartManager $cartManager,
  36. private readonly CartServiceDetailsResponseFactory $cartServiceDetailsResponseFactory,
  37. private readonly CartDetailsResponseFactory $cartDetailsResponseFactory,
  38. ) {
  39. }
  40. /**
  41. * @Route("/front/api/cart/products-count", name="front_api_cart_products_count", methods={"GET"})
  42. */
  43. public function getProductsCount(): Response
  44. {
  45. $cartId = (int)$this->sessionService->get('id_cart');
  46. try {
  47. $cartProductsCount = $cartId === 0 ? 0 : $this->cartProductManager->getNumberProductsInCart($cartId);
  48. } catch (NoResultException|NonUniqueResultException $exception) {
  49. $cartProductsCount = 0;
  50. }
  51. return new Response(
  52. \json_encode(['cartProductsCount' => $cartProductsCount]),
  53. Response::HTTP_OK,
  54. ['content-type' => 'application/json']
  55. );
  56. }
  57. /**
  58. * @IsGranted("IS_AUTHENTICATED_FULLY")
  59. *
  60. * @Route("/front/api/cart/details", name="front_api_cart_details", methods={"GET"})
  61. */
  62. public function getCartDetails(): JsonResponse
  63. {
  64. try {
  65. /** @var Customer $customer */
  66. $customer = $this->getUser();
  67. if (empty($this->sessionService->get('id_cart'))) {
  68. return new JsonResponse(null, Response::HTTP_NO_CONTENT);
  69. }
  70. $cartId = (int)$this->sessionService->get('id_cart');
  71. $cart = $this->cartManager->findOneById($cartId);
  72. if (!$cart) {
  73. return new JsonResponse('cart not found', Response::HTTP_NO_CONTENT);
  74. }
  75. $languageIsoCode = $this->sessionService->getLocale();
  76. $summaryCart = $this->summaryCartService->getFromCustomerAndCart($customer, $cart, $languageIsoCode);
  77. $cartProductsResponse = $this->cartDetailsResponseFactory->createBySummaryCart($summaryCart, $customer, $languageIsoCode);
  78. return new JsonResponse($this->serializer->serialize($cartProductsResponse, JsonEncoder::FORMAT), Response::HTTP_OK, [], true);
  79. } catch (\LogicException $LogicException) {
  80. return new JsonResponse(null, Response::HTTP_NO_CONTENT);
  81. } catch (\Throwable $exception) {
  82. captureException($exception);
  83. return new JsonResponse($exception->getMessage(), Response::HTTP_BAD_REQUEST);
  84. }
  85. }
  86. /**
  87. * @IsGranted("IS_AUTHENTICATED_FULLY")
  88. *
  89. * @Route("/front/api/cart-services/details", name="front_api_cart_services_details", methods={"GET"})
  90. */
  91. public function getCartServicesDetails(): JsonResponse
  92. {
  93. try {
  94. /** @var Customer $customer */
  95. $customer = $this->getUser();
  96. $cartId = (int)$this->sessionService->get('id_cart_services');
  97. if (empty($cartId)) {
  98. throw new \RuntimeException('CartId not found');
  99. }
  100. $cart = $this->cartManager->findOneById($cartId);
  101. if (!$cart || $cart->getCustomer()->getId() !== $customer->getId()) {
  102. throw new \RuntimeException('cart not found');
  103. }
  104. $languageIsoCode = $this->sessionService->getLocale();
  105. $summaryCart = $this->summaryCartService->getFromCustomerAndCart($customer, $cart, $languageIsoCode);
  106. $cartProductsResponse = $this->cartServiceDetailsResponseFactory->createFromSummaryCart($summaryCart, $languageIsoCode, $customer);
  107. return new JsonResponse($this->serializer->serialize($cartProductsResponse, JsonEncoder::FORMAT), Response::HTTP_OK, [], true);
  108. } catch (\LogicException $LogicException) {
  109. return new JsonResponse(null, Response::HTTP_NO_CONTENT);
  110. } catch (\Throwable $exception) {
  111. captureException($exception);
  112. return new JsonResponse($exception->getMessage(), Response::HTTP_BAD_REQUEST);
  113. }
  114. }
  115. /**
  116. * @IsGranted("IS_AUTHENTICATED_FULLY")
  117. *
  118. * @Route("/front/api/cart/update", name="front_api_cart_update", methods={"POST"})
  119. */
  120. public function updateCart(Request $request): Response
  121. {
  122. try {
  123. $cartRequest = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
  124. $cartId = (int)$this->sessionService->get('id_cart');
  125. $cartProducts = $this->newCartService->updateCart($cartId, $cartRequest);
  126. return new JsonResponse($this->serializer->serialize($cartProducts, JsonEncoder::FORMAT), Response::HTTP_OK, [], true);
  127. } catch (\Throwable $exception) {
  128. captureException($exception);
  129. return new JsonResponse('', Response::HTTP_BAD_REQUEST);
  130. }
  131. }
  132. /**
  133. * @IsGranted("IS_AUTHENTICATED_FULLY")
  134. *
  135. * @Route("/front/api/cart/shipping-costs", name="front_api_cart_shipping_costs", methods={"GET"})
  136. */
  137. public function shippingCostsCart(Request $request): Response
  138. {
  139. try {
  140. $isoCode = $request->get('isoCode');
  141. $postalCode = $request->get('postalCode');
  142. if (!$isoCode || !$postalCode) {
  143. return new Response('IsoCode and PostalCode are required', Response::HTTP_BAD_REQUEST);
  144. }
  145. $cartId = (int)$this->sessionService->get('id_cart');
  146. /** @var Customer $customer */
  147. $customer = $this->getUser();
  148. $languageIsoCode = $this->sessionService->getLocale();
  149. $summaryCart = $this->cartService->getSummaryCartRequest(
  150. $cartId,
  151. $languageIsoCode,
  152. $customer,
  153. $isoCode,
  154. $postalCode
  155. );
  156. $shippingCostsResponse = $this->shippingCostResponseFactory->createShippingCosts(
  157. $summaryCart,
  158. $isoCode,
  159. $postalCode,
  160. $languageIsoCode
  161. );
  162. return new JsonResponse($this->serializer->serialize($shippingCostsResponse->carrierDataResponse, JsonEncoder::FORMAT), Response::HTTP_OK, [], true);
  163. } catch (\LogicException $LogicException) {
  164. return new JsonResponse($LogicException->getMessage(), Response::HTTP_NO_CONTENT);
  165. } catch (\Throwable $exception) {
  166. captureException($exception);
  167. return new JsonResponse($exception->getMessage(), Response::HTTP_BAD_REQUEST);
  168. }
  169. }
  170. }