src/EventSubscriber/Seo/RedirectSubscriber.php line 122

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Seo;
  3. use App\Repository\Seo\R301Repository;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  6. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Cookie;
  9. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  10. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\Routing\RouterInterface;
  13. class RedirectSubscriber implements EventSubscriberInterface
  14. {
  15.     private $r301Repository;
  16.     private $router;
  17.     private $protocole;
  18.     private $productionUrl;
  19.     /**
  20.      * RedirectSubscriber constructor.
  21.      * @param R301Repository $r301Repository
  22.      * @param RouterInterface $router
  23.      * @param $protocole
  24.      * @param $productionUrl
  25.      */
  26.     public function __construct(R301Repository $r301RepositoryRouterInterface $router$protocole$productionUrl)
  27.     {
  28.         $this->r301Repository   $r301Repository;
  29.         $this->router           $router;
  30.         $this->protocole        $protocole;
  31.         $this->productionUrl    $productionUrl;
  32.     }
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [
  36.             KernelEvents::REQUEST   => 'onKernelRequest',
  37.             KernelEvents::RESPONSE  => 'onKernelResponse',
  38.             KernelEvents::EXCEPTION => 'onKernelException',
  39.         ];
  40.     }
  41.     /**
  42.      * @param GetResponseEvent $event
  43.      */
  44.     public function onKernelRequest(GetResponseEvent $event)
  45.     {
  46.         if (!$event->isMasterRequest()) {
  47.             return;
  48.         }
  49.         $request    $event->getRequest();
  50.         $host       $request->getHost();
  51.         // Redirection de tout lien HTTP vers HTTPS
  52.         if (!$request->isSecure() && 'https' == $this->protocole) {
  53.             $nouvelle_url str_replace('//''/'$request->getRequestUri());
  54.             $array_redirection self::getRedirections();
  55.             if (array_key_exists($nouvelle_url$array_redirection)) {
  56.                 $nouvelle_url $array_redirection[$nouvelle_url];
  57.             }
  58.             $response = new RedirectResponse($this->protocole.'://'.$host.$nouvelle_url301);
  59.             $event->setResponse($response);
  60.         }
  61.     }
  62.     /**
  63.      * @var FilterResponseEvent $event
  64.      * @return null
  65.      */
  66.     public function onKernelResponse(FilterResponseEvent $event): void
  67.     {
  68.         if (!$event->isMasterRequest()) {
  69.             return;
  70.         }
  71.     }
  72.     /**
  73.      * @var GetResponseForExceptionEvent $event
  74.      * @return null
  75.      */
  76.     public function onKernelException(GetResponseForExceptionEvent $event): void
  77.     {
  78.         $request    $event->getRequest();
  79.         $host       $request->getHost();
  80.         $pathInfo   $request->getPathInfo();
  81.         $pathInfo   explode('/'$pathInfo);
  82.         $slug       end($pathInfo);
  83.         $pathInfo   array_slice($pathInfo0count($pathInfo) - 1);
  84.         $pathInfo   implode('/'$pathInfo);
  85.         $r301       $this->r301Repository->findOneBySlugFrom($slug);
  86.         if (!is_null($r301)) {
  87.             $redirectUrl $host '/' $pathInfo '/' $r301->getSlugTo();
  88.             $redirectUrl str_replace('//''/'$redirectUrl);
  89.             $response = new RedirectResponse($this->protocole '://' $redirectUrl301);
  90.             $event->setResponse($response);
  91.         }
  92.         $array_redirection self::getRedirections();
  93.         if (isset($array_redirection[$request->getRequestUri()])) {
  94.             $nouvelle_url $array_redirection[$request->getRequestUri()];
  95.             $response = new RedirectResponse($this->protocole.'://'.$host.$nouvelle_url301);
  96.             $event->setResponse($response);
  97.         }
  98.         // If not a HttpNotFoundException ignore
  99.         if (!$event->getException() instanceof NotFoundHttpException) {
  100.             return;
  101.         }
  102.     }
  103.     private function getRedirections()
  104.     {
  105.         $array = [];
  106.         return $array;
  107.     }
  108. }