vendor/store.shopware.com/acrisseoredirectcs/src/Subscriber/PageNotFoundExceptionSubscriber.php line 42

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\SeoRedirect\Subscriber;
  3. use Acris\SeoRedirect\Components\RedirectService;
  4. use Acris\SeoRedirect\Storefront\Framework\Routing\RequestTransformer;
  5. use Shopware\Core\Framework\Routing\RequestTransformerInterface;
  6. use Shopware\Core\System\SystemConfig\SystemConfigService;
  7. use Shopware\Storefront\Framework\Routing\Exception\SalesChannelMappingException;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. class PageNotFoundExceptionSubscriber implements EventSubscriberInterface
  15. {
  16.     private RedirectService $redirectService;
  17.     private SystemConfigService $configService;
  18.     private RequestTransformerInterface $storefrontRequestTransformer;
  19.     public function __construct(RedirectService $redirectServiceSystemConfigService $configServiceRequestTransformerInterface $storefrontRequestTransformer)
  20.     {
  21.         $this->redirectService $redirectService;
  22.         $this->configService $configService;
  23.         $this->storefrontRequestTransformer $storefrontRequestTransformer;
  24.     }
  25.     // -59, so that it is before RouterListener and in between to allow other possible listeners.
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             KernelEvents::EXCEPTION => [
  30.                 ['onKernelException', -59],
  31.             ],
  32.         ];
  33.     }
  34.     public function onKernelException(ExceptionEvent $event)
  35.     {
  36.         $redirectConfig $this->configService->get('AcrisSeoRedirectCS.config.redirectCheckPlace');
  37.         if ($redirectConfig == 'notFoundPage' && $this->isNotFoundPage($event->getThrowable())) {
  38.             $request $this->getTransformedRequest($event->getRequest());
  39.             $orgRequest Request::createFromGlobals();
  40.             if ($this->redirectService->checkRedirect($request$orgRequest)) {
  41.                 $event->setResponse(new RedirectResponse($request->attributes->get(RequestTransformer::REQUEST_ATTRIBUTE_SEO_REDIRECT_URL), $request->attributes->get(RequestTransformer::REQUEST_ATTRIBUTE_SEO_REDIRECT_CODE)));
  42.             }
  43.         }
  44.     }
  45.     private function isNotFoundPage(\Throwable $exception): bool
  46.     {
  47.         return !empty($exception) && method_exists($exception'getStatusCode') && !empty($exception->getStatusCode()) && $exception->getStatusCode() === Response::HTTP_NOT_FOUND;
  48.     }
  49.     private function getTransformedRequest(Request $orgRequest): Request
  50.     {
  51.         try {
  52.             $request $this->storefrontRequestTransformer->transform($orgRequest);
  53.         } catch (SalesChannelMappingException $salesChannelMappingException) {
  54.             $isRedirected $this->redirectService->checkRedirect($orgRequest$orgRequest);
  55.             if($isRedirected === true) {
  56.                 return $orgRequest;
  57.             }
  58.             // if not redirect, throw sales channel not found exception again
  59.             throw $salesChannelMappingException;
  60.         }
  61.         return $request;
  62.     }
  63. }