Passed
Push — master ( fd728f...c078bf )
by Petr
03:30
created

ExceptionListener::onKernelException()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 54
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 54
ccs 0
cts 28
cp 0
rs 8.7449
cc 5
eloc 34
nc 5
nop 1
crap 30

How to fix   Long Method   

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 AppBundle\EventListener;
4
5
use AppBundle\Enum\Environment;
6
use AppBundle\Service\HashGenerator;
7
use Monolog\Logger;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
11
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
12
13
/**
14
 * @author Vehsamrak
15
 */
16
class ExceptionListener
17
{
18
19
    /** @var string */
20
    private $environment;
21
22
    /** @var string */
23
    private $adminEmail;
24
25
    /** @var Logger */
26
    private $logger;
27
28 28
    public function __construct(string $environment, string $adminEmail, Logger $logger)
29
    {
30 28
        $this->environment = $environment;
31 28
        $this->adminEmail = $adminEmail;
32 28
        $this->logger = $logger;
33 28
    }
34
35
    public function onKernelException(GetResponseForExceptionEvent $event)
36
    {
37
        /** @var \Exception $exception */
38
        $exception = $event->getException();
39
40
        if ($exception instanceof HttpExceptionInterface && $exception->getStatusCode() < 500) {
41
        	return;
42
        }
43
44
        if ($this->environment === Environment::PRODUCTION) {
45
            $bugSerialNumber = HashGenerator::generate();
46
47
            $this->logger->info(
48
                sprintf(
49
                    '#%s - %s in %s at line %s',
50
                    $bugSerialNumber,
51
                    $exception->getMessage(),
52
                    $exception->getFile(),
53
                    $exception->getLine()
54
                )
55
            );
56
57
            $errorMessage = sprintf(
58
                'Wow! You probably found a bug with serial number #%s. Please report it to %s.',
59
                $bugSerialNumber,
60
                $this->adminEmail
61
            );
62
        } else {
63
            $errorMessage = sprintf(
64
                '%s in %s at line %s',
65
                $exception->getMessage(),
66
                $exception->getFile(),
67
                $exception->getLine()
68
            );
69
        }
70
71
        $data = [
72
            'errors' => [
73
                $errorMessage,
74
            ],
75
        ];
76
77
        $response = new JsonResponse();
78
        $response->setContent(json_encode($data));
79
80
        if ($exception instanceof HttpExceptionInterface) {
81
            $response->setStatusCode($exception->getStatusCode());
82
            $response->headers->replace($exception->getHeaders());
83
        } else {
84
            $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
85
        }
86
87
        $event->setResponse($response);
88
    }
89
}
90