Passed
Push — master ( cc3241...e24646 )
by Eric
01:01 queued 13s
created

FilesystemStorage::validateFile()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 3
rs 10
c 1
b 0
f 1
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
use InvalidArgumentException;
20
21
use function file_get_contents;
22
use function is_file;
23
use function is_readable;
24
use function rtrim;
25
26
use const DIRECTORY_SEPARATOR;
27
28
class FilesystemStorage implements StorageInterface
29
{
30
    private readonly string $templateDir;
31
32 15
    public function __construct(string $templateDir)
33
    {
34 15
        $this->templateDir = rtrim($templateDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
1 ignored issue
show
Bug introduced by
The property templateDir is declared read-only in Esi\SimpleTpl\Storage\FilesystemStorage.
Loading history...
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40 10
    #[\Override]
41
    public function loadTemplate(string $templateName): string
42
    {
43 10
        $templatePath = \sprintf('%s%s.tpl', $this->templateDir, $templateName);
44
45 10
        return $this->readFile($templatePath);
46
    }
47
48
    /**
49
     * Reads the content of the template file.
50
     *
51
     * @param string $templatePath The path to the template file.
52
     *
53
     * @throws TemplateHasNoContentException If the file has no valid content.
54
     *
55
     * @return string The content of the template file.
56
     */
57 10
    protected function readFile(string $templatePath): string
58
    {
59 10
        $this->validateFile($templatePath);
60
61 7
        $contents = file_get_contents($templatePath);
62
63 7
        if ($contents === '' || $contents === false) {
64 1
            throw TemplateHasNoContentException::create($templatePath);
65
        }
66
67 6
        return $contents;
68
    }
69
70
    /**
71
     * Validates the template file.
72
     *
73
     * @param string $templatePath The path to the template file.
74
     *
75
     * @throws InvalidArgumentException If the file does not exist or is not readable.
76
     */
77 10
    protected function validateFile(string $templatePath): void
78
    {
79 10
        if (!is_file($templatePath) || !is_readable($templatePath)) {
80 3
            throw TemplateNotFoundException::forFilesystemTemplate($templatePath);
81
        }
82
    }
83
}
84