Passed
Push — develop ( 717361...212af8 )
by Brent
02:48
created

TwigRenderer::renderTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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