src/Application/Service/Front/SubscriptionService.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Application\Service\Front;
  3. use App\Entity\System\Product;
  4. use App\Manager\System\SubscriptionManager;
  5. use App\Application\Service\Helper\LogWriterService;
  6. use App\Application\Service\Session\SessionService;
  7. use App\Entity\System\Customer;
  8. use App\Entity\System\Pack;
  9. use App\Entity\System\Subscription;
  10. use App\Service\SecurityService;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. class SubscriptionService
  13. {
  14. public function __construct(
  15. private readonly RequestStack $requestStack,
  16. private readonly SubscriptionManager $subscriptionManager,
  17. private readonly LogWriterService $logWriterService,
  18. private readonly SessionService $sessionService,
  19. private readonly SecurityService $securityService,
  20. ) {
  21. }
  22. /**
  23. * @param array $parameters
  24. */
  25. public function checkout(array $parameters): void
  26. {
  27. $logMessage = '';
  28. $this->requestStack->getSession()->remove('checkout_services');
  29. $this->requestStack->getSession()->remove('goToStepServices');
  30. $this->requestStack->getSession()->remove('arrayProductServices');
  31. $this->sessionService->delete('restOrderIds');
  32. $this->sessionService->delete('paidOrderIds');
  33. $customer = $this->securityService->findSessionCustomer();
  34. if ($customer === null) {
  35. return;
  36. }
  37. $this->manageStepServices($customer, $parameters);
  38. $this->manageStepShops($customer, $parameters, $logMessage);
  39. /** @var Subscription|null $subscription */
  40. $subscription = $this->subscriptionManager->findOneById((int)$parameters['idSubscription']);
  41. if (Pack::PACK_WITHOUT_PACK === $customer->getPack()->getId() || ($subscription && $subscription->getPack()->getId() > $customer->getPack()->getId())) {
  42. if ($parameters['idSubscription']) {
  43. $this->requestStack->getSession()->set('newPackContracted', $parameters['idSubscription']);
  44. $logMessage .= ' - Pack: '.$parameters['idSubscription'];
  45. }
  46. }
  47. $logMessage = 'Usuario: '.$customer->getId().$logMessage;
  48. $this->logWriterService->subscriptionPurchase($logMessage);
  49. }
  50. /**
  51. * @param Customer $customer
  52. * @param array $parameters
  53. */
  54. public function manageStepServices(Customer $customer, array $parameters): void
  55. {
  56. if (empty($parameters['goToStepServices']) && empty($parameters['serviceProductId'])) {
  57. return;
  58. }
  59. $customerPackId = $customer->getPack()->getId();
  60. if ($parameters['serviceProductId']) {
  61. 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)) {
  62. $this->requestStack->getSession()->set('goToStepServices', true);
  63. $this->requestStack->getSession()->set('arrayProductServices', [$parameters['serviceProductId'] => true]);
  64. } elseif (in_array($parameters['serviceProductId'], Product::ALLOWED_CONNECTOR_PRODUCT_IDS_BY_PACK_ID[Pack::PACK_ECOMMERCE], true)) {
  65. $this->requestStack->getSession()->set('newPackContracted', Subscription::PACK_ECOMMERCE_M);
  66. } elseif (in_array($parameters['serviceProductId'], Product::ALLOWED_CONNECTOR_PRODUCT_IDS_BY_PACK_ID[Pack::PACK_MARKETPLACES], true)) {
  67. $this->requestStack->getSession()->set('newPackContracted', Subscription::PACK_MARKETPLACE_M);
  68. }
  69. }
  70. }
  71. /**
  72. * @param Customer $customer
  73. * @param array $parameters
  74. * @param string $logMessage
  75. */
  76. private function manageStepShops(Customer $customer, array $parameters, string &$logMessage): void
  77. {
  78. if ($parameters['isNewShopSelected']) {
  79. $newShopSelected = [
  80. 'catalogId' => $parameters['catalogId'],
  81. 'themeId' => $parameters['themeId'],
  82. ];
  83. $logMessage .= ' - Catálogo: '.$parameters['catalogId'].', Plantilla: '.$parameters['themeId'];
  84. $this->requestStack->getSession()->set('newShopSelected', $newShopSelected);
  85. $customerPack = $customer->getPack()->getId();
  86. if ($customerPack === Pack::PACK_ECOMMERCE || $customerPack === Pack::PACK_MARKETPLACES) {
  87. $this->requestStack->getSession()->set('goToStepServices', true);
  88. }
  89. }
  90. }
  91. }