Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.43% covered (success)
96.43%
54 / 56
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
MenuRecordRepository
96.43% covered (success)
96.43%
54 / 56
66.67% covered (warning)
66.67%
4 / 6
16
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
 findAll
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
3.02
 findById
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
 save
100.00% covered (success)
100.00%
26 / 26
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
 mapRowToRecord
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace App\Domain\Menu\Repository;
6
7use App\Domain\Menu\Entity\MenuRecord;
8use Yiisoft\Db\Connection\ConnectionInterface;
9
10 class MenuRecordRepository
11{
12    public function __construct(private ConnectionInterface $db) {}
13
14    /**
15     * @return array<int, MenuRecord>
16     */
17    public function findAll(string $tableName = 'srv_menu_records'): 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|null>> $rows */
25        $rows = $this->db->createCommand("SELECT * FROM {$tableName} ORDER BY created_at ASC")->queryAll();
26        $result = [];
27        foreach ($rows as $row) {
28            $result[] = $this->mapRowToRecord($row);
29        }
30        return $result;
31    }
32
33    public function findById(string $id, string $tableName = 'srv_menu_records'): ?MenuRecord
34    {
35        $rawName = str_replace(['{{%', '}}', '`'], '', $tableName);
36        if ($this->db->getSchema()->getTableSchema($rawName) === null) {
37            return null;
38        }
39
40        $row = $this->db->createCommand("SELECT * FROM {$tableName} WHERE id = :id", [':id' => $id])->queryOne();
41        if ($row === null) {
42            return null;
43        }
44
45        /** @var array<string, string|int|null> $row */
46        return $this->mapRowToRecord($row);
47    }
48
49    public function save(MenuRecord $record, string $tableName = 'srv_menu_records'): void
50    {
51        $exists = $this->findById($record->getId(), $tableName);
52        if ($exists === null) {
53            $this->db->createCommand()->insert($tableName, [
54                'id' => $record->getId(),
55                'type' => $record->getType(),
56                'title' => $record->getTitle(),
57                'icon' => $record->getIcon(),
58                'url' => $record->getUrl(),
59                'badge_text' => $record->getBadgeText(),
60                'badge_color' => $record->getBadgeColor(),
61                'target' => $record->getTarget(),
62                'status' => $record->getStatus(),
63                'created_at' => $record->getCreatedAt(),
64                'updated_at' => $record->getUpdatedAt(),
65            ])->execute();
66        } else {
67            $this->db->createCommand()->update($tableName, [
68                'type' => $record->getType(),
69                'title' => $record->getTitle(),
70                'icon' => $record->getIcon(),
71                'url' => $record->getUrl(),
72                'badge_text' => $record->getBadgeText(),
73                'badge_color' => $record->getBadgeColor(),
74                'target' => $record->getTarget(),
75                'status' => $record->getStatus(),
76                'updated_at' => $record->getUpdatedAt(),
77            ], ['id' => $record->getId()])->execute();
78        }
79    }
80
81    public function delete(string $id, string $tableName = 'srv_menu_records'): void
82    {
83        $this->db->createCommand()->delete($tableName, ['id' => $id])->execute();
84    }
85
86    /**
87     * @param array<string, string|int|null> $row
88     */
89    private function mapRowToRecord(array $row): MenuRecord
90    {
91        return new MenuRecord(
92            (string) $row['id'],
93            (string) ($row['type'] ?? 'element'),
94            (string) ($row['title'] ?? $row['label'] ?? $row['name'] ?? ''),
95            isset($row['icon']) ? (string) $row['icon'] : null,
96            isset($row['url']) ? (string) $row['url'] : null,
97            isset($row['badge_text']) ? (string) $row['badge_text'] : null,
98            isset($row['badge_color']) ? (string) $row['badge_color'] : null,
99            isset($row['target']) ? (string) $row['target'] : null,
100            (string) ($row['status'] ?? 'active'),
101            (int) ($row['created_at'] ?? time()),
102            (int) ($row['updated_at'] ?? time())
103        );
104    }
105}