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:14
created

ResponseListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
ccs 21
cts 21
cp 1
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A onKernelView() 0 19 3
A createResponse() 0 6 1
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 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