TemplatingViewListener   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
lcom 1
cbo 3
dl 0
loc 36
ccs 15
cts 15
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getContent() 0 15 4
A supports() 0 4 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