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

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