<?php
namespace App\Application\Service\Front;
use App\Entity\System\Product;
use App\Manager\System\SubscriptionManager;
use App\Application\Service\Helper\LogWriterService;
use App\Application\Service\Session\SessionService;
use App\Entity\System\Customer;
use App\Entity\System\Pack;
use App\Entity\System\Subscription;
use App\Service\SecurityService;
use Symfony\Component\HttpFoundation\RequestStack;
class SubscriptionService
{
public function __construct(
private readonly RequestStack $requestStack,
private readonly SubscriptionManager $subscriptionManager,
private readonly LogWriterService $logWriterService,
private readonly SessionService $sessionService,
private readonly SecurityService $securityService,
) {
}
/**
* @param array $parameters
*/
public function checkout(array $parameters): void
{
$logMessage = '';
$this->requestStack->getSession()->remove('checkout_services');
$this->requestStack->getSession()->remove('goToStepServices');
$this->requestStack->getSession()->remove('arrayProductServices');
$this->sessionService->delete('restOrderIds');
$this->sessionService->delete('paidOrderIds');
$customer = $this->securityService->findSessionCustomer();
if ($customer === null) {
return;
}
$this->manageStepServices($customer, $parameters);
$this->manageStepShops($customer, $parameters, $logMessage);
/** @var Subscription|null $subscription */
$subscription = $this->subscriptionManager->findOneById((int)$parameters['idSubscription']);
if (Pack::PACK_WITHOUT_PACK === $customer->getPack()->getId() || ($subscription && $subscription->getPack()->getId() > $customer->getPack()->getId())) {
if ($parameters['idSubscription']) {
$this->requestStack->getSession()->set('newPackContracted', $parameters['idSubscription']);
$logMessage .= ' - Pack: '.$parameters['idSubscription'];
}
}
$logMessage = 'Usuario: '.$customer->getId().$logMessage;
$this->logWriterService->subscriptionPurchase($logMessage);
}
/**
* @param Customer $customer
* @param array $parameters
*/
public function manageStepServices(Customer $customer, array $parameters): void
{
if (empty($parameters['goToStepServices']) && empty($parameters['serviceProductId'])) {
return;
}
$customerPackId = $customer->getPack()->getId();
if ($parameters['serviceProductId']) {
if (isset(Product::ALLOWED_CONNECTOR_PRODUCT_IDS_BY_PACK_ID[$customerPackId]) && in_array($parameters['serviceProductId'], Product::ALLOWED_CONNECTOR_PRODUCT_IDS_BY_PACK_ID[$customerPackId], true)) {
$this->requestStack->getSession()->set('goToStepServices', true);
$this->requestStack->getSession()->set('arrayProductServices', [$parameters['serviceProductId'] => true]);
} elseif (in_array($parameters['serviceProductId'], Product::ALLOWED_CONNECTOR_PRODUCT_IDS_BY_PACK_ID[Pack::PACK_ECOMMERCE], true)) {
$this->requestStack->getSession()->set('newPackContracted', Subscription::PACK_ECOMMERCE_M);
} elseif (in_array($parameters['serviceProductId'], Product::ALLOWED_CONNECTOR_PRODUCT_IDS_BY_PACK_ID[Pack::PACK_MARKETPLACES], true)) {
$this->requestStack->getSession()->set('newPackContracted', Subscription::PACK_MARKETPLACE_M);
}
}
}
/**
* @param Customer $customer
* @param array $parameters
* @param string $logMessage
*/
private function manageStepShops(Customer $customer, array $parameters, string &$logMessage): void
{
if ($parameters['isNewShopSelected']) {
$newShopSelected = [
'catalogId' => $parameters['catalogId'],
'themeId' => $parameters['themeId'],
];
$logMessage .= ' - Catálogo: '.$parameters['catalogId'].', Plantilla: '.$parameters['themeId'];
$this->requestStack->getSession()->set('newShopSelected', $newShopSelected);
$customerPack = $customer->getPack()->getId();
if ($customerPack === Pack::PACK_ECOMMERCE || $customerPack === Pack::PACK_MARKETPLACES) {
$this->requestStack->getSession()->set('goToStepServices', true);
}
}
}
}