Completed
Push — master ( 7705fa...057260 )
by Karel
10:09 queued 11s
created

ExceptionListener   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 44
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A onKernelException() 0 22 5
A getSubscribedEvents() 0 6 1
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\ErrorRenderer\Exception\FlattenException;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
21
use Symfony\Component\HttpKernel\Event\KernelEvent;
22
use Symfony\Component\HttpKernel\EventListener\ExceptionListener as HttpKernelExceptionListener;
23
use Symfony\Component\HttpKernel\KernelEvents;
24
25
/**
26
 * ExceptionListener.
27
 *
28
 * @author Ener-Getick <[email protected]>
29
 *
30
 * @internal
31
 */
32
class ExceptionListener implements EventSubscriberInterface
33 10
{
34
    private $exceptionListener;
35 10
    private $dispatcher;
36
37 10
    public function __construct($controller, ?LoggerInterface $logger, EventDispatcherInterface $dispatcher)
38 1
    {
39
        $this->exceptionListener = new HttpKernelExceptionListener($controller, $logger);
40
        $this->dispatcher = $dispatcher;
41 9
    }
42 9
43
    public function onKernelException(GetResponseForExceptionEvent $event)
44
    {
45
        $request = $event->getRequest();
46
47 16
        if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
48
            return;
49
        }
50 16
51 16
        $exception = $event->getException();
52
        $requestListener = function (KernelEvent $event) use (&$requestListener, $exception) {
53
            $request = $event->getRequest();
54
55
            if (!$event->isMasterRequest() && ($request->attributes->get('exception') instanceof FlattenException || $request->attributes->get('exception') instanceof LegacyFlattenException)) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\ErrorR...eption\FlattenException does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
56
                $request->attributes->set('exception', $exception);
57 9
            }
58
59
            $this->dispatcher->removeListener(KernelEvents::REQUEST, $requestListener);
60 9
        };
61 9
        $this->dispatcher->addListener(KernelEvents::REQUEST, $requestListener);
62 9
63 9
        $this->exceptionListener->onKernelException($event);
64 9
    }
65 9
66
    /**
67 9
     * {@inheritdoc}
68
     */
69
    public static function getSubscribedEvents()
70
    {
71
        return array(
72
            KernelEvents::EXCEPTION => array('onKernelException', -100),
73
        );
74
    }
75
}
76