TemplateResolver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 11
dl 0
loc 45
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 3 1
A __construct() 0 4 1
A getTemplatePath() 0 13 3
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