GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#1)
by Pascal
07:37
created

ResponseListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createEmptyResponse() 0 4 1
A onKernelView() 0 21 3
A isContentAllowed() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Saikootau\ApiBundle\Event\Listener;
6
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
9
10
class ResponseListener extends MediaTypeListener
11
{
12
    private static $noContentMethods = [
13
        'HEAD',
14
    ];
15
16
    public function onKernelView(GetResponseForControllerResultEvent $event): void
17
    {
18
        $controllerResult = $event->getControllerResult();
19
20
        if ($controllerResult instanceof Response) {
21
            return;
22
        }
23
24
        if (!$this->isContentAllowed($event->getRequest()->getMethod())) {
25
            $event->setResponse(
26
                $this->createEmptyResponse(Response::HTTP_OK)
27
            );
28
29
            return;
30
        }
31
32
        $serializer = $this->getSerializer($event->getRequest());
33
        $event->setResponse(new Response(
34
            $serializer->serialize($controllerResult),
35
            Response::HTTP_OK,
36
            ['Content-Type' => $serializer->getContentType()]
37
        ));
38
    }
39
40
    /**
41
     * Checks if the response should contain any content.
42
     *
43
     * @param string $method
44
     *
45
     * @return bool
46
     */
47
    private function isContentAllowed(string $method): bool
48
    {
49
        return !in_array($method, self::$noContentMethods);
50
    }
51
52
    /**
53
     * Create a response without any body.
54
     *
55
     * @param int $status
56
     *
57
     * @return Response
58
     */
59
    private function createEmptyResponse(int $status): Response
60
    {
61
        return new Response(
62
            '', $status
63
        );
64
    }
65
}
66