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

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.         $user $this->securityService->findUser();
  74.         if ($user instanceof Customer) {
  75.             if ($key === 'customer_surnames') {
  76.                 return $user->getSurnames();
  77.             }
  78.             if ($key === 'customer_name') {
  79.                 return $user->getSurnames();
  80.             }
  81.             if ($key === 'customer_group') {
  82.                 return $user->getGroup() ? $user->getGroup()->getId() : null;
  83.             }
  84.             if ($key === 'customerPack') {
  85.                 return $user->getIdPack();
  86.             }
  87.             if ($key === 'customerDateCsv') {
  88.                 return $user->getDateCsv();
  89.             }
  90.             if ($key === 'customerExcluirIva') {
  91.                 return $user->getDateExcluirIva() >= new \DateTime();
  92.             }
  93.             if ($key === 'is_wholesaler') {
  94.                 return $this->customerGroupService->customerHasProfitWholesale($user->getId());
  95.             }
  96.             if ($key === 'id_customer') {
  97.                 return $user->getId();
  98.             }
  99.         }
  100.         $value $default;
  101.         if (!empty($_SESSION[$key])) {
  102.             $value $_SESSION[$key];
  103.         }
  104.         if ($delete) {
  105.             unset($_SESSION[$key]);
  106.             try {
  107.                 $this->requestStack->getSession()->remove($key);
  108.             } catch (\Exception $e) {
  109.                 \Sentry\captureException($e);
  110.             }
  111.         }
  112.         return $value;
  113.     }
  114.     public function deleteSessionsMultipleOrders(): void
  115.     {
  116.         $this->delete('carts_pendings');
  117.         $this->delete('payment_card');
  118.         $this->delete('parameters_order');
  119.         $this->delete('parameters_multiple_orders');
  120.         $this->delete('carts_pendings_first');
  121.     }
  122.     public function delete(string $key): void
  123.     {
  124.         if (isset($_SESSION[$key])) {
  125.             unset($_SESSION[$key]);
  126.         }
  127.         try {
  128.             $this->requestStack->getSession()->remove($key);
  129.         } catch (\Exception $e) {
  130.         }
  131.     }
  132.     public function setLocale(string $locale): void
  133.     {
  134.         if (!\in_array($locale$this->parameterBag->get('locales'), true)) {
  135.             return;
  136.         }
  137.         $this->set('lang'$locale);
  138.     }
  139.     public function getLocale(): string
  140.     {
  141.         return $this->get('lang');
  142.     }
  143.     public function getLocaleId(): int
  144.     {
  145.         return $this->get('id_lang');
  146.     }
  147.     public function setLocaleId(int $languageId): void
  148.     {
  149.         $language $this->languageManager->findOneById($languageId);
  150.         if (!$language || !\in_array($language->getIsoCode(), $this->parameterBag->get('locales'), true)) {
  151.             return;
  152.         }
  153.         $this->set('id_lang'$language->getId());
  154.     }
  155.     public function setCustomerLang(int $customerId): void
  156.     {
  157.         $customer $this->customerManager->getOneById($customerId);
  158.         $this->setLocaleParameters($customer->getLanguage()->getIsoCode());
  159.     }
  160.     public function setLocaleParameters(string $locale): void
  161.     {
  162.         $language $this->languageManager->findOneByIsoCode($locale);
  163.         if (!$language || !\in_array($language->getIsoCode(), $this->parameterBag->get('locales'), true)) {
  164.             return;
  165.         }
  166.         $this->setLocaleId($language->getId());
  167.         $this->set('date_format_lite'$language->getDateFormatLite());
  168.         $this->set('date_format_full'$language->getDateFormatFull());
  169.         $this->set('currency_format'$language->getCurrencyFormat());
  170.         $this->setLocale($language->getIsoCode());
  171.     }
  172.     public function deleteFlashData(): void
  173.     {
  174.         if ($this->requestStack->getSession()->get('__flashdata')) {
  175.             $flashdata_vars $this->requestStack->getSession()->get('__flashdata');
  176.             $this->requestStack->getSession()->remove('__flashdata');
  177.             foreach ($flashdata_vars as $value) {
  178.                 $this->requestStack->getSession()->remove('__flashdata');
  179.             }
  180.             unset($flashdata_vars);
  181.         }
  182.     }
  183. }