TwigRendererAdapter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 41
ccs 11
cts 11
cp 1
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 3 1
A getEngine() 0 3 1
A __construct() 0 4 1
A createEngine() 0 5 1
A render() 0 3 1
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