ViewListener::getContent()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 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