Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 39 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| HostDbConfigRepository | |
0.00% |
0 / 39 |
|
0.00% |
0 / 3 |
30 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| findByHostId | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
6 | |||
| save | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Domain\Host\Repository; |
| 6 | |
| 7 | use App\Domain\Host\Entity\HostDbConfig; |
| 8 | use Yiisoft\Db\Connection\ConnectionInterface; |
| 9 | |
| 10 | class HostDbConfigRepository |
| 11 | { |
| 12 | public const TABLE_NAME = '{{%hosts_db}}'; |
| 13 | |
| 14 | public function __construct(private ConnectionInterface $db) {} |
| 15 | |
| 16 | public function findByHostId(string $hostId): ?HostDbConfig |
| 17 | { |
| 18 | $row = $this->db->createCommand('SELECT * FROM ' . self::TABLE_NAME . ' WHERE host_id = :host_id', [':host_id' => $hostId])->queryOne(); |
| 19 | if ($row === null) { |
| 20 | return null; |
| 21 | } |
| 22 | |
| 23 | /** @var array<string, string|int> $row */ |
| 24 | return new HostDbConfig( |
| 25 | (string) $row['id'], |
| 26 | (string) $row['host_id'], |
| 27 | (string) $row['driver'], |
| 28 | (string) $row['host'], |
| 29 | (int) $row['port'], |
| 30 | (string) $row['db_name'], |
| 31 | (string) $row['username'], |
| 32 | (string) $row['password'], |
| 33 | (int) $row['created_at'], |
| 34 | (int) $row['updated_at'] |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | public function save(HostDbConfig $config): void |
| 39 | { |
| 40 | $exists = $this->findByHostId($config->getHostId()); |
| 41 | if ($exists === null) { |
| 42 | $this->db->createCommand()->insert(self::TABLE_NAME, [ |
| 43 | 'id' => $config->getId(), |
| 44 | 'host_id' => $config->getHostId(), |
| 45 | 'driver' => $config->getDriver(), |
| 46 | 'host' => $config->getHost(), |
| 47 | 'port' => $config->getPort(), |
| 48 | 'db_name' => $config->getDbName(), |
| 49 | 'username' => $config->getUsername(), |
| 50 | 'password' => $config->getPassword(), |
| 51 | 'created_at' => $config->getCreatedAt(), |
| 52 | 'updated_at' => $config->getUpdatedAt(), |
| 53 | ])->execute(); |
| 54 | } else { |
| 55 | $this->db->createCommand()->update(self::TABLE_NAME, [ |
| 56 | 'driver' => $config->getDriver(), |
| 57 | 'host' => $config->getHost(), |
| 58 | 'port' => $config->getPort(), |
| 59 | 'db_name' => $config->getDbName(), |
| 60 | 'username' => $config->getUsername(), |
| 61 | 'password' => $config->getPassword(), |
| 62 | 'updated_at' => $config->getUpdatedAt(), |
| 63 | ], ['id' => $config->getId()])->execute(); |
| 64 | } |
| 65 | } |
| 66 | } |