Lines
95.23%
40 / 42
Functions and Methods
80.00%
4 / 5
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| EventDecoder | 95.23% | 40 / 42 | 80.00% | 4 / 5 | 19 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| decodeStateChange | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 3 | |||||
| decodeStateUpdate | 86.66% | 13 / 15 | 0.00% | 0 / 1 | 5.06 | |||||
| decodeEvent | 100.00% | 13 / 13 | 100.00% | 1 / 1 | 6 | |||||
| decodeTriggerEvent | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 4 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Client\Event; | |
| 6 | ||
| 7 | use Stewart\Client\State\EntityStateDecoder; | |
| 8 | use Stewart\Contracts\Event\EventOrigin; | |
| 9 | use Stewart\Contracts\Event\HaEvent; | |
| 10 | use Stewart\Contracts\State\EventContext; | |
| 11 | use Stewart\Contracts\State\StateChange; | |
| 12 | use Stewart\Contracts\State\StateChangeOrigin; | |
| 13 | use Stewart\Contracts\Trigger\TriggerEvent; | |
| 14 | use Stewart\Support\Json\JsonShape; | |
| 15 | use Stewart\Support\State\StateUpdate; | |
| 16 | ||
| 17 | final readonly class EventDecoder | |
| 18 | { | |
| 19 | public function __construct(private EntityStateDecoder $states) {} | |
| 20 | ||
| 21 | /** @param array<string, mixed> $event */ | |
| 22 | public function decodeStateChange(array $event): ?StateChange | |
| 23 | { | |
| 24 | $update = $this->decodeStateUpdate($event); | |
| 25 | $data = $event['data'] ?? null; | |
| 26 | $old = \is_array($data) ? $data['old_state'] ?? null : null; | |
| 27 | ||
| 28 | return $update?->toStateChange(\is_array($old) ? $this->states->decodeEntityStateOrSkip($old) : null); | |
| 29 | } | |
| 30 | ||
| 31 | /** @param array<string, mixed> $event */ | |
| 32 | public function decodeStateUpdate(array $event): ?StateUpdate | |
| 33 | { | |
| 34 | $data = $event['data'] ?? null; | |
| 35 | ||
| 36 | if (!\is_array($data)) { | |
| 37 | return null; | |
| 38 | } | |
| 39 | ||
| 40 | $entityId = $this->states->parseEntityIdOrWarn($data['entity_id'] ?? null); | |
| 41 | ||
| 42 | if ($entityId === null) { | |
| 43 | return null; | |
| 44 | } | |
| 45 | ||
| 46 | $new = $data['new_state'] ?? null; | |
| 47 | $context = $event['context'] ?? null; | |
| 48 | ||
| 49 | return new StateUpdate( | |
| 50 | entityId: $entityId, | |
| 51 | to: \is_array($new) ? $this->states->decodeEntityStateOrSkip($new) : null, | |
| 52 | firedAt: $this->states->parseInstantOrWarn($event['time_fired'] ?? null, $entityId->value, 'time_fired'), | |
| 53 | context: \is_array($context) ? EventContext::fromArray($context) : null, | |
| 54 | origin: StateChangeOrigin::Live, | |
| 55 | ); | |
| 56 | } | |
| 57 | ||
| 58 | /** @param array<string, mixed> $event */ | |
| 59 | public function decodeEvent(array $event): ?HaEvent | |
| 60 | { | |
| 61 | $type = $event['event_type'] ?? null; | |
| 62 | ||
| 63 | if (!\is_string($type) || $type === '') { | |
| 64 | return null; | |
| 65 | } | |
| 66 | ||
| 67 | $data = $event['data'] ?? null; | |
| 68 | $context = $event['context'] ?? null; | |
| 69 | $origin = $event['origin'] ?? null; | |
| 70 | ||
| 71 | return new HaEvent( | |
| 72 | type: $type, | |
| 73 | data: \is_array($data) ? JsonShape::treatKeysAsStrings($data) : [], | |
| 74 | origin: \is_string($origin) ? EventOrigin::tryFrom($origin) ?? EventOrigin::Local : EventOrigin::Local, | |
| 75 | firedAt: $this->states->parseInstantOrWarn($event['time_fired'] ?? null, $type, 'time_fired'), | |
| 76 | context: \is_array($context) ? EventContext::fromArray($context) : null, | |
| 77 | ); | |
| 78 | } | |
| 79 | ||
| 80 | /** @param array<string, mixed> $event */ | |
| 81 | public function decodeTriggerEvent(array $event): ?TriggerEvent | |
| 82 | { | |
| 83 | $variables = $event['variables'] ?? null; | |
| 84 | $trigger = \is_array($variables) ? $variables['trigger'] ?? null : null; | |
| 85 | ||
| 86 | if (!\is_array($trigger)) { | |
| 87 | return null; | |
| 88 | } | |
| 89 | ||
| 90 | $context = $event['context'] ?? null; | |
| 91 | ||
| 92 | return new TriggerEvent( | |
| 93 | trigger: JsonShape::treatKeysAsStrings($trigger), | |
| 94 | context: \is_array($context) ? EventContext::fromArray($context) : null, | |
| 95 | ); | |
| 96 | } | |
| 97 | } |