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