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

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 $sentryClient
  34.      */
  35.     protected $sentryClient null;
  36.     /**
  37.      * ExceptionListener constructor.
  38.      *
  39.      * @param ThemeManager    $manager
  40.      * @param RequestStack    $request
  41.      * @param TwigEngine      $twigEngine
  42.      * @param                 $projectDir
  43.      * @param ClientInterface $sentryClient
  44.      */
  45.     public function __construct(
  46.         ThemeManager $manager,
  47.         RequestStack $request,
  48.         TwigEngine $twigEngine,
  49.         $projectDir,
  50.         ClientInterface $sentryClient
  51.     ) {
  52.         $this->themeManager   $manager;
  53.         $this->currentRequest $request;
  54.         $this->templating     $twigEngine;
  55.         $this->projectDir     $projectDir;
  56.         $this->sentryClient   $sentryClient;
  57.     }
  58.     /**
  59.      * @param GetResponseForExceptionEvent $event
  60.      */
  61.     public function onException(GetResponseForExceptionEvent $event)
  62.     {
  63.         try {
  64.             // Getting the status code
  65.             $statusCodeException null;
  66.             if ($event->getException() instanceof \Exception && method_exists($event->getException(),
  67.                     'getStatusCode')) {
  68.                 $statusCodeException $event->getException()->getStatusCode();
  69.                 $this->sentryClient->captureException($event->getException());
  70.             }
  71.             $defaultErrorPage    'error.html.twig';
  72.             $defaultNotFoundPage 'error404.html.twig';
  73.             $pathToErrorFiles    'Resources/TwigBundle/views/Exception/';
  74.             $themeSettings       $this->themeManager->getActiveTheme();
  75.             $siteName            strtolower($themeSettings['site_name']);
  76.             $logo                $themeSettings['logo'];
  77.             $currentThemeName $themeSettings['name'] ? $themeSettings['name'] : null;
  78.             // 404 / 403 page
  79.             if (in_array($statusCodeException, [404403])) {
  80.                 if (file_exists($this->projectDir DIRECTORY_SEPARATOR $pathToErrorFiles 'error404_' $currentThemeName '.html.twig')) {
  81.                     $response $this->templating->render($this->projectDir DIRECTORY_SEPARATOR $pathToErrorFiles 'error404_' $currentThemeName '.html.twig',
  82.                         [
  83.                             'siteName' => strtolower(trim($siteName)),
  84.                             'logo'     => strtolower(trim($logo)),
  85.                         ]);
  86.                     $event->setResponse(new Response($response404));
  87.                 } else {
  88.                     $response $this->templating->render($this->projectDir DIRECTORY_SEPARATOR $pathToErrorFiles $defaultNotFoundPage,
  89.                         [
  90.                             'siteName' => strtolower(trim($siteName)),
  91.                             'logo'     => strtolower(trim($logo)),
  92.                         ]);
  93.                     $event->setResponse(new Response($response404));
  94.                 }
  95.             } else {
  96.                 if (file_exists($this->projectDir DIRECTORY_SEPARATOR $pathToErrorFiles 'error_' $currentThemeName '.html.twig')) {
  97.                     $response $this->templating->render($this->projectDir DIRECTORY_SEPARATOR $pathToErrorFiles 'error404_' $currentThemeName '.html.twig',
  98.                         [
  99.                             'siteName' => strtolower(trim($siteName)),
  100.                             'logo'     => strtolower(trim($logo)),
  101.                         ]);
  102.                     $event->setResponse(new Response($response404));
  103.                 } else {
  104.                     $response $this->templating->render($this->projectDir DIRECTORY_SEPARATOR $pathToErrorFiles $defaultErrorPage,
  105.                         [
  106.                             'siteName' => strtolower(trim($siteName)),
  107.                             'logo'     => strtolower(trim($logo)),
  108.                         ]);
  109.                     $event->setResponse(new Response($response404));
  110.                 }
  111.             }
  112.         } catch (\Exception $exception) {
  113.             $this->sentryClient->captureException($exception);
  114.         }
  115.     }
  116. }