src/EventSubscriber/RequestSubscribe.php line 41

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author <akartis-dev>
  4.  */
  5. namespace App\EventSubscriber;
  6. use App\Service\Constant\CookiesConstant;
  7. use App\Service\Logs\LogsService;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Cookie;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Event\RequestEvent;
  13. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  14. class RequestSubscribe implements EventSubscriberInterface
  15. {
  16.     public function __construct(
  17.         private array       $locale,
  18.         private LogsService $logsService
  19.     )
  20.     {
  21.     }
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             RequestEvent::class => 'onKernelRequest',
  26.             ResponseEvent::class => 'onKernelResponse'
  27.         ];
  28.     }
  29.     /**
  30.      * Kernel request to force locale in url if request has no locale
  31.      * Assign default Navigator id with cookie to handle logs
  32.      *
  33.      * @param RequestEvent $event
  34.      */
  35.     public function onKernelRequest(RequestEvent $event): void
  36.     {
  37.         $request $event->getRequest();
  38.         $redirect $this->checkLocale($request);
  39.         if ($redirect) {
  40.             $response = new RedirectResponse($redirect);
  41.             $event->setResponse($response);
  42.         }
  43.     }
  44.     public function onKernelResponse(ResponseEvent $event)
  45.     {
  46.         if ($event->isMainRequest()) {
  47.             $request $event->getRequest();
  48.             $expireCookieTime time() + (60 24 30); // 1months
  49.             $response $event->getResponse();
  50.             $navId $request->cookies->get(CookiesConstant::navId);
  51.             if (!$navId) {
  52.                 $response->headers->setCookie(
  53.                     Cookie::create(
  54.                         nameCookiesConstant::navId,
  55.                         value$this->logsService->createCookiesId(),
  56.                         expire$expireCookieTime,
  57.                     )
  58.                 );
  59.             }
  60.             $event->setResponse($response);
  61.         }
  62.     }
  63.     /**
  64.      * Return url to redirect with local or null
  65.      *
  66.      * @param Request $request
  67.      * @return string|null
  68.      */
  69.     private function checkLocale(Request $request): ?string
  70.     {
  71.         $locale array_keys($this->locale);
  72.         $method $request->getMethod();
  73.         if (Request::METHOD_GET === $method && !in_array($request->getLocale(), $localetrue)) {
  74.             $defaultLocal "fr";
  75.             return sprintf("/%s%s"$defaultLocal$request->getRequestUri());
  76.         }
  77.         return null;
  78.     }
  79. }