Passed
Push — master ( e18bff...6e2c26 )
by Kévin
04:13
created

ExceptionListener::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\EventListener;
15
16
use ApiPlatform\Core\Util\RequestAttributesExtractor;
17
use Psr\Log\LoggerInterface;
18
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
19
use Symfony\Component\HttpKernel\EventListener\ExceptionListener as BaseExceptionListener;
20
21
/**
22
 * Handles requests errors.
23
 *
24
 * @author Samuel ROZE <[email protected]>
25
 * @author Kévin Dunglas <[email protected]>
26
 */
27
final class ExceptionListener
28
{
29
    private $exceptionListener;
30
31
    public function __construct($controller, LoggerInterface $logger = null)
32
    {
33
        $this->exceptionListener = new BaseExceptionListener($controller, $logger);
34
    }
35
36
    public function onKernelException(GetResponseForExceptionEvent $event): void
37
    {
38
        $request = $event->getRequest();
39
        // Normalize exceptions only for routes managed by API Platform
40
        if (
41
            'html' === $request->getRequestFormat('') ||
42
            !((RequestAttributesExtractor::extractAttributes($request)['respond'] ?? $request->attributes->getBoolean('_api_respond', false)) || $request->attributes->getBoolean('_graphql', false))
43
        ) {
44
            return;
45
        }
46
47
        $this->exceptionListener->onKernelException($event);
48
    }
49
}
50