Twig::renderSource()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Shippinno\Template;
5
6
use League\Flysystem\Filesystem;
7
use Throwable;
8
use Twig_Environment;
9
use Twig_Loader_Array;
10
11
class Twig extends Template
12
{
13
    /**
14
     * @var Twig_Environment
15
     */
16
    private $environment;
17
18
    /**
19
     * @param Filesystem $filesystem
20
     */
21 4
    public function __construct(Filesystem $filesystem)
22
    {
23 4
        parent::__construct($filesystem);
24 4
        $this->environment = new Twig_Environment(new Twig_Loader_Array());
25 4
    }
26
27
    /**
28
     * @param string $source
29
     * @param array $variables
30
     * @return string
31
     * @throws RenderFailedException
32
     */
33 3
    public function renderSource(string $source, array $variables): string
34
    {
35
        try {
36 3
            return $this->environment->createTemplate($source)->render($variables);
37 1
        } catch (Throwable $e) {
38 1
            throw new RenderFailedException($source, $e);
39
        }
40
    }
41
42
43
    /**
44
     * @param string $templateName
45
     * @return string
46
     */
47 2
    protected function fileName(string $templateName): string
48
    {
49 2
        return $templateName . '.twig';
50
    }
51
}
52