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

GenerateHttpExceptionResponseListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 28
ccs 11
cts 11
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubscribedEvents() 0 6 1
A generateResponse() 0 10 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