Issues (6)

src/TemplateResolver/TemplateResolver.php (1 issue)

Severity
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
The condition false === $match is always true.
Loading history...
52 1
            return realpath(__DIR__ . '/../../templates');
53
        }
54
55 1
        return $match;
56
    }
57
}
58