Completed
Push — develop ( a1c9d2...d630b8 )
by Baptiste
06:13
created

generateResponse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 3
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Rest\ServerBundle\EventListener;
5
6
use Symfony\Component\{
7
    EventDispatcher\EventSubscriberInterface,
8
    HttpKernel\KernelEvents,
9
    HttpKernel\Event\GetResponseForExceptionEvent,
10
    HttpKernel\Exception\HttpExceptionInterface,
11
    HttpFoundation\Response
12
};
13
14
final class GenerateHttpExceptionResponseListener implements EventSubscriberInterface
15
{
16
    private $shouldGenerate;
17
18 8
    public function __construct(bool $debug)
19
    {
20
        //let the debug bundle handle exceptions when necessarry
21 8
        $this->shouldGenerate = !$debug;
22 8
    }
23
24 2
    public static function getSubscribedEvents()
25
    {
26
        return [
27 2
            KernelEvents::EXCEPTION => [['generateResponse', -50]],
28
        ];
29
    }
30
31 6
    public function generateResponse(GetResponseForExceptionEvent $event): void
32
    {
33 6
        if (!$event->getException() instanceof HttpExceptionInterface) {
34 2
            return;
35
        }
36
37 4
        if ($this->shouldGenerate) {
38 2
            $event->setResponse(new Response);
39
        }
40 4
    }
41
}
42