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
Push — master ( 8adfc3...aede48 )
by Pascal
9s
created

ResourceResponseListener   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 127
ccs 43
cts 43
cp 1
rs 10
c 0
b 0
f 0
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getResource() 0 7 2
A getStatusCode() 0 7 3
A getSerializer() 0 15 2
A getHeaders() 0 8 2
A getRequestAcceptableContentTypes() 0 9 2
A onKernelView() 0 19 3
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Saikootau\ApiBundle\EventListener;
6
7
use Saikootau\ApiBundle\Http\ResourceResponse;
8
use Saikootau\ApiBundle\MediaType\Exception\NonNegotiableMediaTypeException;
9
use Saikootau\ApiBundle\MediaType\MediaTypeNegotiator;
10
use Saikootau\ApiBundle\MediaType\MediaTypes;
11
use Saikootau\ApiBundle\Serializer\Serializer;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
15
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
16
17
class ResourceResponseListener
18
{
19
    private $defaultContentType;
20
    private $mediaTypeNegotiator;
21
22 5
    public function __construct(MediaTypeNegotiator $mediaTypeNegotiator, string $defaultContentType)
23
    {
24 5
        $this->mediaTypeNegotiator = $mediaTypeNegotiator;
25 5
        $this->defaultContentType = $defaultContentType;
26 5
    }
27
28 5
    public function onKernelView(GetResponseForControllerResultEvent $event): void
29
    {
30 5
        $controllerResult = $event->getControllerResult();
31
32 5
        if ($controllerResult instanceof Response) {
33 1
            return;
34
        }
35
36 4
        $serializer = $this->getSerializer($event->getRequest());
37
38 3
        $body = '';
39 3
        if (null !== $controllerResult) {
40 3
            $body = $serializer->serialize($this->getResource($controllerResult));
41
        }
42
43 3
        $event->setResponse(new Response(
44 3
            $body,
45 3
            $this->getStatusCode($event->getRequest(), $controllerResult),
46 3
            $this->getHeaders($controllerResult, $serializer->getContentType())
47
        ));
48 3
    }
49
50
    /**
51
     * Get the actual resource from the controller result.
52
     *
53
     * @param object $controllerResult
54
     *
55
     * @return object
56
     */
57 3
    private function getResource(object $controllerResult): object
58
    {
59 3
        if ($controllerResult instanceof ResourceResponse) {
60 1
            return $controllerResult->getResource();
61
        }
62
63 2
        return $controllerResult;
64
    }
65
66
    /**
67
     * Get the status code to return from the controller result.
68
     * Defaults to 202 for POST, PUT and 200 for every other method.
69
     *
70
     * @param Request     $request
71
     * @param object|null $controllerResult
72
     *
73
     * @return int
74
     */
75 3
    private function getStatusCode(Request $request, ?object $controllerResult): int
76
    {
77 3
        if ($controllerResult instanceof ResourceResponse) {
78 1
            return $controllerResult->getStatusCode();
79
        }
80
81 2
        return in_array($request->getMethod(), ['POST', 'PUT']) ? Response::HTTP_ACCEPTED : Response::HTTP_OK;
82
    }
83
84
    /**
85
     * Generates an array of headers for the given controller result.
86
     *
87
     * @param object|null $controllerResult
88
     * @param string      $contentType
89
     *
90
     * @return array
91
     */
92 3
    private function getHeaders(?object $controllerResult, string $contentType): array
93
    {
94 3
        $headers = ['Content-Type' => $contentType];
95 3
        if ($controllerResult instanceof ResourceResponse) {
96 1
            $headers = array_merge($controllerResult->getHeaders(), $headers);
97
        }
98
99 3
        return $headers;
100
    }
101
102
    /**
103
     * Get a matching serializer for the given request's acceptable content types.
104
     *
105
     * @param Request $request
106
     *
107
     * @return Serializer
108
     *
109
     * @throws NotAcceptableHttpException
110
     */
111 4
    private function getSerializer(Request $request): Serializer
112
    {
113
        try {
114
            /** @var Serializer $serializer */
115 4
            $serializer = $this->mediaTypeNegotiator->negotiate(...$this->getRequestAcceptableContentTypes($request));
116 1
        } catch (NonNegotiableMediaTypeException $exception) {
117 1
            throw new NotAcceptableHttpException(
118 1
                sprintf(
119 1
                    'Deliverable content types are: %s',
120 1
                    implode(', ', $this->mediaTypeNegotiator->getSupportedMediaTypes())
121
                )
122
            );
123
        }
124
125 3
        return $serializer;
126
    }
127
128
    /**
129
     * Returns an array of acceptable content types for the given request.
130
     *
131
     * @param Request $request
132
     *
133
     * @return string[]
134
     */
135 4
    private function getRequestAcceptableContentTypes(Request $request): array
136
    {
137 4
        $acceptableContentTypes = $request->getAcceptableContentTypes();
138
139 4
        if (false !== ($anyIndex = array_search(MediaTypes::TYPE_APPLICATION_ANY, $acceptableContentTypes))) {
140 1
            $acceptableContentTypes[$anyIndex] = $this->defaultContentType;
141
        }
142
143 4
        return $acceptableContentTypes;
144
    }
145
}
146