|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alchemy\RestBundle\EventListener; |
|
4
|
|
|
|
|
5
|
|
|
use Alchemy\Rest\Request\ContentTypeMatcher; |
|
6
|
|
|
use Alchemy\Rest\Response\ExceptionTransformer; |
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
9
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
|
10
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; |
|
11
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
12
|
|
|
|
|
13
|
|
|
class ExceptionListener implements EventSubscriberInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
private $handledContentTypes; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var ContentTypeMatcher |
|
22
|
|
|
*/ |
|
23
|
|
|
private $contentTypeMatcher; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ExceptionTransformer |
|
27
|
|
|
*/ |
|
28
|
|
|
private $exceptionTransformer; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param ContentTypeMatcher $matcher |
|
32
|
|
|
* @param ExceptionTransformer $exceptionTransformer |
|
33
|
|
|
* @param array $handledContentTypes |
|
34
|
|
|
*/ |
|
35
|
4 |
|
public function __construct( |
|
36
|
|
|
ContentTypeMatcher $matcher, |
|
37
|
|
|
ExceptionTransformer $exceptionTransformer = null, |
|
38
|
|
|
array $handledContentTypes = null |
|
39
|
|
|
) { |
|
40
|
4 |
|
$this->contentTypeMatcher = $matcher; |
|
41
|
4 |
|
$this->exceptionTransformer = $exceptionTransformer ?: new ExceptionTransformer\DefaultExceptionTransformer(); |
|
42
|
4 |
|
$this->handledContentTypes = $handledContentTypes ?: array('application/json'); |
|
43
|
4 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param GetResponseForExceptionEvent $event |
|
47
|
|
|
*/ |
|
48
|
4 |
|
public function onKernelException(GetResponseForExceptionEvent $event) |
|
49
|
|
|
{ |
|
50
|
4 |
|
$request = $event->getRequest(); |
|
51
|
4 |
|
$acceptHeader = $request->headers->get('Accept', '*/*'); |
|
52
|
|
|
|
|
53
|
4 |
|
if (!$this->contentTypeMatcher->matches($acceptHeader, $this->handledContentTypes)) { |
|
|
|
|
|
|
54
|
2 |
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
$exception = $event->getException(); |
|
58
|
2 |
|
$data = $this->exceptionTransformer->transformException($exception); |
|
59
|
2 |
|
$status = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500; |
|
60
|
|
|
|
|
61
|
2 |
|
$event->setResponse(new JsonResponse($data, $status)); |
|
62
|
2 |
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
public static function getSubscribedEvents() |
|
65
|
|
|
{ |
|
66
|
2 |
|
return array(KernelEvents::EXCEPTION => 'onKernelException'); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.