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
04:06
created

ResponseListener::onKernelView()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 1
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
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