Lines 74.28% 26 / 35
Functions and Methods 60.86% 14 / 23
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
InMemoryRegistry 74.28% 26 / 35 60.86% 14 / 23 31.99 0.00% 0 / 1
 seedArea 100.00% 2 / 2 100.00% 1 / 1 1
 seedFloor 100.00% 2 / 2 100.00% 1 / 1 1
 seedLabel 100.00% 2 / 2 100.00% 1 / 1 1
 seedDevice 100.00% 2 / 2 100.00% 1 / 1 1
 seedEntity 100.00% 2 / 2 100.00% 1 / 1 1
 listAreas 100.00% 1 / 1 100.00% 1 / 1 1
 findArea 100.00% 1 / 1 100.00% 1 / 1 1
 findAreaByName 100.00% 1 / 1 100.00% 1 / 1 1
 listAreasOnFloor 100.00% 1 / 1 100.00% 1 / 1 1
 listFloors 0.00% 0 / 1 0.00% 0 / 1 2
 findFloor 0.00% 0 / 1 0.00% 0 / 1 2
 findFloorByName 0.00% 0 / 1 0.00% 0 / 1 2
 listLabels 0.00% 0 / 1 0.00% 0 / 1 2
 findLabel 0.00% 0 / 1 0.00% 0 / 1 2
 findLabelByName 0.00% 0 / 1 0.00% 0 / 1 2
 listDevices 0.00% 0 / 1 0.00% 0 / 1 2
 findDevice 100.00% 1 / 1 100.00% 1 / 1 1
 listDevicesInArea 100.00% 1 / 1 100.00% 1 / 1 1
 listEntities 0.00% 0 / 1 0.00% 0 / 1 2
 findEntity 0.00% 0 / 1 0.00% 0 / 1 2
 findEntityPlacement 100.00% 1 / 1 100.00% 1 / 1 1
 getIndexedRegistry 100.00% 7 / 7 100.00% 1 / 1 1
 invalidateIndex 100.00% 2 / 2 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Testing\Registry;
6
7use Stewart\Contracts\Entity\EntityId;
8use Stewart\Contracts\Registry\Area;
9use Stewart\Contracts\Registry\AreaId;
10use Stewart\Contracts\Registry\Collection\AreaCollection;
11use Stewart\Contracts\Registry\Collection\DeviceCollection;
12use Stewart\Contracts\Registry\Collection\FloorCollection;
13use Stewart\Contracts\Registry\Collection\LabelCollection;
14use Stewart\Contracts\Registry\Collection\RegisteredEntityCollection;
15use Stewart\Contracts\Registry\Device;
16use Stewart\Contracts\Registry\DeviceId;
17use Stewart\Contracts\Registry\EntityPlacement;
18use Stewart\Contracts\Registry\Floor;
19use Stewart\Contracts\Registry\FloorId;
20use Stewart\Contracts\Registry\Label;
21use Stewart\Contracts\Registry\LabelId;
22use Stewart\Contracts\Registry\RegisteredEntity;
23use Stewart\Contracts\Registry\Registry;
24use Stewart\Support\State\IndexedRegistry;
25
26final class InMemoryRegistry implements Registry
27{
28    /** @var array<string, Area> */
29    private array $areas = [];
30
31    /** @var array<string, Floor> */
32    private array $floors = [];
33
34    /** @var array<string, Label> */
35    private array $labels = [];
36
37    /** @var array<string, Device> */
38    private array $devices = [];
39
40    /** @var array<string, RegisteredEntity> */
41    private array $entities = [];
42
43    private ?IndexedRegistry $indexed = null;
44
45    public function seedArea(Area $area): self
46    {
47        $this->areas[$area->areaId->value] = $area;
48
49        return $this->invalidateIndex();
50    }
51
52    public function seedFloor(Floor $floor): self
53    {
54        $this->floors[$floor->floorId->value] = $floor;
55
56        return $this->invalidateIndex();
57    }
58
59    public function seedLabel(Label $label): self
60    {
61        $this->labels[$label->labelId->value] = $label;
62
63        return $this->invalidateIndex();
64    }
65
66    public function seedDevice(Device $device): self
67    {
68        $this->devices[$device->deviceId->value] = $device;
69
70        return $this->invalidateIndex();
71    }
72
73    public function seedEntity(RegisteredEntity $entity): self
74    {
75        $this->entities[$entity->entityId->value] = $entity;
76
77        return $this->invalidateIndex();
78    }
79
80    public function listAreas(): AreaCollection
81    {
82        return $this->getIndexedRegistry()->listAreas();
83    }
84
85    public function findArea(AreaId|string $areaId): ?Area
86    {
87        return $this->getIndexedRegistry()->findArea($areaId);
88    }
89
90    public function findAreaByName(string $name): ?Area
91    {
92        return $this->getIndexedRegistry()->findAreaByName($name);
93    }
94
95    public function listAreasOnFloor(FloorId|string $floorId): AreaCollection
96    {
97        return $this->getIndexedRegistry()->listAreasOnFloor($floorId);
98    }
99
100    public function listFloors(): FloorCollection
101    {
102        return $this->getIndexedRegistry()->listFloors();
103    }
104
105    public function findFloor(FloorId|string $floorId): ?Floor
106    {
107        return $this->getIndexedRegistry()->findFloor($floorId);
108    }
109
110    public function findFloorByName(string $name): ?Floor
111    {
112        return $this->getIndexedRegistry()->findFloorByName($name);
113    }
114
115    public function listLabels(): LabelCollection
116    {
117        return $this->getIndexedRegistry()->listLabels();
118    }
119
120    public function findLabel(LabelId|string $labelId): ?Label
121    {
122        return $this->getIndexedRegistry()->findLabel($labelId);
123    }
124
125    public function findLabelByName(string $name): ?Label
126    {
127        return $this->getIndexedRegistry()->findLabelByName($name);
128    }
129
130    public function listDevices(): DeviceCollection
131    {
132        return $this->getIndexedRegistry()->listDevices();
133    }
134
135    public function findDevice(DeviceId|string $deviceId): ?Device
136    {
137        return $this->getIndexedRegistry()->findDevice($deviceId);
138    }
139
140    public function listDevicesInArea(AreaId|string $areaId): DeviceCollection
141    {
142        return $this->getIndexedRegistry()->listDevicesInArea($areaId);
143    }
144
145    public function listEntities(): RegisteredEntityCollection
146    {
147        return $this->getIndexedRegistry()->listEntities();
148    }
149
150    public function findEntity(EntityId|string $entityId): ?RegisteredEntity
151    {
152        return $this->getIndexedRegistry()->findEntity($entityId);
153    }
154
155    public function findEntityPlacement(EntityId|string $entityId): EntityPlacement
156    {
157        return $this->getIndexedRegistry()->findEntityPlacement($entityId);
158    }
159
160    private function getIndexedRegistry(): IndexedRegistry
161    {
162        return $this->indexed ??= IndexedRegistry::fromParts(
163            AreaCollection::keyedByAreaId($this->areas),
164            FloorCollection::keyedByFloorId($this->floors),
165            LabelCollection::keyedByLabelId($this->labels),
166            DeviceCollection::keyedByDeviceId($this->devices),
167            RegisteredEntityCollection::keyedByEntityId($this->entities),
168        );
169    }
170
171    private function invalidateIndex(): self
172    {
173        $this->indexed = null;
174
175        return $this;
176    }
177}