src/Controller/Front/Api/Session/SessionController.php line 77

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Front\Api\Session;
  4. use App\Application\Service\Customer\CustomerGroupService;
  5. use App\Application\Service\Helper\CookieHelper;
  6. use App\Application\Service\Services\ServiceCustomerService;
  7. use App\Application\Service\Session\SessionService;
  8. use App\Entity\System\Customer;
  9. use App\Entity\System\Service;
  10. use App\Factory\Account\SessionResponseFactory;
  11. use App\Manager\System\CatalogManager;
  12. use App\Service\Customer\CustomerPackService;
  13. use App\Service\SecurityService;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\Cookie;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. class SessionController extends AbstractController
  20. {
  21.     private CustomerPackService $customerPackService;
  22.     private CatalogManager $catalogManager;
  23.     private SecurityService $securityService;
  24.     private CustomerGroupService $customerGroupService;
  25.     private ServiceCustomerService $serviceCustomerService;
  26.     private SessionService $sessionService;
  27.     public function __construct(
  28.         CustomerPackService $customerPackService,
  29.         CatalogManager $catalogManager,
  30.         SecurityService $securityService,
  31.         CustomerGroupService $customerGroupService,
  32.         ServiceCustomerService $serviceCustomerService,
  33.         SessionService $sessionService
  34.     ) {
  35.         $this->customerPackService $customerPackService;
  36.         $this->catalogManager $catalogManager;
  37.         $this->securityService $securityService;
  38.         $this->customerGroupService $customerGroupService;
  39.         $this->serviceCustomerService $serviceCustomerService;
  40.         $this->sessionService $sessionService;
  41.     }
  42.     /**
  43.      * @Route("/front/api/session/check", name="front_api_customer_check_cookie", methods={"GET"})
  44.      * @Route("/{lang}/front/api/session/check", name="front_api_customer_check_cookie_lang", methods={"GET"})
  45.      */
  46.     public function check(Request $request): Response
  47.     {
  48.         $language $this->sessionService->getLocale();
  49.         $session $request->getSession();
  50.         $cookieValue $request->cookies->get(\SESSION_NAME);
  51.         if ($cookieValue === null) {
  52.             $cookieValue CookieHelper::generateValue();
  53.         }
  54.         $expireTime = \time() + 172800;
  55.         $cookie = new Cookie(\SESSION_NAME$cookieValue$expireTime'/'PARENT_DOMAIN);
  56.         if (!$session->isStarted()) {
  57.             $response = new Response(
  58.                 'Session is not started',
  59.                 Response::HTTP_NO_CONTENT,
  60.                 ['content-type' => 'application/json']
  61.             );
  62.             $response->headers->setCookie($cookie);
  63.             return $response;
  64.         }
  65.         $lastUsed $session->getMetadataBag()->getLastUsed();
  66.         $maxLifetime $session->getMetadataBag()->getLifetime();
  67.         if (\time() - $lastUsed $maxLifetime) {
  68.             $session->invalidate();
  69.             $response = new Response(
  70.                 'Session has expired',
  71.                 Response::HTTP_NO_CONTENT,
  72.                 ['content-type' => 'application/json']
  73.             );
  74.             $response->headers->setCookie($cookie);
  75.             return $response;
  76.         }
  77.         $user $this->securityService->findUser();
  78.         if (!$user instanceof Customer) {
  79.             $response = new Response(
  80.                 'Session data not found',
  81.                 Response::HTTP_NO_CONTENT,
  82.                 ['content-type' => 'application/json']
  83.             );
  84.             $response->headers->setCookie($cookie);
  85.             return $response;
  86.         }
  87.         $customerId =  $user->getId();
  88.         $email $user->getEmail();
  89.         $name $user->getName();
  90.         $surname $user->getSurnames();
  91.         $isPartialCreation $user->getPartialCreation() > 0;
  92.         $languageId $user->getLanguage()->getId();
  93.         $showWholesalePrice $this->customerGroupService->customerHasProfitWholesale($user->getId());
  94.         $canDownloadProductCsv $this->customerGroupService->customerHasProfitWholesale($user->getId())
  95.             && $this->serviceCustomerService->customerHasService($customerIdService::DOWNLOAD_PRODUCT_LIST);
  96.         $customerPackInfo $this->customerPackService->getCustomerPackInfo($customerId$languageId);
  97.         $customerCatalogReferences $this->catalogManager->findCatalogReferencesByCustomerId($customerId);
  98.         $customerCatalogNames $this->catalogManager->findCatalogNamesByCustomerId($customerId);
  99.         $isImpersonated $this->securityService->isImpersonated();
  100.         $sessionResponse SessionResponseFactory::build(
  101.             $customerId,
  102.             $email,
  103.             $name,
  104.             $surname,
  105.             $language,
  106.             $customerPackInfo,
  107.             $isPartialCreation,
  108.             $isImpersonated,
  109.             $customerCatalogReferences,
  110.             $customerCatalogNames,
  111.             $canDownloadProductCsv,
  112.             $showWholesalePrice
  113.         );
  114.         return new Response(\json_encode($sessionResponse), Response::HTTP_OK, ['content-type' => 'application/json']);
  115.     }
  116. }