Completed
Pull Request — master (#46)
by
unknown
02:50
created

ExceptionListener::onKernelException()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 38
rs 6.7272
cc 7
eloc 25
nc 7
nop 1
1
<?php
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\EventListener;
10
11
use KleijnWeb\SwaggerBundle\Response\VndErrorResponse;
12
use Psr\Log\LoggerInterface;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
15
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16
use Symfony\Component\Security\Core\Exception\AuthenticationException;
17
18
/**
19
 * @author John Kleijn <[email protected]>
20
 */
21
class ExceptionListener
22
{
23
    /**
24
     * @var LoggerInterface
25
     */
26
    private $logger;
27
28
    /**
29
     * @param LoggerInterface $logger
30
     */
31
    public function __construct(LoggerInterface $logger)
32
    {
33
        $this->logger = $logger;
34
    }
35
36
    /**
37
     * @param LoggerInterface $logger
38
     *
39
     * @return $this
40
     */
41
    public function setLogger(LoggerInterface $logger)
42
    {
43
        $this->logger = $logger;
44
45
        return $this;
46
    }
47
48
    /**
49
     * @param GetResponseForExceptionEvent $event
50
     */
51
    public function onKernelException(GetResponseForExceptionEvent $event)
52
    {
53
        $logRef = uniqid();
54
        $exception = $event->getException();
55
56
        if ($exception instanceof NotFoundHttpException) {
57
            $event->setResponse(new VndErrorResponse("Not found", Response::HTTP_NOT_FOUND));
58
59
            return;
60
        }
61
62
        if ($exception instanceof AuthenticationException) {
63
            $event->setResponse(new VndErrorResponse("Unauthorized", Response::HTTP_UNAUTHORIZED));
64
65
            return;
66
        }
67
68
        $code = $exception->getCode();
69
70
        if (strlen($code) !== 3) {
71
            $this->fallback($message, $code, $logRef, $exception);
72
        } else {
73
            switch (substr($code, 0, 1)) {
74
                case '4':
75
                    $message = get_class($exception) === InvalidParametersException::class ? $exception->getMessage() : 'Input Error';
76
                    $this->logger->notice("Input error [logref $logRef]: " . $exception->__toString());
77
                    break;
78
                case '5':
79
                    $message = 'Server Error';
80
                    $this->logger->error("Runtime error [logref $logRef]: " . $exception->__toString());
81
                    break;
82
                default:
83
                    $this->fallback($message, $code, $logRef, $exception);
84
            }
85
        }
86
87
        $event->setResponse(new VndErrorResponse($message, $code, $logRef));
88
    }
89
90
    /**
91
     * @param string     $message
92
     * @param string     $code
93
     * @param string     $logRef
94
     * @param \Exception $exception
95
     */
96
    private function fallback(&$message, &$code, $logRef, \Exception $exception)
97
    {
98
        $code = 500;
99
        $message = 'Server Error';
100
        $this->logger->critical("Runtime error [logref $logRef]: " . $exception->__toString());
101
    }
102
}
103