TwigRendererAdapter::createEngine()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
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 Twig\Environment as Engine;
9
use Twig\Loader;
10
11
class TwigRendererAdapter 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 $this->getEngine()->getLoader()->exists($name);
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 2
    public function render(string $name, array $params = []): string
32
    {
33 2
        return $this->getEngine()->render($name, $params);
34
    }
35
36
    /**
37
     * Get the Twig template Engine.
38
     */
39 3
    protected function getEngine(): Engine
40
    {
41 3
        return $this->engine ??= $this->createEngine();
42
    }
43
44
    /**
45
     * Create the Twig template Engine.
46
     */
47 3
    protected function createEngine(): Engine
48
    {
49 3
        return new Engine(
50 3
            new Loader\FilesystemLoader($this->viewPath),
51 3
            ["cache" => $this->cachePath ?? false],
52
        );
53
    }
54
}
55