Passed
Push — master ( 66c33c...4d8a3f )
by Eric
02:06
created

DatabaseStorage   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 10
dl 0
loc 28
ccs 10
cts 10
cp 1
rs 10
c 1
b 0
f 1
wmc 4
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 PDO;
20
21
final readonly class DatabaseStorage implements StorageInterface
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 21 at column 6
Loading history...
22
{
23 3
    public function __construct(private PDO $pdo, private DatabaseStorageConfig $databaseStorageConfig) {}
24
25
    /**
26
     * @inheritDoc
27
     */
28 3
    #[\Override]
29
    public function loadTemplate(string $templateName): string
30
    {
31 3
        $stmt = $this->pdo->prepare(
32 3
            \sprintf(
33 3
                'SELECT %s FROM %s WHERE %s = :name',
34 3
                $this->databaseStorageConfig->contentField,
35 3
                $this->databaseStorageConfig->tableName,
36 3
                $this->databaseStorageConfig->nameField
37 3
            )
38 3
        );
39
40 3
        $stmt->execute(['name' => $templateName]);
41
42
        /**
43
         * @var false|string $content
44
         */
45 3
        $content = $stmt->fetchColumn();
46
47 3
        if ($content === false) {
48 1
            throw TemplateNotFoundException::forDatabaseTemplate($templateName);
49
        }
50
51 2
        if ($content === '') {
52 1
            throw TemplateHasNoContentException::create($templateName);
53
        }
54
55 1
        return $content;
56
    }
57
}
58