<?php
namespace App\EventSubscriber\Seo;
use App\Repository\Seo\R301Repository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouterInterface;
class RedirectSubscriber implements EventSubscriberInterface
{
private $r301Repository;
private $router;
private $protocole;
private $productionUrl;
/**
* RedirectSubscriber constructor.
* @param R301Repository $r301Repository
* @param RouterInterface $router
* @param $protocole
* @param $productionUrl
*/
public function __construct(R301Repository $r301Repository, RouterInterface $router, $protocole, $productionUrl)
{
$this->r301Repository = $r301Repository;
$this->router = $router;
$this->protocole = $protocole;
$this->productionUrl = $productionUrl;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
KernelEvents::RESPONSE => 'onKernelResponse',
KernelEvents::EXCEPTION => 'onKernelException',
];
}
/**
* @param GetResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$request = $event->getRequest();
$host = $request->getHost();
// Redirection de tout lien HTTP vers HTTPS
if (!$request->isSecure() && 'https' == $this->protocole) {
$nouvelle_url = str_replace('//', '/', $request->getRequestUri());
$array_redirection = self::getRedirections();
if (array_key_exists($nouvelle_url, $array_redirection)) {
$nouvelle_url = $array_redirection[$nouvelle_url];
}
$response = new RedirectResponse($this->protocole.'://'.$host.$nouvelle_url, 301);
$event->setResponse($response);
}
}
/**
* @var FilterResponseEvent $event
* @return null
*/
public function onKernelResponse(FilterResponseEvent $event): void
{
if (!$event->isMasterRequest()) {
return;
}
}
/**
* @var GetResponseForExceptionEvent $event
* @return null
*/
public function onKernelException(GetResponseForExceptionEvent $event): void
{
$request = $event->getRequest();
$host = $request->getHost();
$pathInfo = $request->getPathInfo();
$pathInfo = explode('/', $pathInfo);
$slug = end($pathInfo);
$pathInfo = array_slice($pathInfo, 0, count($pathInfo) - 1);
$pathInfo = implode('/', $pathInfo);
$r301 = $this->r301Repository->findOneBySlugFrom($slug);
if (!is_null($r301)) {
$redirectUrl = $host . '/' . $pathInfo . '/' . $r301->getSlugTo();
$redirectUrl = str_replace('//', '/', $redirectUrl);
$response = new RedirectResponse($this->protocole . '://' . $redirectUrl, 301);
$event->setResponse($response);
}
$array_redirection = self::getRedirections();
if (isset($array_redirection[$request->getRequestUri()])) {
$nouvelle_url = $array_redirection[$request->getRequestUri()];
$response = new RedirectResponse($this->protocole.'://'.$host.$nouvelle_url, 301);
$event->setResponse($response);
}
// If not a HttpNotFoundException ignore
if (!$event->getException() instanceof NotFoundHttpException) {
return;
}
}
private function getRedirections()
{
$array = [];
return $array;
}
}