Completed
Push — master ( fb3acb...928588 )
by Christian
02:51 queued 10s
created

setResponseStatusCode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\EventListener;
13
14
use FOS\RestBundle\FOSRestBundle;
15
use FOS\RestBundle\Util\ExceptionValueMap;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
18
use Symfony\Component\HttpKernel\Event\ResponseEvent;
19
use Symfony\Component\HttpKernel\KernelEvents;
20
21
/**
22
 * @author Christian Flothmann <[email protected]>
23
 */
24
class ResponseStatusCodeListener implements EventSubscriberInterface
25
{
26
    private $exceptionValueMap;
27
    private $responseStatusCode;
28
29 5
    public function __construct(ExceptionValueMap $exceptionValueMap)
30
    {
31 5
        $this->exceptionValueMap = $exceptionValueMap;
32 5
    }
33
34
    public static function getSubscribedEvents(): array
35
    {
36
        return [
37
            KernelEvents::EXCEPTION => 'getResponseStatusCodeFromThrowable',
38
            KernelEvents::RESPONSE => 'setResponseStatusCode',
39
        ];
40
    }
41
42
    /**
43
     * @param ExceptionEvent $event
44
     */
45 5
    public function getResponseStatusCodeFromThrowable($event): void
46
    {
47 5
        if (!$event->isMasterRequest()) {
48 1
            return;
49
        }
50
51 5
        $request = $event->getRequest();
52
53 5
        if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
54 1
            return;
55
        }
56
57 4
        if (method_exists($event, 'getThrowable')) {
58 4
            $throwable = $event->getThrowable();
59
        } else {
60
            $throwable = $event->getException();
0 ignored issues
show
Bug introduced by
The method getException() does not seem to exist on object<Symfony\Component...l\Event\ExceptionEvent>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
        }
62
63 4
        $statusCode = $this->exceptionValueMap->resolveThrowable($throwable);
64
65 4
        if (is_int($statusCode)) {
66 3
            $this->responseStatusCode = $statusCode;
67
        }
68 4
    }
69
70
    /**
71
     * @param ResponseEvent $event
72
     */
73 5
    public function setResponseStatusCode($event): void
74
    {
75 5
        if (!$event->isMasterRequest()) {
76 1
            return;
77
        }
78
79 5
        if (null !== $this->responseStatusCode) {
80 3
            $event->getResponse()->setStatusCode($this->responseStatusCode);
81
82 3
            $this->responseStatusCode = null;
83
        }
84 5
    }
85
}
86