src/EventSubscriber/KernelSubscriber.php line 33

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber;
  4. use App\Entity\CommunicationLog;
  5. use App\Entity\Property;
  6. use App\Entity\Redirect;
  7. use App\Entity\User;
  8. use App\Exception\BadSecurityTokenException;
  9. use App\Repository\RedirectRepository;
  10. use App\Service\LogService;
  11. use App\Service\PropertyService;
  12. use App\Service\UserService;
  13. use Symfony\Bundle\SecurityBundle\Security;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpKernel\Event\RequestEvent;
  17. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  18. class KernelSubscriber implements EventSubscriberInterface
  19. {
  20.     private RedirectRepository $redirectRepository;
  21.     public function __construct(
  22.         RedirectRepository $redirectRepository
  23.     )
  24.     {
  25.         $this->redirectRepository $redirectRepository;
  26.     }
  27.     public function onKernelRequest(RequestEvent $event)
  28.     {
  29.         if (!$event->isMasterRequest()) {
  30.             return;
  31.         }
  32.         $path ltrim($event->getRequest()->getPathInfo(),'/');
  33.         $path rtrim($path,'/');
  34.         $redirect $this->redirectRepository->findOneBy(['source' => $path]);
  35.         if (!$redirect instanceof Redirect) {
  36.             return;
  37.         }
  38.         $targetUrl 'https://vsechnykurzy.cz/'.ltrim($redirect->getTarget(),"/");
  39.         $event->setResponse(new RedirectResponse($targetUrl));
  40.     }
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return [
  44.             'kernel.request' => ['onKernelRequest'300],
  45.         ];
  46.     }
  47. }