TemplateRenderer::render()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Frostaly\Template;
6
7
use Frostaly\Template\Contracts\RendererAdapterInterface;
8
9
class TemplateRenderer
10
{
11 2
    public function __construct(
12
        private RendererAdapterInterface $renderer,
13
        private string $extension = 'html',
14
    ) {
15 2
        $this->extension = ltrim($extension, '.');
16
    }
17
18
    /**
19
     * Check whether a template exists.
20
     */
21 2
    public function exists(string $name): bool
22
    {
23 2
        $template = $this->normalizeTemplate($name);
24 2
        return $this->renderer->exists($template);
25
    }
26
27
    /**
28
     * Render a template with the given parameters.
29
     */
30 2
    public function render(string $name, array $params = []): string
31
    {
32 2
        $template = $this->normalizeTemplate($name);
33 2
        return $this->renderer->render($template, $params);
34
    }
35
36
    /**
37
     * Normalize the template's name.
38
     */
39 2
    private function normalizeTemplate(string $name): string
40
    {
41 2
        if (preg_match('#\.[a-z]+$#i', $name)) {
42 1
            return $name;
43
        }
44 1
        return sprintf('%s.%s', $name, $this->extension);
45
    }
46
}
47