src/EventSubscriber/Redirect404Subscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  7. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. /**
  10.  * Class for handling redirections of old website.
  11.  *
  12.  * @author Adrian Bobowicz <adrian.bobowicz@makolab.com>
  13.  */
  14. class Redirect404Subscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * {@inheritDoc}
  18.      */
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             KernelEvents::EXCEPTION => 'onKernelException',
  23.         ];
  24.     }
  25.     /**
  26.      * Method that being run while kernel exception.
  27.      *
  28.      * This is where 404 exceptions are filtered based on the pattern in the uri of the old site, so that the user
  29.      * will be redirected to a similar page of the new site.
  30.      */
  31.     public function onKernelException(ExceptionEvent $event): void
  32.     {
  33.         $exception $event->getThrowable();
  34.         if ($exception instanceof NotFoundHttpException) {
  35.             $uri $event->getRequest()->getRequestUri();
  36.             //404
  37.             if (in_array($uri, ['/products''/parcel-lockers''/contact''/about-us''/'''])
  38.                 || preg_match('/^\/(en|uk|ru|pl)\/career\/index\?.*$/'$uri)
  39.                 || preg_match('/^\/(en|uk|ru|pl)\/news\?.*$/'$uri)) {
  40.                 return;
  41.             }
  42.             $response $this->parcelLockersResponse($uri)
  43.                 ?? $this->productResponse($uri)
  44.                 ?? $this->contactResponse($uri)
  45.                 ?? $this->aboutUsResponse($uri)
  46.                 ?? $this->baseResponse($uri);
  47.             if ($response) {
  48.                 $event->setResponse($response);
  49.             }
  50.         }
  51.     }
  52.     /**
  53.      * Product redirections.
  54.      */
  55.     private function productResponse(string $uri): ?Response
  56.     {
  57.         if ('/en/category' === $uri) {
  58.             return new RedirectResponse('/products'Response::HTTP_MOVED_PERMANENTLY);
  59.         }
  60.         if (preg_match('/^\/(en|pl|uk|ru)\/(category|product|engineering|mint).*$/'$uri)) {
  61.             return new RedirectResponse('/products'Response::HTTP_FOUND);
  62.         }
  63.         return null;
  64.     }
  65.     /**
  66.      * Parcel Lockers redirections.
  67.      */
  68.     private function parcelLockersResponse(string $uri): ?Response
  69.     {
  70.         if (preg_match('/^\/(en|uk|ru)\/category\/postovi-terminali$/'$uri)) {
  71.             return new RedirectResponse('/parcel-lockers'Response::HTTP_MOVED_PERMANENTLY);
  72.         }
  73.         if ('pl/category/postovi-terminali' === $uri) {
  74.             return new RedirectResponse('/parcel-lockers'Response::HTTP_FOUND);
  75.         }
  76.         return null;
  77.     }
  78.     /**
  79.      * Contact redirections.
  80.      */
  81.     private function contactResponse(string $uri): ?Response
  82.     {
  83.         if (preg_match('/^\/(en|ru)\/contacts$/'$uri)) {
  84.             return new RedirectResponse('/contact'Response::HTTP_MOVED_PERMANENTLY);
  85.         }
  86.         if (preg_match('/^\/(uk|pl)\/contacts$/'$uri)) {
  87.             return new RedirectResponse('/contact'Response::HTTP_FOUND);
  88.         }
  89.         return null;
  90.     }
  91.     /**
  92.      * About us redirections.
  93.      */
  94.     private function aboutUsResponse(string $uri): ?Response
  95.     {
  96.         if (preg_match('/^\/(en|pl|ru|uk)\/news/'$uri)) {
  97.             return new RedirectResponse('/about-us'Response::HTTP_FOUND);
  98.         }
  99.         return null;
  100.     }
  101.     /**
  102.      * Base redirections.
  103.      */
  104.     private function baseResponse(string $uri): ?Response
  105.     {
  106.         if (in_array(
  107.             $uri,
  108.             [
  109.                 '/en',
  110.                 '/en/',
  111.                 '/en/career',
  112.                 '/en/career/index',
  113.                 '/en/modern-retail-forum',
  114.                 '/en/quality',
  115.                 '/en/site/privacy-policy',
  116.                 '/pl',
  117.                 '/pl/career',
  118.                 '/pl/career/index',
  119.                 '/pl/modern-retail-forum',
  120.                 '/pl/quality',
  121.                 '/pl/site/privacy-policy',
  122.                 '/ru',
  123.                 '/ru/',
  124.                 '/ru/career',
  125.                 '/ru/career/index',
  126.                 '/ru/modern-retail-forum',
  127.                 '/ru/quality',
  128.                 '/ru/site/privacy-policy',
  129.                 '/ru/squares',
  130.                 '/uk',
  131.                 '/uk/career',
  132.                 '/uk/modern-retail-forum',
  133.                 '/uk/quality',
  134.                 '/uk/site/privacy-policy',
  135.                 '/uk/squares',
  136.                 '/pl/',
  137.             ]
  138.         )) {
  139.             return new RedirectResponse('/'Response::HTTP_FOUND);
  140.         }
  141.         return null;
  142.     }
  143. }