Lines 90.47% 38 / 42
Methods 90.32% 28 / 31
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 assignAppId 0.00% 0 / 1 0.00% 0 / 1 2
 seedSnapshot 100.00% 1 / 1 100.00% 1 / 1 1
 cancelSubscriptions 0.00% 0 / 2 0.00% 0 / 1 6
 refuseExposures 100.00% 1 / 1 100.00% 1 / 1 1
 exposeSensor 100.00% 1 / 1 100.00% 1 / 1 1
 exposeBinarySensor 100.00% 1 / 1 100.00% 1 / 1 1
 exposeSwitch 100.00% 1 / 1 100.00% 1 / 1 1
 exposeButton 100.00% 1 / 1 100.00% 1 / 1 1
 exposeNumber 100.00% 1 / 1 100.00% 1 / 1 1
 exposeSelect 100.00% 1 / 1 100.00% 1 / 1 1
 exposeText 100.00% 1 / 1 100.00% 1 / 1 1
 exposeTime 100.00% 1 / 1 100.00% 1 / 1 1
 exposeDate 100.00% 1 / 1 100.00% 1 / 1 1
 exposeDateTime 100.00% 1 / 1 100.00% 1 / 1 1
 exposeAction 100.00% 1 / 1 100.00% 1 / 1 1
 requireSensor 100.00% 1 / 1 100.00% 1 / 1 1
 requireBinarySensor 0.00% 0 / 1 0.00% 0 / 1 2
 requireSwitch 100.00% 1 / 1 100.00% 1 / 1 1
 requireButton 100.00% 1 / 1 100.00% 1 / 1 1
 requireNumber 100.00% 1 / 1 100.00% 1 / 1 1
 requireSelect 100.00% 1 / 1 100.00% 1 / 1 1
 requireText 100.00% 1 / 1 100.00% 1 / 1 1
 requireTime 100.00% 1 / 1 100.00% 1 / 1 1
 requireDate 100.00% 1 / 1 100.00% 1 / 1 1
 requireDateTime 100.00% 1 / 1 100.00% 1 / 1 1
 requireAction 100.00% 1 / 1 100.00% 1 / 1 1
 claimKey 100.00% 7 / 7 100.00% 1 / 1 3
 trackHandle 100.00% 4 / 4 100.00% 1 / 1 3
 requireHandle 100.00% 2 / 2 100.00% 1 / 1 2
 findHandle 100.00% 1 / 1 100.00% 1 / 1 1
40final class RecordingEntityExposure implements EntityExposure
41{
42    public private(set) RecordedExposureCollection $exposures;
43
44    /** @var array<string, RecordingExposure<covariant ExposureConfig>> */
45    private array $handlesByKey = [];
46
47    /** @var array<string, ExposedEntitySnapshot> */
48    private array $seededByKey = [];
49
50    private ?ExposureException $refusal = null;
51
52    public function __construct(
53        public private(set) AppId $appId = new AppId('test-app'),
54        private readonly ManualTimers $timers = new ManualTimers(),
55    ) {
56        $this->exposures = RecordedExposureCollection::empty();
57    }
58
59    /** @internal */
60    public function assignAppId(AppId $appId): void
61    {
62        $this->appId = $appId;
63    }
64
65    // Stands in for what Home Assistant restored or assigned, applied when the app exposes the key.
66    public function seedSnapshot(ExposedEntityKey|string $key, ExposedEntitySnapshot $snapshot): void
67    {
68        $this->seededByKey[ExposedEntityKey::fromKeyOrString($key)->value] = $snapshot;
69    }
70
71    public function cancelSubscriptions(): void
72    {
73        foreach ($this->handlesByKey as $handle) {
74            $handle->cancelCommandSubscriptions();
75        }
76    }
77
78    public function refuseExposures(ExposureException $refusal): void
79    {
80        $this->refusal = $refusal;
81    }
82
83    public function exposeSensor(ExposedEntityKey|string $key, SensorConfig $config = new SensorConfig(), ?DeviceInfo $device = null): ExposedSensor
84    {
85        return $this->trackHandle(new RecordingExposedSensor($this->claimKey($key, $config, $device), $config, $this->timers));
86    }
87
88    public function exposeBinarySensor(
89        ExposedEntityKey|string $key,
90        BinarySensorConfig $config = new BinarySensorConfig(),
91        ?DeviceInfo $device = null,
92    ): ExposedBinarySensor {
93        return $this->trackHandle(new RecordingExposedBinarySensor($this->claimKey($key, $config, $device), $config, $this->timers));
94    }
95
96    public function exposeSwitch(ExposedEntityKey|string $key, SwitchConfig $config = new SwitchConfig(), ?DeviceInfo $device = null): ExposedSwitch
97    {
98        return $this->trackHandle(new RecordingExposedSwitch($this->claimKey($key, $config, $device), $config, $this->timers));
99    }
100
101    public function exposeButton(ExposedEntityKey|string $key, ButtonConfig $config = new ButtonConfig(), ?DeviceInfo $device = null): ExposedButton
102    {
103        return $this->trackHandle(new RecordingExposedButton($this->claimKey($key, $config, $device), $config, $this->timers));
104    }
105
106    public function exposeNumber(ExposedEntityKey|string $key, NumberConfig $config, ?DeviceInfo $device = null): ExposedNumber
107    {
108        return $this->trackHandle(new RecordingExposedNumber($this->claimKey($key, $config, $device), $config, $this->timers));
109    }
110
111    public function exposeSelect(ExposedEntityKey|string $key, SelectConfig $config, ?DeviceInfo $device = null): ExposedSelect
112    {
113        return $this->trackHandle(new RecordingExposedSelect($this->claimKey($key, $config, $device), $config, $this->timers));
114    }
115
116    public function exposeText(ExposedEntityKey|string $key, TextConfig $config = new TextConfig(), ?DeviceInfo $device = null): ExposedText
117    {
118        return $this->trackHandle(new RecordingExposedText($this->claimKey($key, $config, $device), $config, $this->timers));
119    }
120
121    public function exposeTime(ExposedEntityKey|string $key, TimeConfig $config = new TimeConfig(), ?DeviceInfo $device = null): ExposedTime
122    {
123        return $this->trackHandle(new RecordingExposedTime($this->claimKey($key, $config, $device), $config, $this->timers));
124    }
125
126    public function exposeDate(ExposedEntityKey|string $key, DateConfig $config = new DateConfig(), ?DeviceInfo $device = null): ExposedDate
127    {
128        return $this->trackHandle(new RecordingExposedDate($this->claimKey($key, $config, $device), $config, $this->timers));
129    }
130
131    public function exposeDateTime(ExposedEntityKey|string $key, DateTimeConfig $config = new DateTimeConfig(), ?DeviceInfo $device = null): ExposedDateTime
132    {
133        return $this->trackHandle(new RecordingExposedDateTime($this->claimKey($key, $config, $device), $config, $this->timers));
134    }
135
136    public function exposeAction(ExposedEntityKey|string $key, ActionConfig $config = new ActionConfig()): ExposedAction
137    {
138        return $this->trackHandle(new RecordingExposedAction($this->claimKey($key, $config, null), $config, $this->timers));
139    }
140
141    public function requireSensor(ExposedEntityKey|string $key): RecordingExposedSensor
142    {
143        return $this->requireHandle($key, RecordingExposedSensor::class, 'sensor');
144    }
145
146    public function requireBinarySensor(ExposedEntityKey|string $key): RecordingExposedBinarySensor
147    {
148        return $this->requireHandle($key, RecordingExposedBinarySensor::class, 'binary sensor');
149    }
150
151    public function requireSwitch(ExposedEntityKey|string $key): RecordingExposedSwitch
152    {
153        return $this->requireHandle($key, RecordingExposedSwitch::class, 'switch');
154    }
155
156    public function requireButton(ExposedEntityKey|string $key): RecordingExposedButton
157    {
158        return $this->requireHandle($key, RecordingExposedButton::class, 'button');
159    }
160
161    public function requireNumber(ExposedEntityKey|string $key): RecordingExposedNumber
162    {
163        return $this->requireHandle($key, RecordingExposedNumber::class, 'number');
164    }
165
166    public function requireSelect(ExposedEntityKey|string $key): RecordingExposedSelect
167    {
168        return $this->requireHandle($key, RecordingExposedSelect::class, 'select');
169    }
170
171    public function requireText(ExposedEntityKey|string $key): RecordingExposedText
172    {
173        return $this->requireHandle($key, RecordingExposedText::class, 'text');
174    }
175
176    public function requireTime(ExposedEntityKey|string $key): RecordingExposedTime
177    {
178        return $this->requireHandle($key, RecordingExposedTime::class, 'time');
179    }
180
181    public function requireDate(ExposedEntityKey|string $key): RecordingExposedDate
182    {
183        return $this->requireHandle($key, RecordingExposedDate::class, 'date');
184    }
185
186    public function requireDateTime(ExposedEntityKey|string $key): RecordingExposedDateTime
187    {
188        return $this->requireHandle($key, RecordingExposedDateTime::class, 'datetime');
189    }
190
191    public function requireAction(ExposedEntityKey|string $key): RecordingExposedAction
192    {
193        return $this->requireHandle($key, RecordingExposedAction::class, 'action');
194    }
195
196    /** @throws ExposureException */
197    private function claimKey(ExposedEntityKey|string $key, ExposureConfig $config, ?DeviceInfo $device): ExposedEntityKey
198    {
199        $key = ExposedEntityKey::fromKeyOrString($key);
200
201        if ($this->refusal !== null) {
202            throw $this->refusal;
203        }
204
205        if ($this->findHandle($key)?->removed === false) {
206            throw ExposureException::keyTaken($this->appId, $key);
207        }
208
209        $this->exposures = $this->exposures->withRecordedExposure(new RecordedExposure($key, $config, $device));
210
211        return $key;
212    }
213
214    /**
215     * @template THandle of RecordingExposure<covariant ExposureConfig>
216     *
217     * @param THandle $handle
218     * @return THandle
219     */
220    private function trackHandle(RecordingExposure $handle): RecordingExposure
221    {
222        $seeded = $this->seededByKey[$handle->getKey()->value] ?? null;
223
224        if ($seeded !== null && $handle instanceof RecordingExposedEntity) {
225            $handle->applySnapshot($seeded);
226        }
227
228        return $this->handlesByKey[$handle->getKey()->value] = $handle;
229    }
230
231    /**
232     * @template THandle of RecordingExposure<covariant ExposureConfig>
233     *
234     * @param class-string<THandle> $class
235     * @return THandle
236     */
237    private function requireHandle(ExposedEntityKey|string $key, string $class, string $kind): RecordingExposure
238    {
239        $handle = $this->findHandle($key);
240
241        return $handle instanceof $class ? $handle : throw new LogicException(\sprintf('No %s was exposed as "%s".', $kind, $key));
242    }
243
244    /** @return RecordingExposure<covariant ExposureConfig>|null */
245    private function findHandle(ExposedEntityKey|string $key): ?RecordingExposure
246    {
247        return $this->handlesByKey[(string) $key] ?? null;
248    }
249}