<?php
namespace App\Application\Service\Session;
use App\Application\Service\Customer\CustomerGroupService;
use App\Entity\System\Customer;
use App\Manager\System\CustomerManager;
use App\Manager\System\LanguageManager;
use App\Service\EnvironmentService;
use App\Service\SecurityService;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class SessionService
{
public const ALLOWED_CATALOGS_KEY = 'allowedCatalogs';
private RequestStack $requestStack;
private SecurityService $securityService;
private CustomerGroupService $customerGroupService;
private LanguageManager $languageManager;
private ParameterBagInterface $parameterBag;
private CustomerManager $customerManager;
private EnvironmentService $environmentService;
public function __construct(
RequestStack $requestStack,
SecurityService $securityService,
CustomerGroupService $customerGroupService,
LanguageManager $languageManager,
ParameterBagInterface $parameterBag,
CustomerManager $customerManager,
EnvironmentService $environmentService
) {
$this->requestStack = $requestStack;
$this->securityService = $securityService;
$this->customerGroupService = $customerGroupService;
$this->languageManager = $languageManager;
$this->parameterBag = $parameterBag;
$this->customerManager = $customerManager;
$this->environmentService = $environmentService;
}
// @phpstan-ignore-next-line
public function set(string $key, $value): bool
{
if (!empty($key)) {
$_SESSION[$key] = $value;
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)
{
if ($key === 'id_lang' || $key === 'lang' || $key === 'id_cart') {
try {
return $this->requestStack->getSession()->get($key, false);
} catch (\Exception $e) {
\Sentry\captureException($e);
if ($this->environmentService->isDev()) {
throw $e;
}
}
}
if ($key === 'id_employee') {
return $this->securityService->getSessionEmployeeId();
}
if ($key === 'id_profile') {
return $this->securityService->getSessionEmployeeProfileId();
}
$user = $this->securityService->findUser();
if ($user instanceof Customer) {
if ($key === 'customer_surnames') {
return $user->getSurnames();
}
if ($key === 'customer_name') {
return $user->getSurnames();
}
if ($key === 'customer_group') {
return $user->getGroup() ? $user->getGroup()->getId() : null;
}
if ($key === 'customerPack') {
return $user->getIdPack();
}
if ($key === 'customerDateCsv') {
return $user->getDateCsv();
}
if ($key === 'customerExcluirIva') {
return $user->getDateExcluirIva() >= new \DateTime();
}
if ($key === 'is_wholesaler') {
return $this->customerGroupService->customerHasProfitWholesale($user->getId());
}
if ($key === 'id_customer') {
return $user->getId();
}
}
$value = $default;
if (!empty($_SESSION[$key])) {
$value = $_SESSION[$key];
}
if ($delete) {
unset($_SESSION[$key]);
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
{
if (isset($_SESSION[$key])) {
unset($_SESSION[$key]);
}
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);
}
}
}