Completed
Push — develop ( 186fbd...dd6a23 )
by Baptiste
02:23
created

ExceptionListener   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 6 1
A transformException() 0 16 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Rest\ServerBundle\EventListener;
5
6
use Innmind\Http\Exception\Http\ExceptionInterface;
7
use Symfony\Component\{
8
    EventDispatcher\EventSubscriberInterface,
9
    HttpKernel\KernelEvents,
10
    HttpKernel\Event\GetResponseForExceptionEvent,
11
    HttpKernel\Exception\HttpException
12
};
13
14
final class ExceptionListener implements EventSubscriberInterface
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 1
    public static function getSubscribedEvents()
20
    {
21
        return [
22 1
            KernelEvents::EXCEPTION => [['transformException', 100]],
23
        ];
24
    }
25
26
    /**
27
     * Transform an innmind http exception into a symfony one to generate the
28
     * appropriate http response
29
     *
30
     * @param GetResponseForExceptionEvent $event
31
     *
32
     * @return void
33
     */
34 2
    public function transformException(GetResponseForExceptionEvent $event)
35
    {
36 2
        $exception = $event->getException();
37
38 2
        if (!$exception instanceof ExceptionInterface) {
39 1
            return;
40
        }
41
42 1
        $event->setException(
43 1
            new HttpException(
44 1
                $exception->httpCode(),
45 1
                null,
46
                $exception
47
            )
48
        );
49 1
    }
50
}
51