RestApiEventSubscriber::onResponseEarly()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace MediaMonks\RestApi\EventSubscriber;
4
5
use JetBrains\PhpStorm\ArrayShape;
6
use MediaMonks\RestApi\Request\RequestMatcherInterface;
7
use MediaMonks\RestApi\Request\RequestTransformerInterface;
8
use MediaMonks\RestApi\Response\ResponseTransformerInterface;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
12
use Symfony\Component\HttpKernel\Event\KernelEvent;
13
use Symfony\Component\HttpKernel\Event\RequestEvent;
14
use Symfony\Component\HttpKernel\Event\ResponseEvent;
15
use Symfony\Component\HttpKernel\Event\ViewEvent;
16
use Symfony\Component\HttpKernel\KernelEvents;
17
18
class RestApiEventSubscriber implements EventSubscriberInterface
19
{
20
    public function __construct(
21
        private RequestMatcherInterface      $requestMatcher,
22
        private RequestTransformerInterface  $requestTransformer,
23
        private ResponseTransformerInterface $responseTransformer
24
    )
25
    {
26
27
    }
28
29
    #[ArrayShape([KernelEvents::REQUEST => "array[]", KernelEvents::EXCEPTION => "array[]", KernelEvents::VIEW => "array[]", KernelEvents::RESPONSE => "array[]"])] public static function getSubscribedEvents(): array
30
    {
31
        return [
32
            KernelEvents::REQUEST => [
33
                ['onRequest', 512],
34
            ],
35
            KernelEvents::EXCEPTION => [
36
                ['onException', 512],
37
            ],
38 12
            KernelEvents::VIEW => [
39
                ['onView', 0],
40
            ],
41
            KernelEvents::RESPONSE => [
42
                ['onResponseEarly', 0],
43 12
                ['onResponseLate', -512],
44 12
            ],
45 12
        ];
46 12
    }
47
48
    public function onRequest(RequestEvent $event): void
49
    {
50
        if (!$this->eventRequestMatches($event)) {
51 6
            return;
52
        }
53
54 6
        $this->requestTransformer->transform($event->getRequest());
55 6
    }
56 6
57 6
    public function onException(ExceptionEvent $event): void
58 6
    {
59 6
        if (!$this->eventRequestMatches($event)) {
60 6
            return;
61 6
        }
62 6
63 6
        $event->setResponse($this->responseTransformer->createResponseFromContent($event->getThrowable()));
64 6
    }
65 6
66 6
    public function onView(ViewEvent $event): void
67 6
    {
68
        if (!$this->eventRequestMatches($event)) {
69
            return;
70
        }
71
72
        $event->setResponse($this->responseTransformer->createResponseFromContent($event->getControllerResult()));
73 2
    }
74
75 2
    public function onResponseEarly(ResponseEvent $event): void
76 1
    {
77
        if (!$this->eventRequestMatches($event)) {
78 1
            return;
79 1
        }
80
81
        $event->setResponse($this->responseTransformer->transformEarly($event->getRequest(), $event->getResponse()));
82
    }
83
84
    public function onResponseLate(ResponseEvent $event): void
85
    {
86 2
        if (!$this->eventRequestMatches($event)) {
87
            return;
88 2
        }
89 1
90
        $this->responseTransformer->transformLate($event->getRequest(), $event->getResponse());
91 1
    }
92 1
93
    protected function eventRequestMatches(KernelEvent $event): bool
94
    {
95
        if ($event->getRequest()->getMethod() === Request::METHOD_OPTIONS) return false;
96
97
        return $this->requestMatcher->matches($event->getRequest(), $event->getRequestType());
98
    }
99
}
100