TemplatingViewListener::getContent()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.2
cc 4
eloc 8
nc 4
nop 2
crap 4
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