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

ExceptionResponseListener::getResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 2
dl 0
loc 13
ccs 10
cts 10
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Saikootau\ApiBundle\Event\Listener;
6
7
use Saikootau\ApiBundle\Resource\Builder\ErrorResourceBuilder;
8
use Saikootau\ApiBundle\Resource\Builder\ServiceResourceBuilder;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
12
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
13
use Throwable;
14
15
class ExceptionResponseListener extends MediaTypeListener
16
{
17 9
    public function onKernelException(GetResponseForExceptionEvent $event): void
18
    {
19 9
        if ($event->getResponse()) {
20
            // Another listener already added a response, skip
21 1
            return;
22
        }
23
24 8
        if (($request = $event->getRequest()) && $request instanceof Request) {
25 7
            $response = $this->getResponse($request, $event->getException());
26 7
            $event->setResponse($response);
27
        }
28 8
    }
29
30 7
    private function getResponse(Request $request, Throwable $exception): Response
31
    {
32 7
        $errors = (new ErrorResourceBuilder())->build($exception);
33 7
        $service = (new ServiceResourceBuilder($request))
34 7
            ->addError(...$errors)
35 7
            ->build();
36
37 7
        $serializer = $this->getSerializer($request);
38
39 7
        return new Response(
40 7
            $serializer->serialize($service),
41 7
            $this->getResponseStatusCodeByException($exception),
42 7
            $this->getResponseHeadersByException($serializer->getContentType(), $exception)
43
        );
44
    }
45
46 7
    private function getResponseStatusCodeByException(Throwable $exception): int
47
    {
48 7
        if ($exception instanceof HttpExceptionInterface) {
49 3
            return $exception->getStatusCode();
50
        }
51
52 4
        return Response::HTTP_INTERNAL_SERVER_ERROR;
53
    }
54
55 7
    private function getResponseHeadersByException(string $contentType, Throwable $exception): array
56
    {
57 7
        $headers = [];
58 7
        if ($exception instanceof HttpExceptionInterface) {
59 3
            $headers = array_merge($headers, $exception->getHeaders());
60
        }
61 7
        $headers['Content-Type'] = $contentType;
62
63 7
        return $headers;
64
    }
65
}
66