Test Failed
Push — master ( 22b809...326de4 )
by Brent
05:15 queued 39s
created

TwigRenderer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 68.75%

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 11
cts 16
cp 0.6875
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A make() 0 4 1
A renderTemplate() 0 8 2
A customExtension() 0 4 1
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 2
    public function __construct(string $templateDirectory)
13
    {
14 2
        $fs = new Filesystem();
15
16 2
        if (! $fs->exists($templateDirectory)) {
17 1
            $fs->mkdir($templateDirectory);
18
        }
19
20 2
        $loader = new \Twig_Loader_Filesystem($templateDirectory);
21
22 2
        parent::__construct($loader);
23 2
    }
24
25 2
    public static function make(string $templateDirectory): TwigRenderer
26
    {
27 2
        return new self($templateDirectory);
28
    }
29
30 1
    public function renderTemplate(string $path, array $variables): string
31
    {
32
        try {
33 1
            return $this->render($path, $variables);
34
        } catch (Twig_Error_Loader $e) {
35
            throw InvalidConfiguration::templateNotFound($path);
36
        }
37
    }
38
39
    public function customExtension(Extension $extension): void
40
    {
41
        $this->addGlobal($extension->name(), $extension);
42
    }
43
}
44