src/WellCommerce/Bundle/AppBundle/EventListener/ExceptionSubscriber.php line 52

Open in your IDE?
  1. <?php
  2. declare(strict_types=0);
  3. /*
  4.  * WellCommerce Foundation
  5.  *
  6.  * This file is part of the WellCommerce package.
  7.  *
  8.  * (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
  9.  *
  10.  * For the full copyright and license information,
  11.  * please view the LICENSE file that was distributed with this source code.
  12.  */
  13. namespace WellCommerce\Bundle\AppBundle\EventListener;
  14. use Doctrine\Common\Util\Debug;
  15. use Doctrine\ORM\QueryBuilder;
  16. use Psr\Container\ContainerInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpFoundation\RedirectResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  22. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  23. use Symfony\Component\HttpKernel\KernelEvents;
  24. use WellCommerce\Bundle\AppBundle\Entity\Redirect;
  25. use WellCommerce\Bundle\CoreBundle\DependencyInjection\AbstractServiceSubscriber;
  26. use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
  27. /**
  28.  * Class ExceptionSubscriber
  29.  *
  30.  * @author  Adam Piotrowski <adam@wellcommerce.org>
  31.  */
  32. class ExceptionSubscriber extends AbstractServiceSubscriber implements EventSubscriberInterface
  33. {
  34.     protected $breadcrumbs;
  35.     public function __construct(ContainerInterface $locatorBreadcrumbs $breadcrumbs)
  36.     {
  37.         parent::__construct($locator);
  38.         $this->breadcrumbs $breadcrumbs;
  39.     }
  40.     public static function getSubscribedEvents()
  41.     {
  42.         return [
  43.             KernelEvents::EXCEPTION => ['onKernelException'0],
  44.         ];
  45.     }
  46.     public function onKernelException(ExceptionEvent $event)
  47.     {
  48.         if (false === $this->getKernel()->isDebug()) {
  49.             $this->getThemeStorage()->setCurrentTheme($this->getShopStorage()->getCurrentShop()->getTheme());
  50.             $exception  $event->getThrowable();
  51.             $request    $event->getRequest();
  52.             $pathInfo   $request->getPathInfo();
  53.             $requestUri $request->getRequestUri();
  54.             if (substr($pathInfo, -11) === '/') {
  55.                 $url str_replace($pathInfortrim($pathInfo' /'), $requestUri);
  56.                 $response = new RedirectResponse($url301);
  57.                 $event->setResponse($response);
  58.             } else {
  59.                 $redirect $this->getRedirect($request);
  60.                 if ($redirect instanceof Redirect) {
  61.                     $response = new RedirectResponse($redirect->getTarget(), $redirect->getStatus());
  62.                     $event->setResponse($response);
  63.                 } else {
  64.                     $this->breadcrumbs->clear();
  65.                     $this->breadcrumbs->addRouteItem('homepage.heading.index''front.home_page.index', [], 0);
  66.                     if ($exception instanceof HttpExceptionInterface) {
  67.                         $this->breadcrumbs->addItem('Niepoprawny adres strony');
  68.                         $content $this->getTemplatingHelper()->render('WellCommerceAppBundle:Front/Exception:404.html.twig', [
  69.                             'message'   => $exception->getMessage(),
  70.                             'code'      => $exception->getCode(),
  71.                             'exception' => $exception,
  72.                         ]);
  73.                     } else {
  74.                         $this->breadcrumbs->addItem('Błąd strony');
  75.                         $content $this->getTemplatingHelper()->render('WellCommerceAppBundle:Front/Exception:500.html.twig', [
  76.                             'message'   => $exception->getMessage(),
  77.                             'code'      => $exception->getCode(),
  78.                             'exception' => $exception,
  79.                         ]);
  80.                     }
  81.                     $response = new Response();
  82.                     $response->setContent($content);
  83.                     if ($exception instanceof HttpExceptionInterface) {
  84.                         $response->setStatusCode($exception->getStatusCode());
  85.                         $response->headers->replace($exception->getHeaders());
  86.                     } else {
  87.                         $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
  88.                     }
  89.                     $response->headers->set('X-Robots-Tag''noindex'true);
  90.                     $event->setResponse($response);
  91.                 }
  92.             }
  93.         }
  94.     }
  95.     public function getRedirect(Request $request): ?Redirect
  96.     {
  97.         $path       urldecode($request->getPathInfo());
  98.         $repository $this->getEntityManager()->getRepository(Redirect::class);
  99.         /** @var QueryBuilder $queryBuilder */
  100.         $queryBuilder $repository->createQueryBuilder('redirect');
  101.         $queryBuilder->andWhere('REGEXP(:path, redirect.source) = true');
  102.         $queryBuilder->orderBy('redirect.hierarchy''ASC');
  103.         $queryBuilder->setParameter('path'$path);
  104.         $query  $queryBuilder->getQuery();
  105.         $result $query->getResult();
  106.         if (!empty($result)) {
  107.             if (count($result) === 1) {
  108.                 return current($result);
  109.             }
  110.             /** @var Redirect $redirect */
  111.             foreach ($result as $redirect) {
  112.                 if ($redirect->getSource() === $path) {
  113.                     return $redirect;
  114.                 }
  115.             }
  116.         }
  117.         return null;
  118.     }
  119. }