<?php
declare(strict_types=1);
namespace App\Controller\Front\Api\Session;
use App\Application\Service\Customer\CustomerGroupService;
use App\Application\Service\Helper\CookieHelper;
use App\Application\Service\Services\ServiceCustomerService;
use App\Application\Service\Session\SessionService;
use App\Entity\System\Customer;
use App\Entity\System\Service;
use App\Factory\Account\SessionResponseFactory;
use App\Manager\System\CatalogManager;
use App\Service\Customer\CustomerPackService;
use App\Service\SecurityService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class SessionController extends AbstractController
{
private CustomerPackService $customerPackService;
private CatalogManager $catalogManager;
private SecurityService $securityService;
private CustomerGroupService $customerGroupService;
private ServiceCustomerService $serviceCustomerService;
private SessionService $sessionService;
public function __construct(
CustomerPackService $customerPackService,
CatalogManager $catalogManager,
SecurityService $securityService,
CustomerGroupService $customerGroupService,
ServiceCustomerService $serviceCustomerService,
SessionService $sessionService
) {
$this->customerPackService = $customerPackService;
$this->catalogManager = $catalogManager;
$this->securityService = $securityService;
$this->customerGroupService = $customerGroupService;
$this->serviceCustomerService = $serviceCustomerService;
$this->sessionService = $sessionService;
}
/**
* @Route("/front/api/session/check", name="front_api_customer_check_cookie", methods={"GET"})
* @Route("/{lang}/front/api/session/check", name="front_api_customer_check_cookie_lang", methods={"GET"})
*/
public function check(Request $request): Response
{
$language = $this->sessionService->getLocale();
$session = $request->getSession();
$cookieValue = $request->cookies->get(\SESSION_NAME);
if ($cookieValue === null) {
$cookieValue = CookieHelper::generateValue();
}
$expireTime = \time() + 172800;
$cookie = new Cookie(\SESSION_NAME, $cookieValue, $expireTime, '/', PARENT_DOMAIN);
if (!$session->isStarted()) {
$response = new Response(
'Session is not started',
Response::HTTP_NO_CONTENT,
['content-type' => 'application/json']
);
$response->headers->setCookie($cookie);
return $response;
}
$lastUsed = $session->getMetadataBag()->getLastUsed();
$maxLifetime = $session->getMetadataBag()->getLifetime();
if (\time() - $lastUsed > $maxLifetime) {
$session->invalidate();
$response = new Response(
'Session has expired',
Response::HTTP_NO_CONTENT,
['content-type' => 'application/json']
);
$response->headers->setCookie($cookie);
return $response;
}
$user = $this->securityService->findUser();
if (!$user instanceof Customer) {
$response = new Response(
'Session data not found',
Response::HTTP_NO_CONTENT,
['content-type' => 'application/json']
);
$response->headers->setCookie($cookie);
return $response;
}
$customerId = $user->getId();
$email = $user->getEmail();
$name = $user->getName();
$surname = $user->getSurnames();
$isPartialCreation = $user->getPartialCreation() > 0;
$languageId = $user->getLanguage()->getId();
$showWholesalePrice = $this->customerGroupService->customerHasProfitWholesale($user->getId());
$canDownloadProductCsv = $this->customerGroupService->customerHasProfitWholesale($user->getId())
&& $this->serviceCustomerService->customerHasService($customerId, Service::DOWNLOAD_PRODUCT_LIST);
$customerPackInfo = $this->customerPackService->getCustomerPackInfo($customerId, $languageId);
$customerCatalogReferences = $this->catalogManager->findCatalogReferencesByCustomerId($customerId);
$customerCatalogNames = $this->catalogManager->findCatalogNamesByCustomerId($customerId);
$isImpersonated = $this->securityService->isImpersonated();
$sessionResponse = SessionResponseFactory::build(
$customerId,
$email,
$name,
$surname,
$language,
$customerPackInfo,
$isPartialCreation,
$isImpersonated,
$customerCatalogReferences,
$customerCatalogNames,
$canDownloadProductCsv,
$showWholesalePrice
);
return new Response(\json_encode($sessionResponse), Response::HTTP_OK, ['content-type' => 'application/json']);
}
}