ExceptionListener   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 54
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B onKernelException() 0 26 11
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