Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
HostApiConfigRepository
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 findByHostId
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 save
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace App\Domain\Host\Repository;
6
7use App\Domain\Host\Entity\HostApiConfig;
8use Yiisoft\Db\Connection\ConnectionInterface;
9
10 class HostApiConfigRepository
11{
12    public const TABLE_NAME = '{{%hosts_api}}';
13
14    public function __construct(private ConnectionInterface $db) {}
15
16    public function findByHostId(string $hostId): ?HostApiConfig
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 HostApiConfig(
25            (string) $row['id'],
26            (string) $row['host_id'],
27            (string) $row['base_url'],
28            (string) $row['api_token'],
29            (int) $row['timeout'],
30            (int) $row['created_at'],
31            (int) $row['updated_at']
32        );
33    }
34
35    public function save(HostApiConfig $config): void
36    {
37        $exists = $this->findByHostId($config->getHostId());
38        if ($exists === null) {
39            $this->db->createCommand()->insert(self::TABLE_NAME, [
40                'id' => $config->getId(),
41                'host_id' => $config->getHostId(),
42                'base_url' => $config->getBaseUrl(),
43                'api_token' => $config->getApiToken(),
44                'timeout' => $config->getTimeout(),
45                'created_at' => $config->getCreatedAt(),
46                'updated_at' => $config->getUpdatedAt(),
47            ])->execute();
48        } else {
49            $this->db->createCommand()->update(self::TABLE_NAME, [
50                'base_url' => $config->getBaseUrl(),
51                'api_token' => $config->getApiToken(),
52                'timeout' => $config->getTimeout(),
53                'updated_at' => $config->getUpdatedAt(),
54            ], ['id' => $config->getId()])->execute();
55        }
56    }
57}