Lines 93.75% 15 / 16
Functions and Methods 75.00% 3 / 4
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
RegistryFragment 93.75% 15 / 16 75.00% 3 / 4 5.01 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 fromRegistry 100.00% 1 / 1 100.00% 1 / 1 1
 fromDecodedValue 88.88% 8 / 9 0.00% 0 / 1 2.01
 encodeToJson 100.00% 5 / 5 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Ipc\Wire;
6
7use Stewart\Contracts\Registry\Area;
8use Stewart\Contracts\Registry\Collection\AreaCollection;
9use Stewart\Contracts\Registry\Collection\DeviceCollection;
10use Stewart\Contracts\Registry\Collection\FloorCollection;
11use Stewart\Contracts\Registry\Collection\LabelCollection;
12use Stewart\Contracts\Registry\Collection\RegisteredEntityCollection;
13use Stewart\Contracts\Registry\Device;
14use Stewart\Contracts\Registry\Floor;
15use Stewart\Contracts\Registry\Label;
16use Stewart\Contracts\Registry\RegisteredEntity;
17use Stewart\Runtime\Json\JsonFragment;
18use Stewart\Runtime\Json\WireMapper;
19use Stewart\Support\Exception\JsonShapeException;
20use Stewart\Support\State\IndexedRegistry;
21
22final class RegistryFragment implements JsonFragment
23{
24    private ?string $json = null;
25
26    private function __construct(public readonly IndexedRegistry $registry) {}
27
28    public static function fromRegistry(IndexedRegistry $registry): self
29    {
30        return new self($registry);
31    }
32
33    public static function fromDecodedValue(mixed $value, string $path, WireMapper $mapper): static
34    {
35        if (!\is_array($value)) {
36            throw JsonShapeException::wrongType($path, 'an object', get_debug_type($value));
37        }
38
39        return new self(IndexedRegistry::fromParts(
40            AreaCollection::keyedByAreaId($mapper->decodeObjectList(Area::class, $value['areas'] ?? null, $path . '.areas')),
41            FloorCollection::keyedByFloorId($mapper->decodeObjectList(Floor::class, $value['floors'] ?? null, $path . '.floors')),
42            LabelCollection::keyedByLabelId($mapper->decodeObjectList(Label::class, $value['labels'] ?? null, $path . '.labels')),
43            DeviceCollection::keyedByDeviceId($mapper->decodeObjectList(Device::class, $value['devices'] ?? null, $path . '.devices')),
44            RegisteredEntityCollection::keyedByEntityId($mapper->decodeObjectList(RegisteredEntity::class, $value['entities'] ?? null, $path . '.entities')),
45        ));
46    }
47
48    public function encodeToJson(WireMapper $mapper): string
49    {
50        return $this->json ??= '{"areas":' . $mapper->encodeObjectList($this->registry->listAreas()->listValues())
51            . ',"floors":' . $mapper->encodeObjectList($this->registry->listFloors()->listValues())
52            . ',"labels":' . $mapper->encodeObjectList($this->registry->listLabels()->listValues())
53            . ',"devices":' . $mapper->encodeObjectList($this->registry->listDevices()->listValues())
54            . ',"entities":' . $mapper->encodeObjectList($this->registry->listEntities()->listValues()) . '}';
55    }
56}