Lines 87.50% 14 / 16
Functions and Methods 80.00% 8 / 10
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
WorkerExposedEntity 87.50% 14 / 16 80.00% 8 / 10 10.20 0.00% 0 / 1
 getEntityId 100.00% 1 / 1 100.00% 1 / 1 1
 getAttributes 100.00% 1 / 1 100.00% 1 / 1 1
 isAvailable 100.00% 1 / 1 100.00% 1 / 1 1
 setAttributes 0.00% 0 / 1 0.00% 0 / 1 2
 markAvailable 0.00% 0 / 1 0.00% 0 / 1 2
 markUnavailable 100.00% 1 / 1 100.00% 1 / 1 1
 applySnapshot 100.00% 4 / 4 100.00% 1 / 1 1
 recordCommandedState 100.00% 1 / 1 100.00% 1 / 1 1
 sendChange 100.00% 4 / 4 100.00% 1 / 1 1
 findStateValue 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Worker\Exposure;
6
7use Stewart\Contracts\Entity\EntityId;
8use Stewart\Contracts\Exception\ExposureException;
9use Stewart\Contracts\Exposure\ExposedEntity;
10use Stewart\Contracts\Exposure\ExposedEntityConfig;
11use Stewart\Contracts\Exposure\ExposedEntitySnapshot;
12use Stewart\Contracts\Exposure\ExposedState;
13use Stewart\Contracts\Exposure\ExposedStateChange;
14
15/**
16 * @phpstan-import-type ExposedAttributes from ExposedEntity
17 *
18 * @template TConfig of ExposedEntityConfig
19 * @extends WorkerExposure<TConfig>
20 */
21abstract class WorkerExposedEntity extends WorkerExposure implements ExposedEntity
22{
23    private ?EntityId $entityId = null;
24
25    private ?ExposedState $state = null;
26
27    /** @var ExposedAttributes */
28    private array $attributes = [];
29
30    private bool $available = true;
31
32    public function getEntityId(): ?EntityId
33    {
34        return $this->entityId;
35    }
36
37    public function getAttributes(): array
38    {
39        return $this->attributes;
40    }
41
42    public function isAvailable(): bool
43    {
44        return $this->available;
45    }
46
47    public function setAttributes(array $attributes): void
48    {
49        $this->sendChange(new ExposedStateChange(attributes: $attributes));
50    }
51
52    public function markAvailable(): void
53    {
54        $this->sendChange(new ExposedStateChange(available: true));
55    }
56
57    public function markUnavailable(): void
58    {
59        $this->sendChange(new ExposedStateChange(available: false));
60    }
61
62    public function applySnapshot(ExposedEntitySnapshot $snapshot): void
63    {
64        $this->entityId = $snapshot->entityId;
65        $this->state = $snapshot->state;
66        $this->attributes = $snapshot->attributes;
67        $this->available = $snapshot->available;
68    }
69
70    public function recordCommandedState(ExposedState $state): void
71    {
72        $this->state = $state;
73    }
74
75    /** @throws ExposureException */
76    protected function sendChange(ExposedStateChange $change): void
77    {
78        $this->sendUpdate($change);
79
80        $this->state = $change->state ?? $this->state;
81        $this->attributes = $change->attributes ?? $this->attributes;
82        $this->available = $change->available ?? $this->available;
83    }
84
85    protected function findStateValue(): int|float|string|bool|null
86    {
87        return $this->state?->value;
88    }
89}