Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
MenuRecordRepository
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
5 / 5
8
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%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
 findById
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 save
100.00% covered (success)
100.00%
14 / 14
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\Menu\Repository;
6
7use App\Domain\Menu\Entity\MenuRecord;
8use Yiisoft\Db\Connection\ConnectionInterface;
9
10 class MenuRecordRepository
11{
12    private const TABLE_NAME = '{{%menu_records}}';
13
14    public function __construct(private ConnectionInterface $db) {}
15
16    /**
17     * @return array<int, MenuRecord>
18     */
19    public function findAll(): array
20    {
21        /** @var array<int, array<string, string|int>> $rows */
22        $rows = $this->db->createCommand('SELECT * FROM ' . self::TABLE_NAME . ' ORDER BY name ASC')->queryAll();
23        $result = [];
24        foreach ($rows as $row) {
25            $result[] = new MenuRecord(
26                (string) $row['id'],
27                (string) $row['name'],
28                (string) $row['status'],
29                (int) $row['created_at'],
30                (int) $row['updated_at']
31            );
32        }
33        return $result;
34    }
35
36    public function findById(string $id): ?MenuRecord
37    {
38        $row = $this->db->createCommand('SELECT * FROM ' . self::TABLE_NAME . ' WHERE id = :id', [':id' => $id])->queryOne();
39        if ($row === null) {
40            return null;
41        }
42
43        /** @var array<string, string|int> $row */
44        return new MenuRecord(
45            (string) $row['id'],
46            (string) $row['name'],
47            (string) $row['status'],
48            (int) $row['created_at'],
49            (int) $row['updated_at']
50        );
51    }
52
53    public function save(MenuRecord $record): void
54    {
55        $exists = $this->findById($record->getId());
56        if ($exists === null) {
57            $this->db->createCommand()->insert(self::TABLE_NAME, [
58                'id' => $record->getId(),
59                'name' => $record->getName(),
60                'status' => $record->getStatus(),
61                'created_at' => $record->getCreatedAt(),
62                'updated_at' => $record->getUpdatedAt(),
63            ])->execute();
64        } else {
65            $this->db->createCommand()->update(self::TABLE_NAME, [
66                'name' => $record->getName(),
67                'status' => $record->getStatus(),
68                'updated_at' => $record->getUpdatedAt(),
69            ], ['id' => $record->getId()])->execute();
70        }
71    }
72
73    public function delete(string $id): void
74    {
75        $this->db->createCommand()->delete(self::TABLE_NAME, ['id' => $id])->execute();
76    }
77}