Test Failed
Pull Request — dev (#1)
by Paweł
02:00
created

Template::resolveTemplatePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace pjpawel\Magis;
4
5
use pjpawel\Magis\Exception\TemplateException;
6
7
/**
8
 * @author Paweł Podgórski <[email protected]>
9
 */
10
class Template
11
{
12
13
    private string $templatePath;
14
    private ?string $content = null;
15
16
    public function __construct(string $templatePath)
17
    {
18
        $this->templatePath = $templatePath;
19
    }
20
21
    /**
22
     * @return string
23
     */
24
    public function getTemplatePath(): string
25
    {
26
        return $this->templatePath;
27
    }
28
29
    /**
30
     * @return string
31
     * @throws TemplateException
32
     */
33
    public function getTemplateContent(): string
34
    {
35
        if ($this->content === null) {
36
            $this->loadTemplateContent();
37
        }
38
        return $this->content;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->content could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
39
    }
40
41
    /**
42
     * @return void
43
     * @throws TemplateException
44
     */
45
    private function loadTemplateContent(): void
46
    {
47
        if (!is_file($this->templatePath)) {
48
            throw new TemplateException('There is no template with given name');
49
        }
50
        $content = file_get_contents($this->templatePath);
51
        if ($content === false) {
52
            throw new TemplateException('Cannot get content of template');
53
        }
54
        $this->content = $content;
55
    }
56
57
    public static function resolveTemplatePath(string $directory, string $name): string
58
    {
59
        return $directory . $name;
60
    }
61
62
}