AbstractLazyResponseHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 29
ccs 9
cts 9
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isSupportedLazyResponse() 0 3 2
A getSubscribedEvents() 0 4 1
A handleLazyResponse() 0 5 2
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