Lines 100.00% 48 / 48
Methods 100.00% 6 / 6
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 decodeAreaOrSkip 100.00% 11 / 11 100.00% 1 / 1 2
 decodeFloorOrSkip 100.00% 11 / 11 100.00% 1 / 1 3
 decodeLabelOrSkip 100.00% 10 / 10 100.00% 1 / 1 2
 decodeDeviceOrSkip 100.00% 13 / 13 100.00% 1 / 1 2
 skipRow 100.00% 2 / 2 100.00% 1 / 1 1
18final readonly class RegistryDecoder
19{
20    public function __construct(private LoggerInterface $logger = new NullLogger()) {}
21
22    /** @param array<array-key, mixed> $raw */
23    public function decodeAreaOrSkip(array $raw): ?Area
24    {
25        $areaId = AreaId::tryFromString(RegistryRow::readString($raw, 'area_id') ?? '');
26
27        if ($areaId === null) {
28            return $this->skipRow('area');
29        }
30
31        return new Area(
32            areaId: $areaId,
33            name: RegistryRow::readString($raw, 'name') ?? $areaId->value,
34            floorId: FloorId::tryFromString(RegistryRow::readString($raw, 'floor_id') ?? ''),
35            aliases: RegistryRow::readStrings($raw, 'aliases'),
36            labelIds: RegistryRow::readLabelIds($raw)->listValues(),
37            icon: RegistryRow::readString($raw, 'icon'),
38        );
39    }
40
41    /** @param array<array-key, mixed> $raw */
42    public function decodeFloorOrSkip(array $raw): ?Floor
43    {
44        $floorId = FloorId::tryFromString(RegistryRow::readString($raw, 'floor_id') ?? '');
45
46        if ($floorId === null) {
47            return $this->skipRow('floor');
48        }
49
50        $level = $raw['level'] ?? null;
51
52        return new Floor(
53            floorId: $floorId,
54            name: RegistryRow::readString($raw, 'name') ?? $floorId->value,
55            level: \is_int($level) ? $level : null,
56            aliases: RegistryRow::readStrings($raw, 'aliases'),
57            icon: RegistryRow::readString($raw, 'icon'),
58        );
59    }
60
61    /** @param array<array-key, mixed> $raw */
62    public function decodeLabelOrSkip(array $raw): ?Label
63    {
64        $labelId = LabelId::tryFromString(RegistryRow::readString($raw, 'label_id') ?? '');
65
66        if ($labelId === null) {
67            return $this->skipRow('label');
68        }
69
70        return new Label(
71            labelId: $labelId,
72            name: RegistryRow::readString($raw, 'name') ?? $labelId->value,
73            color: RegistryRow::readString($raw, 'color'),
74            icon: RegistryRow::readString($raw, 'icon'),
75            description: RegistryRow::readString($raw, 'description'),
76        );
77    }
78
79    /** @param array<array-key, mixed> $raw */
80    public function decodeDeviceOrSkip(array $raw): ?Device
81    {
82        $deviceId = DeviceId::tryFromString(RegistryRow::readString($raw, 'id') ?? '');
83
84        if ($deviceId === null) {
85            return $this->skipRow('device');
86        }
87
88        return new Device(
89            deviceId: $deviceId,
90            name: RegistryRow::readString($raw, 'name'),
91            nameByUser: RegistryRow::readString($raw, 'name_by_user'),
92            areaId: AreaId::tryFromString(RegistryRow::readString($raw, 'area_id') ?? ''),
93            labelIds: RegistryRow::readLabelIds($raw)->listValues(),
94            manufacturer: RegistryRow::readString($raw, 'manufacturer'),
95            model: RegistryRow::readString($raw, 'model'),
96            disabledBy: RegistryRow::readString($raw, 'disabled_by'),
97        );
98    }
99
100    private function skipRow(string $registry): null
101    {
102        $this->logger->debug('Skipped a {registry} registry entry without an id', ['registry' => $registry]);
103
104        return null;
105    }
106}