Lines
82.60%
19 / 23
Methods
73.33%
11 / 15
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| getDomain | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| getObjectId | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| isOn | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| isOff | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| isUnavailable | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | ||
| wasLastChangedBy | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | ||
| getAttribute | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| getFloatAttribute | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | ||
| getIntAttribute | 75.00% | 3 / 4 | 0.00% | 0 / 1 | 4.25 | ||
| getStringAttribute | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | ||
| getBoolAttribute | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | ||
| getArrayAttribute | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | ||
| getStateAsFloat | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | ||
| getFriendlyName | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | ||
| 10 | final readonly class EntityState | |
| 11 | { | |
| 12 | public const string UNAVAILABLE = 'unavailable'; | |
| 13 | ||
| 14 | public const string UNKNOWN = 'unknown'; | |
| 15 | ||
| 16 | /** @param array<string, mixed> $attributes */ | |
| 17 | public function __construct( | |
| 18 | public EntityId $entityId, | |
| 19 | public string $state, | |
| 20 | public array $attributes = [], | |
| 21 | public ?Instant $lastChangedAt = null, | |
| 22 | public ?Instant $lastUpdatedAt = null, | |
| 23 | public ?EventContext $context = null, | |
| 24 | ) {} | |
| 25 | ||
| 26 | public function getDomain(): string | |
| 27 | { | |
| 28 | return $this->entityId->domain; | |
| 29 | } | |
| 30 | ||
| 31 | public function getObjectId(): string | |
| 32 | { | |
| 33 | return $this->entityId->objectId; | |
| 34 | } | |
| 35 | ||
| 36 | public function isOn(): bool | |
| 37 | { | |
| 38 | return $this->state === 'on'; | |
| 39 | } | |
| 40 | ||
| 41 | public function isOff(): bool | |
| 42 | { | |
| 43 | return $this->state === 'off'; | |
| 44 | } | |
| 45 | ||
| 46 | public function isUnavailable(): bool | |
| 47 | { | |
| 48 | return $this->state === self::UNAVAILABLE || $this->state === self::UNKNOWN; | |
| 49 | } | |
| 50 | ||
| 51 | public function wasLastChangedBy(EventContext $context): bool | |
| 52 | { | |
| 53 | return $this->context !== null && $context->isSameOrParentOf($this->context); | |
| 54 | } | |
| 55 | ||
| 56 | public function getAttribute(string $name): mixed | |
| 57 | { | |
| 58 | return $this->attributes[$name] ?? null; | |
| 59 | } | |
| 60 | ||
| 61 | public function getFloatAttribute(string $name): ?float | |
| 62 | { | |
| 63 | $value = $this->attributes[$name] ?? null; | |
| 64 | ||
| 65 | return is_numeric($value) ? (float) $value : null; | |
| 66 | } | |
| 67 | ||
| 68 | public function getIntAttribute(string $name): ?int | |
| 69 | { | |
| 70 | $value = $this->attributes[$name] ?? null; | |
| 71 | ||
| 72 | if (!\is_int($value) && !\is_float($value) && !\is_string($value)) { | |
| 73 | return null; | |
| 74 | } | |
| 75 | ||
| 76 | return filter_var($value, \FILTER_VALIDATE_INT, \FILTER_NULL_ON_FAILURE); | |
| 77 | } | |
| 78 | ||
| 79 | public function getStringAttribute(string $name): ?string | |
| 80 | { | |
| 81 | $value = $this->attributes[$name] ?? null; | |
| 82 | ||
| 83 | return \is_string($value) ? $value : null; | |
| 84 | } | |
| 85 | ||
| 86 | public function getBoolAttribute(string $name): ?bool | |
| 87 | { | |
| 88 | $value = $this->attributes[$name] ?? null; | |
| 89 | ||
| 90 | return \is_bool($value) ? $value : null; | |
| 91 | } | |
| 92 | ||
| 93 | /** @return array<array-key, mixed>|null */ | |
| 94 | public function getArrayAttribute(string $name): ?array | |
| 95 | { | |
| 96 | $value = $this->attributes[$name] ?? null; | |
| 97 | ||
| 98 | return \is_array($value) ? $value : null; | |
| 99 | } | |
| 100 | ||
| 101 | public function getStateAsFloat(): ?float | |
| 102 | { | |
| 103 | return is_numeric($this->state) ? (float) $this->state : null; | |
| 104 | } | |
| 105 | ||
| 106 | public function getFriendlyName(): string | |
| 107 | { | |
| 108 | $name = $this->attributes['friendly_name'] ?? null; | |
| 109 | ||
| 110 | return \is_string($name) ? $name : $this->entityId->value; | |
| 111 | } | |
| 112 | } |