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