Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
39 / 39 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| MenuTreeRepository | |
100.00% |
39 / 39 |
|
100.00% |
4 / 4 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| findByMenuId | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
3 | |||
| save | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
2 | |||
| delete | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Domain\Menu\Repository; |
| 6 | |
| 7 | use App\Domain\Menu\Entity\MenuTree; |
| 8 | use Yiisoft\Db\Connection\ConnectionInterface; |
| 9 | |
| 10 | class MenuTreeRepository |
| 11 | { |
| 12 | private const TABLE_NAME = '{{%menu_tree}}'; |
| 13 | |
| 14 | public function __construct(private ConnectionInterface $db) {} |
| 15 | |
| 16 | /** |
| 17 | * @return array<int, MenuTree> |
| 18 | */ |
| 19 | public function findByMenuId(string $menuId): array |
| 20 | { |
| 21 | /** @var array<int, array<string, string|int|null>> $rows */ |
| 22 | $rows = $this->db->createCommand( |
| 23 | 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE menu_id = :menu_id ORDER BY sort_order ASC', |
| 24 | [':menu_id' => $menuId] |
| 25 | )->queryAll(); |
| 26 | |
| 27 | $result = []; |
| 28 | foreach ($rows as $row) { |
| 29 | $result[] = new MenuTree( |
| 30 | (string) $row['id'], |
| 31 | (string) $row['menu_id'], |
| 32 | $row['parent_id'] !== null ? (string) $row['parent_id'] : null, |
| 33 | (string) $row['label'], |
| 34 | (string) $row['url_or_route'], |
| 35 | (int) $row['sort_order'], |
| 36 | (int) $row['created_at'], |
| 37 | (int) $row['updated_at'] |
| 38 | ); |
| 39 | } |
| 40 | return $result; |
| 41 | } |
| 42 | |
| 43 | public function save(MenuTree $item): void |
| 44 | { |
| 45 | $row = $this->db->createCommand('SELECT id FROM ' . self::TABLE_NAME . ' WHERE id = :id', [':id' => $item->getId()])->queryOne(); |
| 46 | if ($row === null) { |
| 47 | $this->db->createCommand()->insert(self::TABLE_NAME, [ |
| 48 | 'id' => $item->getId(), |
| 49 | 'menu_id' => $item->getMenuId(), |
| 50 | 'parent_id' => $item->getParentId(), |
| 51 | 'label' => $item->getLabel(), |
| 52 | 'url_or_route' => $item->getUrlOrRoute(), |
| 53 | 'sort_order' => $item->getSortOrder(), |
| 54 | 'created_at' => $item->getCreatedAt(), |
| 55 | 'updated_at' => $item->getUpdatedAt(), |
| 56 | ])->execute(); |
| 57 | } else { |
| 58 | $this->db->createCommand()->update(self::TABLE_NAME, [ |
| 59 | 'menu_id' => $item->getMenuId(), |
| 60 | 'parent_id' => $item->getParentId(), |
| 61 | 'label' => $item->getLabel(), |
| 62 | 'url_or_route' => $item->getUrlOrRoute(), |
| 63 | 'sort_order' => $item->getSortOrder(), |
| 64 | 'updated_at' => $item->getUpdatedAt(), |
| 65 | ], ['id' => $item->getId()])->execute(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | public function delete(string $id): void |
| 70 | { |
| 71 | $this->db->createCommand()->delete(self::TABLE_NAME, ['id' => $id])->execute(); |
| 72 | } |
| 73 | } |