Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
59 / 59
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
ModuleRecordRepository
100.00% covered (success)
100.00%
59 / 59
100.00% covered (success)
100.00%
5 / 5
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 findAll
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
3
 findById
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
3
 save
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
2
 delete
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace App\Domain\Module\Repository;
6
7use App\Domain\Module\Entity\ModuleRecord;
8use Yiisoft\Db\Connection\ConnectionInterface;
9
10 class ModuleRecordRepository
11{
12    public function __construct(private ConnectionInterface $db) {}
13
14    /**
15     * @return array<int, ModuleRecord>
16     */
17    public function findAll(string $tableName = 'srv_modules'): array
18    {
19        $rawName = str_replace(['{{%', '}}', '`'], '', $tableName);
20        if ($this->db->getSchema()->getTableSchema($rawName) === null) {
21            return [];
22        }
23
24        /** @var array<int, array<string, string|int>> $rows */
25        $rows = $this->db->createCommand("SELECT * FROM {$tableName} ORDER BY code ASC")->queryAll();
26        $result = [];
27        foreach ($rows as $row) {
28            $result[] = new ModuleRecord(
29                (string) $row['id'],
30                (string) $row['code'],
31                (string) $row['name'],
32                (string) ($row['scope'] ?? 'both'),
33                (string) $row['table_name'],
34                (string) ($row['status'] ?? 'active'),
35                (int) $row['created_at'],
36                (int) $row['updated_at'],
37                (string) ($row['icon'] ?? 'bi-box-seam')
38            );
39        }
40        return $result;
41    }
42
43    public function findById(string $id, string $tableName = 'srv_modules'): ?ModuleRecord
44    {
45        $rawName = str_replace(['{{%', '}}', '`'], '', $tableName);
46        if ($this->db->getSchema()->getTableSchema($rawName) === null) {
47            return null;
48        }
49
50        $row = $this->db->createCommand("SELECT * FROM {$tableName} WHERE id = :id", [':id' => $id])->queryOne();
51        if ($row === null) {
52            return null;
53        }
54
55        /** @var array<string, string|int> $row */
56        return new ModuleRecord(
57            (string) $row['id'],
58            (string) $row['code'],
59            (string) $row['name'],
60            (string) ($row['scope'] ?? 'both'),
61            (string) $row['table_name'],
62            (string) ($row['status'] ?? 'active'),
63            (int) $row['created_at'],
64            (int) $row['updated_at'],
65            (string) ($row['icon'] ?? 'bi-box-seam')
66        );
67    }
68
69    public function save(ModuleRecord $record, string $tableName = 'srv_modules'): void
70    {
71        $exists = $this->findById($record->getId(), $tableName);
72        if ($exists === null) {
73            $this->db->createCommand()->insert($tableName, [
74                'id' => $record->getId(),
75                'code' => $record->getCode(),
76                'name' => $record->getName(),
77                'icon' => $record->getIcon(),
78                'scope' => $record->getScope(),
79                'table_name' => $record->getTableName(),
80                'status' => $record->getStatus(),
81                'created_at' => $record->getCreatedAt(),
82                'updated_at' => $record->getUpdatedAt(),
83            ])->execute();
84        } else {
85            $this->db->createCommand()->update($tableName, [
86                'code' => $record->getCode(),
87                'name' => $record->getName(),
88                'icon' => $record->getIcon(),
89                'scope' => $record->getScope(),
90                'table_name' => $record->getTableName(),
91                'status' => $record->getStatus(),
92                'updated_at' => $record->getUpdatedAt(),
93            ], ['id' => $record->getId()])->execute();
94        }
95    }
96
97    public function delete(string $id, string $tableName = 'srv_modules'): void
98    {
99        $this->db->createCommand()->delete($tableName, ['id' => $id])->execute();
100    }
101}