src/Social/InternalBundle/Service/ExceptionListener.php line 68

Open in your IDE?
  1. <?php
  2. namespace Social\InternalBundle\Service;
  3. use Sentry\ClientInterface;
  4. use Social\FrontendBundle\Service\ThemeManager;
  5. use Symfony\Bundle\TwigBundle\TwigEngine;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  9. /**
  10.  * Class ExceptionListener
  11.  *
  12.  * @package Social\InternalBundle\Service
  13.  */
  14. class ExceptionListener
  15. {
  16.     /**
  17.      * @var ThemeManager $themeManager
  18.      */
  19.     protected $themeManager;
  20.     /**
  21.      * @var RequestStack
  22.      */
  23.     protected $currentRequest;
  24.     /**
  25.      * @var TwigEngine $templating
  26.      */
  27.     protected $templating;
  28.     /**
  29.      * @var null|string
  30.      */
  31.     protected $projectDir null;
  32.     /**
  33.      * @var ClientInterface
  34.      */
  35.     private $sentry;
  36.     /**
  37.      * ExceptionListener constructor.
  38.      *
  39.      * @param ThemeManager    $manager
  40.      * @param RequestStack    $request
  41.      * @param TwigEngine      $twigEngine
  42.      * @param                 $projectDir
  43.      */
  44.     public function __construct(
  45.         ThemeManager $manager,
  46.         RequestStack $request,
  47.         TwigEngine $twigEngine,
  48.         $projectDir,
  49.         ClientInterface $sentry
  50.     ) {
  51.         $this->themeManager   $manager;
  52.         $this->currentRequest $request;
  53.         $this->templating     $twigEngine;
  54.         $this->projectDir     $projectDir;
  55.         $this->sentry $sentry;
  56.     }
  57.     /**
  58.      * @param GetResponseForExceptionEvent $event
  59.      */
  60.     public function onException(GetResponseForExceptionEvent $event)
  61.     {
  62.         try {
  63.             // Getting the status code
  64.             $statusCodeException null;
  65.             if ($event->getException() instanceof \Exception) {
  66.                 if (method_exists($event->getException(), 'getStatusCode')) {
  67.                     $statusCodeException $event->getException()->getStatusCode();
  68.                 }
  69.                 if (method_exists($event->getException(), 'getCode')) {
  70.                     $statusCodeException $event->getException()->getCode();
  71.                 }
  72.                 $this->sentry->captureException($event->getException());
  73.             }
  74.             $defaultErrorPage    'error.html.twig';
  75.             $defaultNotFoundPage 'error404.html.twig';
  76.             $pathToErrorFiles    'Resources/TwigBundle/views/Exception/';
  77.             $themeSettings       $this->themeManager->getActiveTheme();
  78.             $siteName            strtolower($themeSettings['site_name']);
  79.             $logo                $themeSettings['logo'];
  80.             $currentThemeName $themeSettings['name'] ? $themeSettings['name'] : null;
  81.             // 404 / 403 page
  82.             if (in_array($statusCodeException, [404403])) {
  83.                 if (file_exists($this->projectDir DIRECTORY_SEPARATOR $pathToErrorFiles 'error404_' $currentThemeName '.html.twig')) {
  84.                     $response $this->templating->render($this->projectDir DIRECTORY_SEPARATOR $pathToErrorFiles 'error404_' $currentThemeName '.html.twig',
  85.                         [
  86.                             'siteName' => strtolower(trim($siteName)),
  87.                             'logo'     => strtolower(trim($logo)),
  88.                         ]);
  89.                     $event->setResponse(new Response($response404));
  90.                 } else {
  91.                     $response $this->templating->render($this->projectDir DIRECTORY_SEPARATOR $pathToErrorFiles $defaultNotFoundPage,
  92.                         [
  93.                             'siteName' => strtolower(trim($siteName)),
  94.                             'logo'     => strtolower(trim($logo)),
  95.                         ]);
  96.                     $event->setResponse(new Response($response404));
  97.                 }
  98.             } else {
  99.                 if (file_exists($this->projectDir DIRECTORY_SEPARATOR $pathToErrorFiles 'error_' $currentThemeName '.html.twig')) {
  100.                     $response $this->templating->render($this->projectDir DIRECTORY_SEPARATOR $pathToErrorFiles 'error404_' $currentThemeName '.html.twig',
  101.                         [
  102.                             'siteName' => strtolower(trim($siteName)),
  103.                             'logo'     => strtolower(trim($logo)),
  104.                         ]);
  105.                     $event->setResponse(new Response($response404));
  106.                 } else {
  107.                     $response $this->templating->render($this->projectDir DIRECTORY_SEPARATOR $pathToErrorFiles $defaultErrorPage,
  108.                         [
  109.                             'siteName' => strtolower(trim($siteName)),
  110.                             'logo'     => strtolower(trim($logo)),
  111.                         ]);
  112.                     $event->setResponse(new Response($response404));
  113.                 }
  114.             }
  115.         } catch (\Exception $exception) {
  116.             $this->sentry->captureException($exception);
  117.         }
  118.     }
  119. }