Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
35 / 35 |
|
6.67% |
1 / 15 |
|
4.76% |
1 / 21 |
|
100.00% |
2 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ExceptionListener | |
100.00% |
35 / 35 |
|
6.67% |
1 / 15 |
|
4.76% |
1 / 21 |
|
100.00% |
2 / 2 |
63.29 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| onKernelException | |
100.00% |
32 / 32 |
|
0.00% |
0 / 14 |
|
0.00% |
0 / 20 |
|
100.00% |
1 / 1 |
7 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Listener; |
| 4 | |
| 5 | use App\Application; |
| 6 | use Psr\Log\LoggerInterface; |
| 7 | use Symfony\Component\EventDispatcher\Attribute\AsEventListener; |
| 8 | use Symfony\Component\HttpFoundation\Response; |
| 9 | use Symfony\Component\HttpKernel\Event\ExceptionEvent; |
| 10 | use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
| 11 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 12 | use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException; |
| 13 | use Twig\Environment; |
| 14 | |
| 15 | class ExceptionListener |
| 16 | { |
| 17 | protected Environment $twig; |
| 18 | protected Application $application; |
| 19 | protected LoggerInterface $logger; |
| 20 | |
| 21 | public function __construct(Environment $twig, Application $application, LoggerInterface $applicationLogger) |
| 22 | { |
| 23 | $this->twig = $twig; |
| 24 | $this->application = $application; |
| 25 | $this->logger = $applicationLogger; |
| 26 | } |
| 27 | |
| 28 | #[AsEventListener()] |
| 29 | public function onKernelException(ExceptionEvent $event): void |
| 30 | { |
| 31 | if ( |
| 32 | ('prod' === $this->application->getEnvironment() || 'test' === $this->application->getEnvironment()) |
| 33 | && !$this->application->isDebug() |
| 34 | ) { |
| 35 | if ($event->getThrowable() instanceof NotFoundHttpException) { |
| 36 | $view = $this->twig->render('bundles/TwigBundle/Exception/error404.html.twig'); |
| 37 | $this->logger->debug('Page not found', ['message' => $event->getThrowable()->getMessage()]); |
| 38 | $response = new Response($view, 404); |
| 39 | } elseif ($event->getThrowable() instanceof AccessDeniedHttpException) { |
| 40 | $view = $this->twig->render('bundles/TwigBundle/Exception/error403.html.twig'); |
| 41 | $this->logger->debug('Page access denied', ['message' => $event->getThrowable()->getMessage()]); |
| 42 | $response = new Response($view, 403); |
| 43 | } elseif ($event->getThrowable() instanceof ServiceUnavailableHttpException) { |
| 44 | $view = $this->twig->render('bundles/TwigBundle/Exception/error503.html.twig'); |
| 45 | $this->logger->error( |
| 46 | 'Error 503', |
| 47 | [ |
| 48 | 'message' => $event->getThrowable()->getMessage(), |
| 49 | 'file' => $event->getThrowable()->getFile(), |
| 50 | 'line' => $event->getThrowable()->getLine(), |
| 51 | ] |
| 52 | ); |
| 53 | $response = new Response($view, 503); |
| 54 | } else { |
| 55 | $view = $this->twig->render('bundles/TwigBundle/Exception/error500.html.twig'); |
| 56 | $this->logger->error( |
| 57 | 'Error 500', |
| 58 | [ |
| 59 | 'message' => $event->getThrowable()->getMessage(), |
| 60 | 'file' => $event->getThrowable()->getFile(), |
| 61 | 'line' => $event->getThrowable()->getLine(), |
| 62 | ] |
| 63 | ); |
| 64 | $response = new Response($view, 500); |
| 65 | } |
| 66 | $event->setResponse($response); |
| 67 | } |
| 68 | } |
| 69 | } |