<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Class for handling redirections of old website.
*
* @author Adrian Bobowicz <adrian.bobowicz@makolab.com>
*/
class Redirect404Subscriber implements EventSubscriberInterface
{
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::EXCEPTION => 'onKernelException',
];
}
/**
* Method that being run while kernel exception.
*
* This is where 404 exceptions are filtered based on the pattern in the uri of the old site, so that the user
* will be redirected to a similar page of the new site.
*/
public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
if ($exception instanceof NotFoundHttpException) {
$uri = $event->getRequest()->getRequestUri();
//404
if (in_array($uri, ['/products', '/parcel-lockers', '/contact', '/about-us', '/', ''])
|| preg_match('/^\/(en|uk|ru|pl)\/career\/index\?.*$/', $uri)
|| preg_match('/^\/(en|uk|ru|pl)\/news\?.*$/', $uri)) {
return;
}
$response = $this->parcelLockersResponse($uri)
?? $this->productResponse($uri)
?? $this->contactResponse($uri)
?? $this->aboutUsResponse($uri)
?? $this->baseResponse($uri);
if ($response) {
$event->setResponse($response);
}
}
}
/**
* Product redirections.
*/
private function productResponse(string $uri): ?Response
{
if ('/en/category' === $uri) {
return new RedirectResponse('/products', Response::HTTP_MOVED_PERMANENTLY);
}
if (preg_match('/^\/(en|pl|uk|ru)\/(category|product|engineering|mint).*$/', $uri)) {
return new RedirectResponse('/products', Response::HTTP_FOUND);
}
return null;
}
/**
* Parcel Lockers redirections.
*/
private function parcelLockersResponse(string $uri): ?Response
{
if (preg_match('/^\/(en|uk|ru)\/category\/postovi-terminali$/', $uri)) {
return new RedirectResponse('/parcel-lockers', Response::HTTP_MOVED_PERMANENTLY);
}
if ('pl/category/postovi-terminali' === $uri) {
return new RedirectResponse('/parcel-lockers', Response::HTTP_FOUND);
}
return null;
}
/**
* Contact redirections.
*/
private function contactResponse(string $uri): ?Response
{
if (preg_match('/^\/(en|ru)\/contacts$/', $uri)) {
return new RedirectResponse('/contact', Response::HTTP_MOVED_PERMANENTLY);
}
if (preg_match('/^\/(uk|pl)\/contacts$/', $uri)) {
return new RedirectResponse('/contact', Response::HTTP_FOUND);
}
return null;
}
/**
* About us redirections.
*/
private function aboutUsResponse(string $uri): ?Response
{
if (preg_match('/^\/(en|pl|ru|uk)\/news/', $uri)) {
return new RedirectResponse('/about-us', Response::HTTP_FOUND);
}
return null;
}
/**
* Base redirections.
*/
private function baseResponse(string $uri): ?Response
{
if (in_array(
$uri,
[
'/en',
'/en/',
'/en/career',
'/en/career/index',
'/en/modern-retail-forum',
'/en/quality',
'/en/site/privacy-policy',
'/pl',
'/pl/career',
'/pl/career/index',
'/pl/modern-retail-forum',
'/pl/quality',
'/pl/site/privacy-policy',
'/ru',
'/ru/',
'/ru/career',
'/ru/career/index',
'/ru/modern-retail-forum',
'/ru/quality',
'/ru/site/privacy-policy',
'/ru/squares',
'/uk',
'/uk/career',
'/uk/modern-retail-forum',
'/uk/quality',
'/uk/site/privacy-policy',
'/uk/squares',
'/pl/',
]
)) {
return new RedirectResponse('/', Response::HTTP_FOUND);
}
return null;
}
}