Passed
Push — master ( 130df2...007dfb )
by Alexis
10:23
created

ExceptionListener::setException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Liip/FunctionalTestBundle
7
 *
8
 * (c) Lukas Kahwe Smith <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace Liip\FunctionalTestBundle\EventListener;
15
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
18
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
19
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
20
use Symfony\Component\HttpKernel\Event\RequestEvent;
21
use Symfony\Component\HttpKernel\HttpKernelInterface;
22
use Symfony\Component\HttpKernel\KernelEvents;
23
24 10
if (class_exists(ExceptionEvent::class) && !class_exists(GetResponseForExceptionEvent::class)) {
25
    class_alias(ExceptionEvent::class, GetResponseForExceptionEvent::class);
26
}
27
28 10
if (class_exists(RequestEvent::class) && !class_exists(GetResponseEvent::class)) {
29
    class_alias(RequestEvent::class, GetResponseEvent::class);
30
}
31
32
final class ExceptionListener implements EventSubscriberInterface
33
{
34
    /**
35
     * @var \Throwable|null
36
     */
37
    private $lastException;
38
39 3
    public function setException(GetResponseForExceptionEvent $event): void
40
    {
41 3
        $this->lastException = method_exists($event, 'getThrowable') ? $event->getThrowable() : $event->getException();
42 3
    }
43
44 21
    public function clearLastException(GetResponseEvent $event): void
45
    {
46 21
        if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
47 21
            $this->lastException = null;
48
        }
49 21
    }
50
51 3
    public function getLastException(): ?\Throwable
52
    {
53 3
        return $this->lastException;
54
    }
55
56 4
    public static function getSubscribedEvents(): array
57
    {
58
        return [
59 4
            KernelEvents::EXCEPTION => ['setException', 99999],
60
            KernelEvents::REQUEST => ['clearLastException', 99999],
61
        ];
62
    }
63
}
64