Completed
Push — master ( 233dd0...ddc3fd )
by Karel
06:06
created

ExceptionListener::onKernelException()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 9
cts 9
cp 1
rs 8.8017
c 0
b 0
f 0
cc 6
nc 3
nop 1
crap 6
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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
namespace FOS\RestBundle\EventListener;
13
14
use FOS\RestBundle\FOSRestBundle;
15
use Psr\Log\LoggerInterface;
16
use Symfony\Component\Debug\Exception\FlattenException as LegacyFlattenException;
17
use Symfony\Component\ErrorHandler\Exception\FlattenException;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
21
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
22
use Symfony\Component\HttpKernel\EventListener\ExceptionListener as LegacyExceptionListener;
23
use Symfony\Component\HttpKernel\EventListener\ErrorListener;
24
use Symfony\Component\HttpKernel\KernelEvents;
25
26
/**
27
 * ExceptionListener.
28
 *
29
 * @author Ener-Getick <[email protected]>
30
 *
31
 * @internal
32
 */
33 10
class ExceptionListener implements EventSubscriberInterface
34
{
35 10
    private $exceptionListener;
36
    private $dispatcher;
37 10
38 1
    public function __construct($controller, ?LoggerInterface $logger, EventDispatcherInterface $dispatcher)
39
    {
40
        if (class_exists(ErrorListener::class)) {
41 9
            $this->exceptionListener = new ErrorListener($controller, $logger);
42 9
        } else {
43
            $this->exceptionListener = new LegacyExceptionListener($controller, $logger);
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\HttpKe...tener\ExceptionListener has been deprecated with message: since Symfony 4.4, use ErrorListener instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
44
        }
45
        $this->dispatcher = $dispatcher;
46
    }
47 16
48
    /**
49
     * @param ExceptionEvent $event
50 16
     */
51 16
    public function onKernelException($event)
52
    {
53
        $request = $event->getRequest();
54
55
        if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
56
            return;
57 9
        }
58
59
        if (method_exists($event, 'getThrowable')) {
60 9
            $exception = $event->getThrowable();
61 9
        } else {
62 9
            $exception = $event->getException();
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\HttpKe...onEvent::getException() has been deprecated with message: since Symfony 4.4, use getThrowable instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
63 9
        }
64 9
65 9
        $controllerArgsListener = function ($event) use (&$controllerArgsListener, $exception) {
66
            /** @var ControllerArgumentsEvent $event */
67 9
            $arguments = $event->getArguments();
68
            foreach ($arguments as $k => $argument) {
69
                if ($argument instanceof FlattenException || $argument instanceof LegacyFlattenException) {
70
                    $arguments[$k] = $exception;
71
                    $event->setArguments($arguments);
72
73
                    break;
74
                }
75
            }
76
            $this->dispatcher->removeListener(KernelEvents::CONTROLLER_ARGUMENTS, $controllerArgsListener);
77
        };
78
        $this->dispatcher->addListener(KernelEvents::CONTROLLER_ARGUMENTS, $controllerArgsListener, -100);
79
80
        $this->exceptionListener->onKernelException($event);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public static function getSubscribedEvents()
87
    {
88
        return array(
89
            KernelEvents::EXCEPTION => array('onKernelException', -100),
90
        );
91
    }
92
}
93