TemplatingViewListener::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Zenstruck\ControllerUtil\EventListener;
4
5
use Symfony\Component\Templating\EngineInterface;
6
use Zenstruck\ControllerUtil\View;
7
8
/**
9
 * @author Kevin Bond <[email protected]>
10
 */
11
class TemplatingViewListener extends ViewListener
12
{
13
    protected $templating;
14
15 4
    public function __construct(EngineInterface $templating)
16
    {
17 4
        $this->templating = $templating;
18 4
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 2
    protected function getContent(View $view, $format)
24
    {
25 2
        $template = $view->getTemplate();
26
27 2
        if (is_array($template)) {
28 1
            foreach ($template as $t) {
29 1
                if ($this->templating->exists($t)) {
30 1
                    $template = $t;
31 1
                    break;
32
                }
33 1
            }
34 1
        }
35
36 2
        return $this->templating->render($template, $view->getDataAsArray());
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 3
    protected function supports(View $view, $format)
43
    {
44 3
        return null !== $view->getTemplate();
45
    }
46
}
47