core/lib/Thelia/Action/HttpException.php line 41

  1. <?php
  2. /*
  3.  * This file is part of the Thelia package.
  4.  * http://www.thelia.net
  5.  *
  6.  * (c) OpenStudio <info@thelia.net>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Thelia\Action;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  14. use Symfony\Component\HttpKernel\Exception\HttpException as BaseHttpException;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. use Thelia\Core\HttpFoundation\Response;
  18. use Thelia\Core\Template\ParserInterface;
  19. use Thelia\Exception\AdminAccessDenied;
  20. use Thelia\Model\ConfigQuery;
  21. /**
  22.  * Class HttpException.
  23.  *
  24.  * @author Etienne Roudeix <eroudeix@openstudio.fr>
  25.  * @author Manuel Raynaud  <manu@raynaud.io>
  26.  */
  27. class HttpException extends BaseAction implements EventSubscriberInterface
  28. {
  29.     /** @var ParserInterface */
  30.     protected $parser;
  31.     public function __construct(ParserInterface $parser)
  32.     {
  33.         $this->parser $parser;
  34.     }
  35.     public function checkHttpException(ExceptionEvent $event): void
  36.     {
  37.         $exception $event->getThrowable();
  38.         if ($exception instanceof NotFoundHttpException) {
  39.             $this->display404($event);
  40.         }
  41.         if ($exception instanceof AdminAccessDenied) {
  42.             $this->displayAdminGeneralError($event);
  43.         }
  44.         if ($exception instanceof BaseHttpException && null === $event->getResponse()) {
  45.             $this->displayException($event);
  46.         }
  47.     }
  48.     protected function displayAdminGeneralError(ExceptionEvent $event): void
  49.     {
  50.         // Define the template thant shoud be used
  51.         $this->parser->setTemplateDefinition(
  52.             $this->parser->getTemplateHelper()->getActiveAdminTemplate()
  53.         );
  54.         $message $event->getThrowable()->getMessage();
  55.         $response = new Response(
  56.             $this->parser->render(
  57.                 'general_error.html',
  58.                 [
  59.                     'error_message' => $message,
  60.                 ]
  61.             ),
  62.             403
  63.         );
  64.         $event->setResponse($response);
  65.     }
  66.     protected function display404(ExceptionEvent $event): void
  67.     {
  68.         // Define the template thant shoud be used
  69.         $this->parser->setTemplateDefinition(
  70.             $this->parser->getTemplateHelper()->getActiveFrontTemplate()
  71.         );
  72.         $response = new Response($this->parser->render(ConfigQuery::getPageNotFoundView()), 404);
  73.         $event->setResponse($response);
  74.     }
  75.     protected function displayException(ExceptionEvent $event): void
  76.     {
  77.         /** @var \Symfony\Component\HttpKernel\Exception\HttpException $exception */
  78.         $exception $event->getThrowable();
  79.         $event->setResponse(
  80.             new Response(
  81.                 $exception->getMessage(),
  82.                 $exception->getStatusCode(),
  83.                 $exception->getHeaders()
  84.             )
  85.         );
  86.     }
  87.     /**
  88.      * {@inheritdoc}
  89.      */
  90.     public static function getSubscribedEvents()
  91.     {
  92.         return [
  93.             KernelEvents::EXCEPTION => ['checkHttpException'128],
  94.         ];
  95.     }
  96. }