ExceptionListener::onKernelException()   B
last analyzed

Complexity

Conditions 11
Paths 8

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 22
cp 0
rs 7.3166
c 0
b 0
f 0
cc 11
nc 8
nop 1
crap 132

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Happyr\ApiBundle\EventListener;
4
5
use Fervo\ValidatedMessage\ValidationFailedException as FervoValaidationFailed;
6
use Happyr\ApiBundle\Service\ResponseFactory;
7
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
8
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
9
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
10
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
11
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
13
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
14
use Symfony\Component\Security\Core\Exception\AuthenticationException;
15
use Symfony\Component\Messenger\Exception\ValidationFailedException;
16
17
/**
18
 * @author Tobias Nyholm <[email protected]>
19
 */
20
class ExceptionListener
21
{
22
    /**
23
     * @var ResponseFactory
24
     */
25
    private $responseFactory;
26
27
    /**
28
     * @var string
29
     */
30
    private $pathPrefix;
31
32
    /**
33
     * @param ResponseFactory $responseFactory
34
     * @param string          $pathPrefix
35
     */
36
    public function __construct(ResponseFactory $responseFactory, $pathPrefix)
37
    {
38
        $this->responseFactory = $responseFactory;
39
        $this->pathPrefix = $pathPrefix;
40
    }
41
42
    /**
43
     * Make sure we print a nice error message to the user when we encounter an exception.
44
     *
45
     * @param GetResponseForExceptionEvent $event
46
     */
47
    public function onKernelException(GetResponseForExceptionEvent $event)
48
    {
49
        // Make sure to match uri before we start to catch exceptions
50
        if (!preg_match('|^'.$this->pathPrefix.'.*|sim', $event->getRequest()->getPathInfo())) {
51
            return;
52
        }
53
54
        $exception = $event->getException();
55
        if ($exception instanceof AccessDeniedException || $exception instanceof AccessDeniedHttpException) {
56
            $response = $this->responseFactory->createForbidden();
57
        } elseif ($exception instanceof AuthenticationException) {
58
            $response = $this->responseFactory->createUnauthorized();
59
        } elseif ($exception instanceof BadRequestHttpException) {
60
            $response = $this->responseFactory->createWrongArgs();
61
        } elseif ($exception instanceof MethodNotAllowedException || $exception instanceof MethodNotAllowedHttpException) {
62
            $response = $this->responseFactory->createWithError('Method not allowed', 405, 'GEN-METHOD');
63
        } elseif ($exception instanceof NotFoundHttpException) {
64
            $response = $this->responseFactory->createNotFound();
65
        } elseif ($exception instanceof FervoValaidationFailed || $exception instanceof ValidationFailedException) {
0 ignored issues
show
Bug introduced by
The class Fervo\ValidatedMessage\ValidationFailedException 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...
Bug introduced by
The class Symfony\Component\Messen...lidationFailedException 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...
66
            $response = $this->responseFactory->createValidationFailed($exception->getViolations());
67
        } else {
68
            $response = $this->responseFactory->createInternalError();
69
        }
70
71
        $event->setResponse($response);
72
    }
73
}
74