src/EventSubscriber/LocalSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber;
  4. use App\Helper\Image\ImageInterface;
  5. use App\Repository\Franchise\DomainRepository;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. class LocalSubscriber implements EventSubscriberInterface
  12. {
  13.     public function __construct(
  14.         private string $mainUrl,
  15.         private DomainRepository $domainRepository,
  16.     ) {
  17.     }
  18.     public static function getSubscribedEvents()
  19.     {
  20.         return [KernelEvents::REQUEST => [['onKernelRequest'20]]];
  21.     }
  22.     public function onKernelRequest(RequestEvent $event): void
  23.     {
  24.         $request $event->getRequest();
  25.         $locale $request->getSession()->get('_locale');
  26.         $host $request->server->get('HTTP_HOST');
  27.         if (null !== $locale && '' !== trim($locale)) {
  28.             $request->attributes->set('_locale'$locale);
  29.         }
  30.         if (ImageInterface::IMAGE_CROP_PATH_ROUTE_NAME === $request->get('_route')
  31.             && false === str_ends_with($host$this->mainUrl)
  32.             && false === $this->domainRepository->exist($host)
  33.         ) {
  34.             throw new UnauthorizedHttpException(Response::$statusTexts[Response::HTTP_UNAUTHORIZED]);
  35.         }
  36.     }
  37. }