Passed
Push — master ( 675168...23a502 )
by Paweł
03:16
created

TwigAdapter::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Copyright (c) 2020.
4
 * @author Paweł Antosiak <[email protected]>
5
 */
6
7
declare(strict_types=1);
8
9
namespace Gorynych\Adapter;
10
11
use Twig\Environment;
12
use Twig\Loader\FilesystemLoader;
13
14
class TwigAdapter
15
{
16
    protected Environment $twig;
17
18
    public function __construct()
19
    {
20
        $this->setup();
21
    }
22
23
    /**
24
     * Renders given template
25
     *
26
     * @param string $template
27
     * @param mixed[] $parameters
28
     * @return string
29
     */
30 1
    public function render(string $template, array $parameters): string
31
    {
32 1
        return $this->twig->render($template, $parameters);
33
    }
34
35
    /**
36
     * Setups Twig template engine
37
     */
38
    protected function setup(): void
39
    {
40
        $this->twig = new Environment(
41
            new FilesystemLoader($this->getGorynychTemplatesPath()),
42
            [
43
                'cache' => dirname(__DIR__, 2) . '/var/twig',
44
            ]
45
        );
46
    }
47
48
    /**
49
     * Returns path to Gorynych templates
50
     * This path must be included in setup process, if you want to take advantage of ApiGenerator
51
     *
52
     * @return string
53
     */
54
    final protected function getGorynychTemplatesPath(): string
55
    {
56
        return dirname(__DIR__, 2) . '/templates';
57
    }
58
}
59