Passed
Push — master ( 4eb83c...d278df )
by Eric
02:17
created

FilesystemStorage::getTemplatePath()   A

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
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Esi\SimpleTpl.
7
 *
8
 * (c) 2006 - 2025 Eric Sizemore <[email protected]>
9
 *
10
 * This file is licensed under The MIT License. For the full copyright and
11
 * license information, please view the LICENSE.md file that was distributed
12
 * with this source code.
13
 */
14
15
namespace Esi\SimpleTpl\Storage;
16
17
use Esi\SimpleTpl\Exception\TemplateHasNoContentException;
18
use Esi\SimpleTpl\Exception\TemplateNotFoundException;
19
20
use function file_get_contents;
21
use function is_file;
22
use function is_readable;
23
use function rtrim;
24
25
use const DIRECTORY_SEPARATOR;
26
27
readonly class FilesystemStorage implements StorageInterface
28
{
29
    /**
30
     * The format used by {@see getTemplatePath()}.
31
     *
32
     * @const string
33
     */
34
    private const TemplatePathString = '%s%s.tpl';
35
36
    /**
37
     * The directory/folder containing template files.
38
     */
39
    private string $templateDir;
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param string $templateDir The directory/folder containing template files.
45
     */
46 16
    public function __construct(string $templateDir)
47
    {
48 16
        $this->templateDir = rtrim($templateDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54 11
    #[\Override]
55
    public function loadTemplate(string $templateName): string
56
    {
57 11
        return $this->readFile($this->getTemplatePath($templateName));
58
    }
59
60
    /**
61
     * Builds the full path to a given template.
62
     *
63
     * @param string $templateName The name of the template.
64
     *
65
     * @return string The full path to the template.
66
     */
67 11
    private function getTemplatePath(string $templateName): string
68
    {
69 11
        return \sprintf(self::TemplatePathString, $this->templateDir, $templateName);
70
    }
71
72
    /**
73
     * Reads the content of the template file.
74
     *
75
     * @param string $templatePath The path to the template file.
76
     *
77
     * @throws TemplateHasNoContentException If the file has no valid content.
78
     * @throws TemplateNotFoundException     If the file does not exist or is not readable.
79
     *
80
     * @return string The content of the template file.
81
     */
82 11
    private function readFile(string $templatePath): string
83
    {
84 11
        $this->validateFile($templatePath);
85
86 8
        $contents = file_get_contents($templatePath);
87
88 8
        if ($contents === '' || $contents === false) {
89 1
            throw TemplateHasNoContentException::create($templatePath);
90
        }
91
92 7
        return $contents;
93
    }
94
95
    /**
96
     * Validates the template file.
97
     *
98
     * @param string $templatePath The path to the template file.
99
     *
100
     * @throws TemplateNotFoundException If the file does not exist or is not readable.
101
     */
102 11
    private function validateFile(string $templatePath): void
103
    {
104 11
        if (!is_file($templatePath) || !is_readable($templatePath)) {
105 3
            throw TemplateNotFoundException::forFilesystemTemplate($templatePath);
106
        }
107
    }
108
}
109