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

TwigAdapter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 16.66%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 43
ccs 2
cts 12
cp 0.1666
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getGorynychTemplatesPath() 0 3 1
A render() 0 3 1
A setup() 0 6 1
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