Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DocumentTemplateRepository
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 findActive
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3declare(strict_types=1);
4
5namespace App\Domain\Document\Repository;
6
7use App\Domain\Document\Data\DocumentTemplateData;
8use App\Support\Row;
9use PDO;
10use RuntimeException;
11
12final readonly class DocumentTemplateRepository
13{
14    private const string SELECT_COLUMNS = '
15        template_id    AS "templateId",
16        name,
17        description,
18        active,
19        display_order  AS "displayOrder",
20        recipient_role AS "recipientRole",
21        requires_id_verification AS "requiresIdVerification",
22        created_at     AS "createdAt",
23        updated_at     AS "updatedAt"
24    ';
25
26    public function __construct(
27        private PDO $pdo,
28    ) {}
29
30    /**
31     * @return list<DocumentTemplateData>
32     */
33    public function findActive(): array
34    {
35        $stmt = $this->pdo->query(
36            'SELECT ' . self::SELECT_COLUMNS . '
37             FROM document_templates
38             WHERE active = TRUE
39             ORDER BY display_order, name',
40        );
41
42        if ($stmt === false) {
43            throw new RuntimeException('Failed to query active document templates');
44        }
45
46        $templates = [];
47        foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
48            $templates[] = DocumentTemplateData::fromRow(Row::from($row));
49        }
50
51        return $templates;
52    }
53}