AbstractLazyResponseHandler::handleLazyResponse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Basster\LazyResponseBundle\Response\Handler;
5
6
use Basster\LazyResponseBundle\Response\LazyResponseInterface;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\HttpKernel\Event\ViewEvent;
10
use Symfony\Component\HttpKernel\KernelEvents;
11
12
/**
13
 * Class AbstractLazyResponseHandler.
14
 */
15
abstract class AbstractLazyResponseHandler implements EventSubscriberInterface
16
{
17
    /**
18
     * @return string[][]
19
     *
20
     * @psalm-return array{'kernel.view': array{0: string}}
21
     */
22 3
    public static function getSubscribedEvents(): array
23
    {
24
        return [
25 3
            KernelEvents::VIEW => ['handleLazyResponse'],
26
        ];
27
    }
28
29 10
    public function handleLazyResponse(ViewEvent $event): void
30
    {
31 10
        $controllerResult = $event->getControllerResult();
32 10
        if ($this->isSupportedLazyResponse($controllerResult)) {
33 10
            $event->setResponse($this->generateResponse($controllerResult));
34
        }
35 10
    }
36
37
    abstract protected function isSupported(LazyResponseInterface $controllerResult): bool;
38
39
    abstract protected function generateResponse(LazyResponseInterface $controllerResult): Response;
40
41 10
    private function isSupportedLazyResponse(mixed $controllerResult): bool
42
    {
43 10
        return $controllerResult instanceof LazyResponseInterface && $this->isSupported($controllerResult);
44
    }
45
}
46