LatteView::with()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Wandu\View\Bridges\Latte;
3
4
use Latte\Engine;
5
use Wandu\View\Contracts\RenderInterface;
6
use Wandu\View\FileNotFoundException;
7
8
class LatteView implements RenderInterface
9
{
10
    /** @var \Latte\Engine */
11
    protected $latte;
12
13
    /** @var string */
14
    protected $basePath;
15
16
    /** @var array */
17
    protected $values = [];
18
19
    /**
20
     * @param \Latte\Engine $latte
21
     * @param string $basePath
22
     */
23
    public function __construct(Engine $latte, $basePath = '')
24
    {
25
        $this->latte = $latte;
26
        $this->basePath = $basePath;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function with(array $values = [])
33
    {
34
        $new = clone $this;
35
        $new->values = $values;
36
        return $new;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function render($template, array $values = [], $basePath = null)
43
    {
44
        if (!isset($basePath)) {
45
            $basePath = $this->basePath;
46
        }
47
        if (!file_exists("{$basePath}/{$template}")) {
48
            throw new FileNotFoundException("Cannot find the file, {$basePath}/{$template}");
49
        }
50
        return $this->latte->renderToString("{$basePath}/{$template}" , $values + $this->values);
51
    }
52
}
53