Passed
Push — develop ( 5df010...37d230 )
by Brent
07:25
created

TwigRenderer::renderTemplate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.5

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 8
ccs 2
cts 4
cp 0.5
crap 2.5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Stitcher\Renderer;
4
5
use Stitcher\Exception\InvalidConfiguration;
6
use Symfony\Component\Filesystem\Filesystem;
7
use Twig_Environment;
8
use Twig_Error_Loader;
9
10
class TwigRenderer extends Twig_Environment implements Renderer
11
{
12 22
    public function __construct(string $templateDirectory)
13
    {
14 22
        $fs = new Filesystem();
15
16 22
        if (! $fs->exists($templateDirectory)) {
17 1
            $fs->mkdir($templateDirectory);
18
        }
19
20 22
        $loader = new \Twig_Loader_Filesystem($templateDirectory);
21
22 22
        parent::__construct($loader);
23 22
    }
24
25 22
    public static function make(string $templateDirectory): TwigRenderer
26
    {
27 22
        return new self($templateDirectory);
28
    }
29
30 19
    public function renderTemplate(string $path, array $variables): string
31
    {
32
        try {
33 19
            return $this->render($path, $variables);
34
        } catch (Twig_Error_Loader $e) {
35
            throw InvalidConfiguration::templateNotFound($path);
36
        }
37
    }
38
39 4
    public function customExtension(Extension $extension): void
40
    {
41 4
        $this->addGlobal($extension->name(), $extension);
42 4
    }
43
}
44