Passed
Push — master ( bac7ba...880767 )
by Koldo
03:07
created

PugTemplateRenderer::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Antidot\Render\Phug;
4
5
use Antidot\Render\TemplateRenderer;
6
use Pug\Pug;
7
8
use function array_replace_recursive;
9
use function array_merge_recursive;
10
use function sprintf;
11
use function str_replace;
12
13
class PugTemplateRenderer implements TemplateRenderer
14
{
15
    public const DEFAULT_PATH = 'templates/';
16
17
    /**
18
     * @var Pug
19
     */
20
    private $pug;
21
22
    /**
23
     * @var string
24
     */
25
    private $path;
26
27
    /**
28
     * @var array
29
     */
30
    private $globals;
31
32
    /**
33
     * @var array
34
     */
35
    private $config;
36
37 2
    public function __construct(Pug $pug, array $defaultParams, array $globals, array $config)
38
    {
39 2
        $this->pug = $pug;
40 2
        $this->globals = $globals;
41 2
        $this->config = $config;
42 2
        $this->addPath($this->config['template_path']);
43 2
        $this->setDefaultParams($defaultParams);
44 2
    }
45
46 2
    private function setDefaultParams(array $defaultParams): void
47
    {
48 2
        $this->globals = (array) array_replace_recursive($this->globals, $defaultParams);
49 2
    }
50
51
    /**
52
     * @param string $name
53
     * @param array $params
54
     * @return string
55
     */
56 1
    public function render(string $name, array $params = []) : string
57
    {
58 1
        return $this->pug->render(
59 1
            sprintf(
60 1
                '%s.%s',
61 1
                $this->path . str_replace('::', '/', $name),
62 1
                $this->config['extension']
63
            ),
64 1
            array_merge_recursive($this->globals, $params)
65
        );
66
    }
67
68
    /**
69
     * Add a template path to the engine.
70
     *
71
     * Adds a template path
72
     *
73
     * @param string $path
74
     */
75 2
    private function addPath(string $path) : void
76
    {
77 2
        $this->path = $path ?: self::DEFAULT_PATH;
78 2
    }
79
}
80