Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
45 / 60
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
TranslationRecordRepository
75.00% covered (warning)
75.00%
45 / 60
83.33% covered (warning)
83.33%
5 / 6
11.56
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
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
2
 findById
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
 findByKeyCategoryLanguage
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
6
 save
100.00% covered (success)
100.00%
18 / 18
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\Translation\Repository;
6
7use App\Domain\Translation\Entity\TranslationRecord;
8use Yiisoft\Db\Connection\ConnectionInterface;
9
10 class TranslationRecordRepository implements TranslationRecordRepositoryInterface
11{
12    private const TABLE_NAME = '{{%translations_records}}';
13    private const SQL_SELECT_ALL = 'SELECT * FROM ' . self::TABLE_NAME;
14
15    public function __construct(private ConnectionInterface $db) {}
16
17    /**
18     * @return array<int, TranslationRecord>
19     */
20    public function findAll(): array
21    {
22        /** @var array<int, array<string, string|int>> $rows */
23        $rows = $this->db->createCommand(self::SQL_SELECT_ALL . ' ORDER BY category ASC, message_key ASC')->queryAll();
24        $result = [];
25        foreach ($rows as $row) {
26            $result[] = new TranslationRecord(
27                (string) $row['id'],
28                (string) $row['language_code'],
29                (string) $row['category'],
30                (string) $row['message_key'],
31                (string) $row['message_content'],
32                (int) $row['created_at'],
33                (int) $row['updated_at']
34            );
35        }
36        return $result;
37    }
38
39    public function findById(string $id): ?TranslationRecord
40    {
41        $row = $this->db->createCommand(self::SQL_SELECT_ALL . ' WHERE id = :id', [':id' => $id])->queryOne();
42        if ($row === null) {
43            return null;
44        }
45
46        /** @var array<string, string|int> $row */
47        return new TranslationRecord(
48            (string) $row['id'],
49            (string) $row['language_code'],
50            (string) $row['category'],
51            (string) $row['message_key'],
52            (string) $row['message_content'],
53            (int) $row['created_at'],
54            (int) $row['updated_at']
55        );
56    }
57
58    public function findByKeyCategoryLanguage(string $key, string $category, string $languageCode): ?TranslationRecord
59    {
60        $row = $this->db->createCommand(
61            self::SQL_SELECT_ALL . ' WHERE message_key = :key AND category = :category AND language_code = :lang',
62            [':key' => $key, ':category' => $category, ':lang' => $languageCode]
63        )->queryOne();
64
65        if ($row === null) {
66            return null;
67        }
68
69        /** @var array<string, string|int> $row */
70        return new TranslationRecord(
71            (string) $row['id'],
72            (string) $row['language_code'],
73            (string) $row['category'],
74            (string) $row['message_key'],
75            (string) $row['message_content'],
76            (int) $row['created_at'],
77            (int) $row['updated_at']
78        );
79    }
80
81    public function save(TranslationRecord $record): void
82    {
83        $exists = $this->findById($record->getId());
84        if ($exists === null) {
85            $this->db->createCommand()->insert(self::TABLE_NAME, [
86                'id' => $record->getId(),
87                'language_code' => $record->getLanguageCode(),
88                'category' => $record->getCategory(),
89                'message_key' => $record->getMessageKey(),
90                'message_content' => $record->getMessageContent(),
91                'created_at' => $record->getCreatedAt(),
92                'updated_at' => $record->getUpdatedAt(),
93            ])->execute();
94        } else {
95            $this->db->createCommand()->update(self::TABLE_NAME, [
96                'language_code' => $record->getLanguageCode(),
97                'category' => $record->getCategory(),
98                'message_key' => $record->getMessageKey(),
99                'message_content' => $record->getMessageContent(),
100                'updated_at' => $record->getUpdatedAt(),
101            ], ['id' => $record->getId()])->execute();
102        }
103    }
104
105    public function delete(string $id): void
106    {
107        $this->db->createCommand()->delete(self::TABLE_NAME, ['id' => $id])->execute();
108    }
109}