Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
2.17% |
1 / 46 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
| MenuSortRepository | |
2.17% |
1 / 46 |
|
20.00% |
1 / 5 |
171.22 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| findAll | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
20 | |||
| save | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
6 | |||
| replaceTree | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 | |||
| delete | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Domain\Menu\Repository; |
| 6 | |
| 7 | use App\Domain\Menu\Entity\MenuSort; |
| 8 | use Yiisoft\Db\Connection\ConnectionInterface; |
| 9 | |
| 10 | class MenuSortRepository |
| 11 | { |
| 12 | public function __construct(private ConnectionInterface $db) {} |
| 13 | |
| 14 | /** |
| 15 | * @return array<int, MenuSort> |
| 16 | */ |
| 17 | public function findAll(string $sortTableName = 'srv_menu_sort'): array |
| 18 | { |
| 19 | $rawName = str_replace(['{{%', '}}', '`'], '', $sortTableName); |
| 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 {$sortTableName} ORDER BY sort_order ASC, created_at ASC")->queryAll(); |
| 26 | $result = []; |
| 27 | foreach ($rows as $row) { |
| 28 | $result[] = new MenuSort( |
| 29 | (string) $row['id'], |
| 30 | (string) $row['menu_record_id'], |
| 31 | $row['parent_id'] !== null ? (string) $row['parent_id'] : null, |
| 32 | (int) $row['sort_order'], |
| 33 | (int) $row['created_at'], |
| 34 | (int) $row['updated_at'] |
| 35 | ); |
| 36 | } |
| 37 | return $result; |
| 38 | } |
| 39 | |
| 40 | public function save(MenuSort $item, string $sortTableName = 'srv_menu_sort'): void |
| 41 | { |
| 42 | $row = $this->db->createCommand("SELECT id FROM {$sortTableName} WHERE id = :id", [':id' => $item->getId()])->queryOne(); |
| 43 | if ($row === null) { |
| 44 | $this->db->createCommand()->insert($sortTableName, [ |
| 45 | 'id' => $item->getId(), |
| 46 | 'menu_record_id' => $item->getMenuRecordId(), |
| 47 | 'parent_id' => $item->getParentId(), |
| 48 | 'sort_order' => $item->getSortOrder(), |
| 49 | 'created_at' => $item->getCreatedAt(), |
| 50 | 'updated_at' => $item->getUpdatedAt(), |
| 51 | ])->execute(); |
| 52 | } else { |
| 53 | $this->db->createCommand()->update($sortTableName, [ |
| 54 | 'menu_record_id' => $item->getMenuRecordId(), |
| 55 | 'parent_id' => $item->getParentId(), |
| 56 | 'sort_order' => $item->getSortOrder(), |
| 57 | 'updated_at' => $item->getUpdatedAt(), |
| 58 | ], ['id' => $item->getId()])->execute(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param array<int, array{id: string, menu_record_id: string, parent_id: ?string, sort_order: int}> $treeData |
| 64 | */ |
| 65 | public function replaceTree(array $treeData, string $sortTableName = 'srv_menu_sort'): void |
| 66 | { |
| 67 | $rawName = str_replace(['{{%', '}}', '`'], '', $sortTableName); |
| 68 | if ($this->db->getSchema()->getTableSchema($rawName) === null) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | $this->db->createCommand("DELETE FROM {$sortTableName}")->execute(); |
| 73 | |
| 74 | foreach ($treeData as $item) { |
| 75 | $this->db->createCommand()->insert($sortTableName, [ |
| 76 | 'id' => $item['id'] !== '' ? $item['id'] : 'sort-' . bin2hex(random_bytes(4)), |
| 77 | 'menu_record_id' => $item['menu_record_id'], |
| 78 | 'parent_id' => $item['parent_id'] !== '' ? $item['parent_id'] : null, |
| 79 | 'sort_order' => (int) $item['sort_order'], |
| 80 | 'created_at' => time(), |
| 81 | 'updated_at' => time(), |
| 82 | ])->execute(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public function delete(string $id, string $sortTableName = 'srv_menu_sort'): void |
| 87 | { |
| 88 | $this->db->createCommand()->delete($sortTableName, ['id' => $id])->execute(); |
| 89 | } |
| 90 | } |