PugTemplateRenderer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 18
c 3
b 0
f 0
dl 0
loc 61
ccs 19
cts 19
cp 1
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 9 1
A setDefaultParams() 0 3 1
A addPath() 0 3 2
A __construct() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\Render\Phug;
6
7
use Antidot\Render\TemplateRenderer;
8
use Pug\Pug;
9
10
use function array_replace_recursive;
11
use function array_merge_recursive;
12
use function sprintf;
13
use function str_replace;
14
15
class PugTemplateRenderer implements TemplateRenderer
16
{
17
    public const DEFAULT_PATH = 'templates/';
18
    /** @var Pug */
19
    private $pug;
20
    /** @var string */
21
    private $path;
22
    /** @var array<mixed> */
23
    private $globals;
24
    /** @var array<mixed> */
25
    private $config;
26
27
    /**
28
     * PugTemplateRenderer constructor.
29
     * @param Pug $pug
30
     * @param array<mixed> $defaultParams
31
     * @param array<mixed> $globals
32
     * @param array<mixed> $config
33
     */
34 2
    public function __construct(Pug $pug, array $defaultParams, array $globals, array $config)
35
    {
36 2
        $this->pug = $pug;
37 2
        $this->globals = $globals;
38 2
        $this->config = $config;
39 2
        $this->addPath($this->config['template_path']);
40 2
        $this->setDefaultParams($defaultParams);
41 2
    }
42
43
    /**
44
     * @param array<mixed> $defaultParams
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<mixed> $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 2
    private function addPath(?string $path) : void
74
    {
75 2
        $this->path = $path ?: self::DEFAULT_PATH;
76 2
    }
77
}
78