Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
31 / 31 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| ApiRecordRepository | |
100.00% |
31 / 31 |
|
100.00% |
4 / 4 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| findByHostId | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
| save | |
100.00% |
17 / 17 |
|
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\Api\Repository; |
| 6 | |
| 7 | use App\Domain\Api\Entity\ApiRecord; |
| 8 | use Yiisoft\Db\Connection\ConnectionInterface; |
| 9 | |
| 10 | class ApiRecordRepository |
| 11 | { |
| 12 | public const TABLE_NAME = '{{%api_records}}'; |
| 13 | public function __construct(private ConnectionInterface $db) {} |
| 14 | |
| 15 | public function findByHostId(string $hostId): ?ApiRecord |
| 16 | { |
| 17 | $row = $this->db->createCommand('SELECT * FROM {{%api_records}} WHERE host_id = :host_id', [':host_id' => $hostId])->queryOne(); |
| 18 | if ($row === null) { |
| 19 | return null; |
| 20 | } |
| 21 | |
| 22 | /** @var array<string, string|int> $row */ |
| 23 | return new ApiRecord( |
| 24 | (string) $row['id'], |
| 25 | (string) $row['host_id'], |
| 26 | (string) $row['api_key'], |
| 27 | (string) $row['endpoint_url'], |
| 28 | (string) $row['status'], |
| 29 | (int) $row['created_at'], |
| 30 | (int) $row['updated_at'] |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | public function save(ApiRecord $record): void |
| 35 | { |
| 36 | $exists = $this->findByHostId($record->getHostId()); |
| 37 | if ($exists === null) { |
| 38 | $this->db->createCommand()->insert(self::TABLE_NAME, [ |
| 39 | 'id' => $record->getId(), |
| 40 | 'host_id' => $record->getHostId(), |
| 41 | 'api_key' => $record->getApiKey(), |
| 42 | 'endpoint_url' => $record->getEndpointUrl(), |
| 43 | 'status' => $record->getStatus(), |
| 44 | 'created_at' => $record->getCreatedAt(), |
| 45 | 'updated_at' => $record->getUpdatedAt(), |
| 46 | ])->execute(); |
| 47 | } else { |
| 48 | $this->db->createCommand()->update(self::TABLE_NAME, [ |
| 49 | 'api_key' => $record->getApiKey(), |
| 50 | 'endpoint_url' => $record->getEndpointUrl(), |
| 51 | 'status' => $record->getStatus(), |
| 52 | 'updated_at' => $record->getUpdatedAt(), |
| 53 | ], ['host_id' => $record->getHostId()])->execute(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | public function delete(string $id): void |
| 58 | { |
| 59 | $this->db->createCommand()->delete(self::TABLE_NAME, ['id' => $id])->execute(); |
| 60 | } |
| 61 | } |