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
03:21 queued 01:08
created

ResponseListener::createResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 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 3
    public function onKernelView(GetResponseForControllerResultEvent $event): void
17
    {
18 3
        $controllerResult = $event->getControllerResult();
19
20 3
        if ($controllerResult instanceof Response) {
21 1
            return;
22
        }
23
24 2
        $serializer = $this->getSerializer($event->getRequest());
25 2
        $body = '';
26 2
        if ($this->isContentAllowed($event->getRequest()->getMethod())) {
27 2
            $body = $serializer->serialize($controllerResult);
28
        }
29
30 2
        $event->setResponse(
31 2
            $this->createResponse(
32 2
                $body,
33 2
                Response::HTTP_OK,
34 2
                $serializer->getContentType()
35
            )
36
        );
37 2
    }
38
39
    /**
40
     * Checks if the response should contain any content.
41
     *
42
     * @param string $method
43
     *
44
     * @return bool
45
     */
46 2
    private function isContentAllowed(string $method): bool
47
    {
48 2
        return !in_array($method, self::$noContentMethods);
49
    }
50
51
    /**
52
     * Create a response for the given data.
53
     *
54
     * @param string $body
55
     * @param int    $status
56
     * @param string $contentType
57
     *
58
     * @return Response
59
     */
60 2
    private function createResponse(string $body, int $status, string $contentType): Response
61
    {
62 2
        return new Response(
63 2
            $body,
64 2
            $status,
65 2
            ['Content-Type' => $contentType]
66
        );
67
    }
68
}
69