<?php
declare(strict_types=1);
namespace App\Controller\Front\Cart;
use App\Application\Service\Helper\LogWriterService;
use App\Application\Service\Session\SessionService;
use App\Entity\System\Customer;
use App\Entity\System\Taxonomy;
use App\Manager\System\ProductManager;
use App\Manager\System\SubscriptionManager;
use App\Service\Cart\AddToCartService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class SubscriptionCartController
*
* @Route("/subscription/cart", name="subscription_cart_controller")
*/
class SubscriptionCartController extends AbstractController
{
public function __construct(
private readonly SubscriptionManager $subscriptionManager,
private readonly AddToCartService $addToCartService,
private readonly SessionService $sessionService,
private readonly LogWriterService $logWriterService,
private readonly ProductManager $productManager,
) {
}
/**
* @param Request $request
*
* @Route("/add-pack", name="subscription_cart_add_pack", methods={"POST"})
*
* @return Response
*/
public function subscriptionCartAddPack(Request $request): Response
{
if (!$request->get('id_subscription')) {
return new Response('Subscription id is required', Response::HTTP_BAD_REQUEST);
}
$subscriptionId = (int)$request->get('id_subscription');
$subscription = $this->subscriptionManager->findOneById($subscriptionId);
if (!$subscription) {
return new Response('Subscription not exists', Response::HTTP_BAD_REQUEST);
}
/** @var Customer|null $customer */
$customer = $this->getUser();
if (!$customer) {
$this->logWriterService->logCartCreationError(__METHOD__.' - Could not recover customer from session');
return new Response('could not load customer from session', Response::HTTP_UNAUTHORIZED);
}
try {
$this->addToCartService->addSubscriptionToServicesCart($subscription, $customer);
} catch (\Throwable $exception) {
\Sentry\captureException($exception);
$this->logWriterService->logCartCreationError(__METHOD__.' - Exception: '.\json_encode($exception));
return new Response($exception->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
return new Response();
}
/**
* @param Request $request
*
* @Route("/add-services", name="subscription_cart_add_services", methods={"POST"})
*
* @return Response
*/
public function subscriptionCartAddServices(Request $request): Response
{
/** @var Customer|null $customer */
$customer = $this->getUser();
if (!$customer) {
$this->logWriterService->logCartCreationError(__METHOD__.' - Could not recover customer from session');
return new Response('could not load customer from session', Response::HTTP_UNAUTHORIZED);
}
$servicesRequestContent = $request->get('services');
$servicesRequest = \json_decode($servicesRequestContent, true);
$customerId = $customer->getId();
$cart = $this->addToCartService->handleCartServices($customer);
if (empty($servicesRequest)) {
$this->logWriterService->logCartCreationError(__METHOD__.' - empty request: '.\json_encode($servicesRequest));
$this->addToCartService->removeAllServicesFromCart($cart);
return new Response('empty request from client', Response::HTTP_UNAUTHORIZED);
}
$shopData = [];
$ecommerceConnectorIds = [];
$marketplaceConnectorIds = [];
/** @var array{serviceId: int, themeId: int|null, catalogId: int|null} $serviceRequest */
foreach ($servicesRequest as $serviceRequest) {
$requestedServiceId = $serviceRequest['serviceId'];
$requestedThemeId = $serviceRequest['themeId'];
$requestedCatalogId = $serviceRequest['catalogId'];
if ($requestedThemeId !== null && $requestedCatalogId !== null) {
$shopData[] = ['shopId' => $requestedThemeId, 'topicId' => $requestedCatalogId];
continue;
}
try {
$productDefaultTaxonomy = $this->productManager->getDefaultTaxonomyByProductId($requestedServiceId);
} catch (\Throwable $exception) {
continue;
}
if ($productDefaultTaxonomy === Taxonomy::MARKETPLACES_TAXONOMY_ID) {
$marketplaceConnectorIds[] = $requestedServiceId;
}
if ($productDefaultTaxonomy === Taxonomy::ECOMMERCE_TAXONOMY_ID) {
$ecommerceConnectorIds[] = $requestedServiceId;
}
}
try {
$cart = $this->addToCartService->addMarketplaceConnectorsToServicesCart($cart, $marketplaceConnectorIds, $customerId);
$cart = $this->addToCartService->addEcommerceConnectorsToServicesCart($cart, $ecommerceConnectorIds, $customerId);
$this->addToCartService->addShopsToServicesCart($cart, $shopData, $customerId);
} catch (\Throwable $exception) {
\Sentry\captureException($exception);
$this->logWriterService->logCartCreationError(__METHOD__.' - Exception: '.\json_encode($exception));
return new Response($exception->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
return new Response();
}
/**
* @param Request $request
*
* @Route("/add-payment-methods", name="subscription_cart_add_payment_methods", methods={"POST"})
*
* @return Response
*/
public function subscriptionCartAddPaymentMethods(Request $request): Response
{
$paymentMethodData = json_decode($request->getContent(), true);
if (!isset($paymentMethodData['pack_services_payment_method'])) {
return new Response('Payment method are required', Response::HTTP_BAD_REQUEST);
}
$cartId = $this->sessionService->get('id_cart_services');
if (!$cartId) {
return new Response('Cart doesn\'t exist in session', Response::HTTP_BAD_REQUEST);
}
try {
$cart = $this->addToCartService->removePaymentMethod($cartId);
} catch (\LogicException $exception) {
return new Response($exception->getMessage(), Response::HTTP_BAD_REQUEST);
}
$packPaymentMethodInfo = $paymentMethodData['pack_services_payment_method'];
$this->addToCartService->addPackPaymentMethod($cart, $packPaymentMethodInfo);
return new Response();
}
}