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

ExceptionResponseListener   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 10
c 0
b 0
f 0
ccs 27
cts 27
cp 1
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponseHeadersByException() 0 9 2
A getResponseStatusCodeByException() 0 7 2
A onKernelException() 0 10 4
A getResponse() 0 13 1
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