Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.00% covered (success)
90.00%
18 / 20
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
DocumentTypeRepository
90.00% covered (success)
90.00%
18 / 20
33.33% covered (danger)
33.33%
1 / 3
8.06
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 findActive
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
4.01
 findById
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
3.02
1<?php
2
3declare(strict_types=1);
4
5namespace App\Domain\Document\Type\Repository;
6
7use App\Domain\Document\Type\Data\DocumentTypeData;
8use PDO;
9use RuntimeException;
10
11final readonly class DocumentTypeRepository
12{
13    public function __construct(
14        private PDO $pdo,
15    ) {}
16
17    /**
18     * @return list<DocumentTypeData>
19     */
20    public function findActive(): array
21    {
22        $sql = <<<SQL
23                SELECT
24                    document_type_id   AS "documentTypeId",
25                    code,
26                    label,
27                    description,
28                    allowed_mime_types AS "allowedMimeTypes",
29                    max_size_bytes     AS "maxSizeBytes",
30                    requires_review    AS "requiresReview",
31                    is_active          AS "isActive",
32                    sort_order         AS "sortOrder"
33                FROM document_types
34                WHERE is_active = TRUE
35                ORDER BY sort_order, label
36            SQL;
37
38        $stmt = $this->pdo->prepare($sql);
39        if ($stmt === false) {
40            throw new RuntimeException('Failed to prepare statement');
41        }
42        $stmt->execute();
43
44        $types = [];
45        foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
46            if (is_array($row)) {
47                $types[] = DocumentTypeData::fromRow($row);
48            }
49        }
50
51        return $types;
52    }
53
54    public function findById(int $documentTypeId): ?DocumentTypeData
55    {
56        $sql = <<<SQL
57                SELECT
58                    document_type_id   AS "documentTypeId",
59                    code,
60                    label,
61                    description,
62                    allowed_mime_types AS "allowedMimeTypes",
63                    max_size_bytes     AS "maxSizeBytes",
64                    requires_review    AS "requiresReview",
65                    is_active          AS "isActive",
66                    sort_order         AS "sortOrder"
67                FROM document_types
68                WHERE document_type_id = :id
69            SQL;
70
71        $stmt = $this->pdo->prepare($sql);
72        if ($stmt === false) {
73            throw new RuntimeException('Failed to prepare statement');
74        }
75        $stmt->execute(['id' => $documentTypeId]);
76        $row = $stmt->fetch(PDO::FETCH_ASSOC);
77
78        return is_array($row) ? DocumentTypeData::fromRow($row) : null;
79    }
80}