Passed
Push — master ( cc3241...e24646 )
by Eric
01:01 queued 13s
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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A loadTemplate() 0 21 3
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
readonly class DatabaseStorage implements StorageInterface
22
{
23 3
    public function __construct(private PDO $pdo) {}
24
25
    /**
26
     * @inheritDoc
27
     */
28 3
    #[\Override]
29
    public function loadTemplate(string $templateName): string
30
    {
31 3
        $stmt = $this->pdo->prepare('SELECT content FROM templates WHERE name = :name');
32
33 3
        $stmt->execute(['name' => $templateName]);
34
35
        /**
36
         * @var false|string $content
37
         */
38 3
        $content = $stmt->fetchColumn();
39
40 3
        if ($content === false) {
41 1
            throw TemplateNotFoundException::forDatabaseTemplate($templateName);
42
        }
43
44 2
        if ($content === '') {
45 1
            throw TemplateHasNoContentException::create($templateName);
46
        }
47
48 1
        return $content;
49
    }
50
}
51