src/Controller/Front/Cart/SubscriptionCartController.php line 161

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Front\Cart;
  4. use App\Application\Service\Helper\LogWriterService;
  5. use App\Application\Service\Session\SessionService;
  6. use App\Entity\System\Customer;
  7. use App\Entity\System\Taxonomy;
  8. use App\Manager\System\ProductManager;
  9. use App\Manager\System\SubscriptionManager;
  10. use App\Service\Cart\AddToCartService;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. /**
  16. * Class SubscriptionCartController
  17. *
  18. * @Route("/subscription/cart", name="subscription_cart_controller")
  19. */
  20. class SubscriptionCartController extends AbstractController
  21. {
  22. public function __construct(
  23. private readonly SubscriptionManager $subscriptionManager,
  24. private readonly AddToCartService $addToCartService,
  25. private readonly SessionService $sessionService,
  26. private readonly LogWriterService $logWriterService,
  27. private readonly ProductManager $productManager,
  28. ) {
  29. }
  30. /**
  31. * @param Request $request
  32. *
  33. * @Route("/add-pack", name="subscription_cart_add_pack", methods={"POST"})
  34. *
  35. * @return Response
  36. */
  37. public function subscriptionCartAddPack(Request $request): Response
  38. {
  39. if (!$request->get('id_subscription')) {
  40. return new Response('Subscription id is required', Response::HTTP_BAD_REQUEST);
  41. }
  42. $subscriptionId = (int)$request->get('id_subscription');
  43. $subscription = $this->subscriptionManager->findOneById($subscriptionId);
  44. if (!$subscription) {
  45. return new Response('Subscription not exists', Response::HTTP_BAD_REQUEST);
  46. }
  47. /** @var Customer|null $customer */
  48. $customer = $this->getUser();
  49. if (!$customer) {
  50. $this->logWriterService->logCartCreationError(__METHOD__.' - Could not recover customer from session');
  51. return new Response('could not load customer from session', Response::HTTP_UNAUTHORIZED);
  52. }
  53. try {
  54. $this->addToCartService->addSubscriptionToServicesCart($subscription, $customer);
  55. } catch (\Throwable $exception) {
  56. \Sentry\captureException($exception);
  57. $this->logWriterService->logCartCreationError(__METHOD__.' - Exception: '.\json_encode($exception));
  58. return new Response($exception->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
  59. }
  60. return new Response();
  61. }
  62. /**
  63. * @param Request $request
  64. *
  65. * @Route("/add-services", name="subscription_cart_add_services", methods={"POST"})
  66. *
  67. * @return Response
  68. */
  69. public function subscriptionCartAddServices(Request $request): Response
  70. {
  71. /** @var Customer|null $customer */
  72. $customer = $this->getUser();
  73. if (!$customer) {
  74. $this->logWriterService->logCartCreationError(__METHOD__.' - Could not recover customer from session');
  75. return new Response('could not load customer from session', Response::HTTP_UNAUTHORIZED);
  76. }
  77. $servicesRequestContent = $request->get('services');
  78. $servicesRequest = \json_decode($servicesRequestContent, true);
  79. $customerId = $customer->getId();
  80. $cart = $this->addToCartService->handleCartServices($customer);
  81. if (empty($servicesRequest)) {
  82. $this->logWriterService->logCartCreationError(__METHOD__.' - empty request: '.\json_encode($servicesRequest));
  83. $this->addToCartService->removeAllServicesFromCart($cart);
  84. return new Response('empty request from client', Response::HTTP_UNAUTHORIZED);
  85. }
  86. $shopData = [];
  87. $ecommerceConnectorIds = [];
  88. $marketplaceConnectorIds = [];
  89. /** @var array{serviceId: int, themeId: int|null, catalogId: int|null} $serviceRequest */
  90. foreach ($servicesRequest as $serviceRequest) {
  91. $requestedServiceId = $serviceRequest['serviceId'];
  92. $requestedThemeId = $serviceRequest['themeId'];
  93. $requestedCatalogId = $serviceRequest['catalogId'];
  94. if ($requestedThemeId !== null && $requestedCatalogId !== null) {
  95. $shopData[] = ['shopId' => $requestedThemeId, 'topicId' => $requestedCatalogId];
  96. continue;
  97. }
  98. try {
  99. $productDefaultTaxonomy = $this->productManager->getDefaultTaxonomyByProductId($requestedServiceId);
  100. } catch (\Throwable $exception) {
  101. continue;
  102. }
  103. if ($productDefaultTaxonomy === Taxonomy::MARKETPLACES_TAXONOMY_ID) {
  104. $marketplaceConnectorIds[] = $requestedServiceId;
  105. }
  106. if ($productDefaultTaxonomy === Taxonomy::ECOMMERCE_TAXONOMY_ID) {
  107. $ecommerceConnectorIds[] = $requestedServiceId;
  108. }
  109. }
  110. try {
  111. $cart = $this->addToCartService->addMarketplaceConnectorsToServicesCart($cart, $marketplaceConnectorIds, $customerId);
  112. $cart = $this->addToCartService->addEcommerceConnectorsToServicesCart($cart, $ecommerceConnectorIds, $customerId);
  113. $this->addToCartService->addShopsToServicesCart($cart, $shopData, $customerId);
  114. } catch (\Throwable $exception) {
  115. \Sentry\captureException($exception);
  116. $this->logWriterService->logCartCreationError(__METHOD__.' - Exception: '.\json_encode($exception));
  117. return new Response($exception->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
  118. }
  119. return new Response();
  120. }
  121. /**
  122. * @param Request $request
  123. *
  124. * @Route("/add-payment-methods", name="subscription_cart_add_payment_methods", methods={"POST"})
  125. *
  126. * @return Response
  127. */
  128. public function subscriptionCartAddPaymentMethods(Request $request): Response
  129. {
  130. $paymentMethodData = json_decode($request->getContent(), true);
  131. if (!isset($paymentMethodData['pack_services_payment_method'])) {
  132. return new Response('Payment method are required', Response::HTTP_BAD_REQUEST);
  133. }
  134. $cartId = $this->sessionService->get('id_cart_services');
  135. if (!$cartId) {
  136. return new Response('Cart doesn\'t exist in session', Response::HTTP_BAD_REQUEST);
  137. }
  138. try {
  139. $cart = $this->addToCartService->removePaymentMethod($cartId);
  140. } catch (\LogicException $exception) {
  141. return new Response($exception->getMessage(), Response::HTTP_BAD_REQUEST);
  142. }
  143. $packPaymentMethodInfo = $paymentMethodData['pack_services_payment_method'];
  144. $this->addToCartService->addPackPaymentMethod($cart, $packPaymentMethodInfo);
  145. return new Response();
  146. }
  147. }