TemplateResolver::resolve()   A
last analyzed

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 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Fabiang\ExceptionGenerator\TemplateResolver;
6
7
use function file_exists;
8
use function realpath;
9
use function rtrim;
10
11
class TemplateResolver
12
{
13
    /**
14
     * Path to template.
15
     */
16
    protected string $templatePath;
17
18
    /**
19
     * TemplatePathMatcher instance.
20
     */
21
    protected TemplatePathMatcher $templatePathMatcher;
22
23
    /**
24
     * transforms received path to a valid realpath
25
     */
26 3
    public function __construct(string $templatePath, TemplatePathMatcher $templatePathMatcher)
27
    {
28 3
        $this->templatePath        = rtrim($templatePath, '/');
29 3
        $this->templatePathMatcher = $templatePathMatcher;
30
    }
31
32
    /**
33
     * resolves path for specific template
34
     */
35 3
    public function resolve(string $templateName): string
36
    {
37 3
        return $this->getTemplatePath($templateName) . '/' . $templateName;
38
    }
39
40
    /**
41
     * trys different paths to resolve a valid template path
42
     */
43 3
    protected function getTemplatePath(string $templateName): string
44
    {
45 3
        if (file_exists($this->templatePath . '/' . $templateName)) {
46 1
            return $this->templatePath;
47
        }
48
49 2
        $match = $this->templatePathMatcher->match($templateName);
50
51 2
        if (false === $match) {
0 ignored issues
show
introduced by
The condition false === $match is always true.
Loading history...
52 1
            return realpath(__DIR__ . '/../../templates');
53
        }
54
55 1
        return $match;
56
    }
57
}
58