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

Open in your IDE?
  1. <?php
  2. namespace App\Application\Service\Session;
  3. use App\Manager\System\CustomerManager;
  4. use App\Manager\System\LanguageManager;
  5. use App\Service\EnvironmentService;
  6. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. class SessionService
  9. {
  10. public const ALLOWED_CATALOGS_KEY = 'allowedCatalogs';
  11. private RequestStack $requestStack;
  12. private LanguageManager $languageManager;
  13. private ParameterBagInterface $parameterBag;
  14. private CustomerManager $customerManager;
  15. private EnvironmentService $environmentService;
  16. public function __construct(
  17. RequestStack $requestStack,
  18. LanguageManager $languageManager,
  19. ParameterBagInterface $parameterBag,
  20. CustomerManager $customerManager,
  21. EnvironmentService $environmentService
  22. ) {
  23. $this->requestStack = $requestStack;
  24. $this->languageManager = $languageManager;
  25. $this->parameterBag = $parameterBag;
  26. $this->customerManager = $customerManager;
  27. $this->environmentService = $environmentService;
  28. }
  29. // @phpstan-ignore-next-line
  30. public function set(string $key, $value): bool
  31. {
  32. if ($this->environmentService->isApi()) {
  33. \Sentry\captureException(new \Exception('Session used from API'));
  34. }
  35. if (!empty($key)) {
  36. try {
  37. $this->requestStack->getSession()->set($key, $value);
  38. } catch (\Exception $e) {
  39. if ($key === 'id_lang' || $key === 'lang') {
  40. \Sentry\captureException($e);
  41. }
  42. }
  43. return true;
  44. }
  45. return false;
  46. }
  47. /**
  48. * @param bool|int|string|null $default
  49. */
  50. // @phpstan-ignore-next-line
  51. public function get(string $key, $default = false, bool $delete = false)
  52. {
  53. if ($this->environmentService->isApi()) {
  54. \Sentry\captureException(new \Exception('Session used from API'));
  55. }
  56. try {
  57. $value = $this->requestStack->getSession()->get($key, $default);
  58. } catch (\Exception $e) {
  59. \Sentry\captureException($e);
  60. $value = $default;
  61. }
  62. if ($delete) {
  63. try {
  64. $this->requestStack->getSession()->remove($key);
  65. } catch (\Exception $e) {
  66. \Sentry\captureException($e);
  67. }
  68. }
  69. return $value;
  70. }
  71. public function deleteSessionsMultipleOrders(): void
  72. {
  73. $this->delete('carts_pendings');
  74. $this->delete('payment_card');
  75. $this->delete('parameters_order');
  76. $this->delete('parameters_multiple_orders');
  77. $this->delete('carts_pendings_first');
  78. }
  79. public function delete(string $key): void
  80. {
  81. if ($this->environmentService->isApi()) {
  82. \Sentry\captureException(new \Exception('Session used from API'));
  83. }
  84. try {
  85. $this->requestStack->getSession()->remove($key);
  86. } catch (\Exception $e) {
  87. }
  88. }
  89. public function setLocale(string $locale): void
  90. {
  91. if (!\in_array($locale, $this->parameterBag->get('locales'), true)) {
  92. return;
  93. }
  94. $this->set('lang', $locale);
  95. }
  96. public function getLocale(): string
  97. {
  98. return $this->get('lang');
  99. }
  100. public function getLocaleId(): int
  101. {
  102. return $this->get('id_lang');
  103. }
  104. public function setLocaleId(int $languageId): void
  105. {
  106. $language = $this->languageManager->findOneById($languageId);
  107. if (!$language || !\in_array($language->getIsoCode(), $this->parameterBag->get('locales'), true)) {
  108. return;
  109. }
  110. $this->set('id_lang', $language->getId());
  111. }
  112. public function setCustomerLang(int $customerId): void
  113. {
  114. $customer = $this->customerManager->getOneById($customerId);
  115. $this->setLocaleParameters($customer->getLanguage()->getIsoCode());
  116. }
  117. public function setLocaleParameters(string $locale): void
  118. {
  119. $language = $this->languageManager->findOneByIsoCode($locale);
  120. if (!$language || !\in_array($language->getIsoCode(), $this->parameterBag->get('locales'), true)) {
  121. return;
  122. }
  123. $this->setLocaleId($language->getId());
  124. $this->set('date_format_lite', $language->getDateFormatLite());
  125. $this->set('date_format_full', $language->getDateFormatFull());
  126. $this->set('currency_format', $language->getCurrencyFormat());
  127. $this->setLocale($language->getIsoCode());
  128. }
  129. public function deleteFlashData(): void
  130. {
  131. if ($this->requestStack->getSession()->get('__flashdata')) {
  132. $flashdata_vars = $this->requestStack->getSession()->get('__flashdata');
  133. $this->requestStack->getSession()->remove('__flashdata');
  134. foreach ($flashdata_vars as $value) {
  135. $this->requestStack->getSession()->remove('__flashdata');
  136. }
  137. unset($flashdata_vars);
  138. }
  139. }
  140. }