LatteRendererAdapter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Frostaly\Template\Adapters;
6
7
use Frostaly\Template\Contracts\RendererAdapterInterface;
8
use Latte\Engine;
9
use Latte\Loaders\FileLoader;
10
11
class LatteRendererAdapter implements RendererAdapterInterface
12
{
13
    protected Engine $engine;
14
15 3
    public function __construct(
16
        protected string $viewPath,
17
        protected ?string $cachePath = null,
18
    ) {}
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 1
    public function exists(string $name): bool
24
    {
25 1
        return file_exists($this->viewPath . '/' . $name);
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 2
    public function render(string $name, array $params = []): string
32
    {
33 2
        return $this->getEngine()->renderToString($name, $params);
34
    }
35
36
    /**
37
     * Get the Latte template Engine.
38
     */
39 2
    protected function getEngine(): Engine
40
    {
41 2
        return $this->engine ??= $this->createEngine();
42
    }
43
44
    /**
45
     * Create the Latte template Engine.
46
     */
47 2
    protected function createEngine(): Engine
48
    {
49 2
        $engine = new Engine();
50 2
        $engine->setLoader(new FileLoader($this->viewPath));
51 2
        return $engine->setTempDirectory($this->cachePath);
52
    }
53
}
54