ViewListener   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 3
c 4
b 1
f 0
lcom 0
cbo 4
dl 0
loc 42
ccs 16
cts 16
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A onKernelView() 0 23 3
supports() 0 1 ?
getContent() 0 1 ?
1
<?php
2
3
namespace Zenstruck\ControllerUtil\EventListener;
4
5
use Symfony\Component\HttpFoundation\Response;
6
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
7
use Zenstruck\ControllerUtil\View;
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 */
12
abstract class ViewListener
13
{
14 12
    public function onKernelView(GetResponseForControllerResultEvent $event)
15
    {
16 12
        $result = $event->getControllerResult();
17 12
        $format = $event->getRequest()->getRequestFormat();
18
19 12
        if (!$result instanceof View) {
20 3
            return;
21
        }
22
23 9
        if (!$this->supports($result, $format)) {
24 4
            return;
25
        }
26
27 5
        $response = Response::create(
28 5
            $this->getContent($result, $format),
29 5
            $result->getStatusCode(),
30 5
            $result->getHeaders()
31 5
        )
32 5
        ->setCache($result->getCache())
33 5
        ;
34
35 5
        $event->setResponse($response);
36 5
    }
37
38
    /**
39
     * @param View   $view
40
     * @param string $format
41
     *
42
     * @return bool
43
     */
44
    abstract protected function supports(View $view, $format);
45
46
    /**
47
     * @param View   $view
48
     * @param string $format
49
     *
50
     * @return string
51
     */
52
    abstract protected function getContent(View $view, $format);
53
}
54