src/Application/Service/Session/SessionService.php line 71

Open in your IDE?
  1. <?php
  2. namespace App\Application\Service\Session;
  3. use App\Application\Service\Customer\CustomerGroupService;
  4. use App\Entity\System\Customer;
  5. use App\Manager\System\CustomerManager;
  6. use App\Manager\System\LanguageManager;
  7. use App\Service\EnvironmentService;
  8. use App\Service\SecurityService;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. class SessionService
  12. {
  13.     public const ALLOWED_CATALOGS_KEY 'allowedCatalogs';
  14.     private RequestStack $requestStack;
  15.     private SecurityService $securityService;
  16.     private CustomerGroupService $customerGroupService;
  17.     private LanguageManager $languageManager;
  18.     private ParameterBagInterface $parameterBag;
  19.     private CustomerManager $customerManager;
  20.     private EnvironmentService $environmentService;
  21.     public function __construct(
  22.         RequestStack $requestStack,
  23.         SecurityService $securityService,
  24.         CustomerGroupService $customerGroupService,
  25.         LanguageManager $languageManager,
  26.         ParameterBagInterface $parameterBag,
  27.         CustomerManager $customerManager,
  28.         EnvironmentService $environmentService
  29.     ) {
  30.         $this->requestStack $requestStack;
  31.         $this->securityService $securityService;
  32.         $this->customerGroupService $customerGroupService;
  33.         $this->languageManager $languageManager;
  34.         $this->parameterBag $parameterBag;
  35.         $this->customerManager $customerManager;
  36.         $this->environmentService $environmentService;
  37.     }
  38.     // @phpstan-ignore-next-line
  39.     public function set(string $key$value): bool
  40.     {
  41.         if (!empty($key)) {
  42.             $_SESSION[$key] = $value;
  43.             try {
  44.                 $this->requestStack->getSession()->set($key$value);
  45.             } catch (\Exception $e) {
  46.                 if ($key === 'id_lang' || $key === 'lang') {
  47.                     \Sentry\captureException($e);
  48.                 }
  49.             }
  50.             return true;
  51.         }
  52.         return false;
  53.     }
  54.     /**
  55.      * @param bool|int|string $default
  56.      */
  57.     // @phpstan-ignore-next-line
  58.     public function get(string $key$default falsebool $delete false)
  59.     {
  60.         if ($key === 'id_lang' || $key === 'lang' || $key === 'id_cart') {
  61.             try {
  62.                 return $this->requestStack->getSession()->get($keyfalse);
  63.             } catch (\Exception $e) {
  64.                 \Sentry\captureException($e);
  65.                 if ($this->environmentService->isDev()) {
  66.                     throw $e;
  67.                 }
  68.             }
  69.         }
  70.         if ($key === 'id_employee') {
  71.             return $this->securityService->getSessionEmployeeId();
  72.         }
  73.         if ($key === 'id_profile') {
  74.             return $this->securityService->getSessionEmployeeProfileId();
  75.         }
  76.         $user $this->securityService->findUser();
  77.         if ($user instanceof Customer) {
  78.             if ($key === 'customer_surnames') {
  79.                 return $user->getSurnames();
  80.             }
  81.             if ($key === 'customer_name') {
  82.                 return $user->getSurnames();
  83.             }
  84.             if ($key === 'customer_group') {
  85.                 return $user->getGroup() ? $user->getGroup()->getId() : null;
  86.             }
  87.             if ($key === 'customerPack') {
  88.                 return $user->getIdPack();
  89.             }
  90.             if ($key === 'customerDateCsv') {
  91.                 return $user->getDateCsv();
  92.             }
  93.             if ($key === 'customerExcluirIva') {
  94.                 return $user->getDateExcluirIva() >= new \DateTime();
  95.             }
  96.             if ($key === 'is_wholesaler') {
  97.                 return $this->customerGroupService->customerHasProfitWholesale($user->getId());
  98.             }
  99.             if ($key === 'id_customer') {
  100.                 return $user->getId();
  101.             }
  102.         }
  103.         $value $default;
  104.         if (!empty($_SESSION[$key])) {
  105.             $value $_SESSION[$key];
  106.         }
  107.         if ($delete) {
  108.             unset($_SESSION[$key]);
  109.             try {
  110.                 $this->requestStack->getSession()->remove($key);
  111.             } catch (\Exception $e) {
  112.                 \Sentry\captureException($e);
  113.             }
  114.         }
  115.         return $value;
  116.     }
  117.     public function deleteSessionsMultipleOrders(): void
  118.     {
  119.         $this->delete('carts_pendings');
  120.         $this->delete('payment_card');
  121.         $this->delete('parameters_order');
  122.         $this->delete('parameters_multiple_orders');
  123.         $this->delete('carts_pendings_first');
  124.     }
  125.     public function delete(string $key): void
  126.     {
  127.         if (isset($_SESSION[$key])) {
  128.             unset($_SESSION[$key]);
  129.         }
  130.         try {
  131.             $this->requestStack->getSession()->remove($key);
  132.         } catch (\Exception $e) {
  133.         }
  134.     }
  135.     public function setLocale(string $locale): void
  136.     {
  137.         if (!\in_array($locale$this->parameterBag->get('locales'), true)) {
  138.             return;
  139.         }
  140.         $this->set('lang'$locale);
  141.     }
  142.     public function getLocale(): string
  143.     {
  144.         return $this->get('lang');
  145.     }
  146.     public function getLocaleId(): int
  147.     {
  148.         return $this->get('id_lang');
  149.     }
  150.     public function setLocaleId(int $languageId): void
  151.     {
  152.         $language $this->languageManager->findOneById($languageId);
  153.         if (!$language || !\in_array($language->getIsoCode(), $this->parameterBag->get('locales'), true)) {
  154.             return;
  155.         }
  156.         $this->set('id_lang'$language->getId());
  157.     }
  158.     public function setCustomerLang(int $customerId): void
  159.     {
  160.         $customer $this->customerManager->getOneById($customerId);
  161.         $this->setLocaleParameters($customer->getLanguage()->getIsoCode());
  162.     }
  163.     public function setLocaleParameters(string $locale): void
  164.     {
  165.         $language $this->languageManager->findOneByIsoCode($locale);
  166.         if (!$language || !\in_array($language->getIsoCode(), $this->parameterBag->get('locales'), true)) {
  167.             return;
  168.         }
  169.         $this->setLocaleId($language->getId());
  170.         $this->set('date_format_lite'$language->getDateFormatLite());
  171.         $this->set('date_format_full'$language->getDateFormatFull());
  172.         $this->set('currency_format'$language->getCurrencyFormat());
  173.         $this->setLocale($language->getIsoCode());
  174.     }
  175.     public function deleteFlashData(): void
  176.     {
  177.         if ($this->requestStack->getSession()->get('__flashdata')) {
  178.             $flashdata_vars $this->requestStack->getSession()->get('__flashdata');
  179.             $this->requestStack->getSession()->remove('__flashdata');
  180.             foreach ($flashdata_vars as $value) {
  181.                 $this->requestStack->getSession()->remove('__flashdata');
  182.             }
  183.             unset($flashdata_vars);
  184.         }
  185.     }
  186. }