<?php
namespace App\Application\Service\Session;
use App\Manager\System\CustomerManager;
use App\Manager\System\LanguageManager;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class SessionService
{
public const ALLOWED_CATALOGS_KEY = 'allowedCatalogs';
private RequestStack $requestStack;
private LanguageManager $languageManager;
private ParameterBagInterface $parameterBag;
private CustomerManager $customerManager;
public function __construct(
RequestStack $requestStack,
LanguageManager $languageManager,
ParameterBagInterface $parameterBag,
CustomerManager $customerManager
) {
$this->requestStack = $requestStack;
$this->languageManager = $languageManager;
$this->parameterBag = $parameterBag;
$this->customerManager = $customerManager;
}
// @phpstan-ignore-next-line
public function set(string $key, $value): bool
{
if (!empty($key)) {
try {
$this->requestStack->getSession()->set($key, $value);
} catch (\Exception $e) {
if ($key === 'id_lang' || $key === 'lang') {
\Sentry\captureException($e);
}
}
return true;
}
return false;
}
/**
* @param bool|int|string $default
*/
// @phpstan-ignore-next-line
public function get(string $key, $default = false, bool $delete = false)
{
try {
$value = $this->requestStack->getSession()->get($key, $default);
} catch (\Exception $e) {
\Sentry\captureException($e);
$value = $default;
}
if ($delete) {
try {
$this->requestStack->getSession()->remove($key);
} catch (\Exception $e) {
\Sentry\captureException($e);
}
}
return $value;
}
public function deleteSessionsMultipleOrders(): void
{
$this->delete('carts_pendings');
$this->delete('payment_card');
$this->delete('parameters_order');
$this->delete('parameters_multiple_orders');
$this->delete('carts_pendings_first');
}
public function delete(string $key): void
{
try {
$this->requestStack->getSession()->remove($key);
} catch (\Exception $e) {
}
}
public function setLocale(string $locale): void
{
if (!\in_array($locale, $this->parameterBag->get('locales'), true)) {
return;
}
$this->set('lang', $locale);
}
public function getLocale(): string
{
return $this->get('lang');
}
public function getLocaleId(): int
{
return $this->get('id_lang');
}
public function setLocaleId(int $languageId): void
{
$language = $this->languageManager->findOneById($languageId);
if (!$language || !\in_array($language->getIsoCode(), $this->parameterBag->get('locales'), true)) {
return;
}
$this->set('id_lang', $language->getId());
}
public function setCustomerLang(int $customerId): void
{
$customer = $this->customerManager->getOneById($customerId);
$this->setLocaleParameters($customer->getLanguage()->getIsoCode());
}
public function setLocaleParameters(string $locale): void
{
$language = $this->languageManager->findOneByIsoCode($locale);
if (!$language || !\in_array($language->getIsoCode(), $this->parameterBag->get('locales'), true)) {
return;
}
$this->setLocaleId($language->getId());
$this->set('date_format_lite', $language->getDateFormatLite());
$this->set('date_format_full', $language->getDateFormatFull());
$this->set('currency_format', $language->getCurrencyFormat());
$this->setLocale($language->getIsoCode());
}
public function deleteFlashData(): void
{
if ($this->requestStack->getSession()->get('__flashdata')) {
$flashdata_vars = $this->requestStack->getSession()->get('__flashdata');
$this->requestStack->getSession()->remove('__flashdata');
foreach ($flashdata_vars as $value) {
$this->requestStack->getSession()->remove('__flashdata');
}
unset($flashdata_vars);
}
}
}