Passed
Push — dev ( 72f23d...60923c )
by Paweł
02:14 queued 14s
created

Template   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 15
c 2
b 0
f 0
dl 0
loc 50
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getTemplateContent() 0 6 2
A __construct() 0 3 1
A getTemplatePath() 0 3 1
A loadTemplateContent() 0 10 3
A resolveTemplatePath() 0 3 1
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
}