Lines
96.55%
28 / 29
Methods
83.33%
5 / 6
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| handledClass | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| keySuffix | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| encodeValue | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 4 | ||
| decodeValue | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 5 | ||
| decodeOrigin | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 2 | ||
| 15 | final readonly class StateUpdateConverter implements ValueConverter | |
| 16 | { | |
| 17 | /** @param StringIdentifierConverter<EntityId> $entityIds */ | |
| 18 | public function __construct( | |
| 19 | private StringIdentifierConverter $entityIds, | |
| 20 | private EpochInstantConverter $instants, | |
| 21 | private EventContextConverter $contexts, | |
| 22 | private EntityStateConverter $states, | |
| 23 | ) {} | |
| 24 | ||
| 25 | public function handledClass(): string | |
| 26 | { | |
| 27 | return StateUpdate::class; | |
| 28 | } | |
| 29 | ||
| 30 | public function keySuffix(): string | |
| 31 | { | |
| 32 | return ''; | |
| 33 | } | |
| 34 | ||
| 35 | /** @return array<string, mixed> */ | |
| 36 | public function encodeValue(object $value): array | |
| 37 | { | |
| 38 | \assert($value instanceof StateUpdate); | |
| 39 | ||
| 40 | return [ | |
| 41 | 'entity_id' => $this->entityIds->encodeValue($value->entityId), | |
| 42 | 'to' => $value->to === null ? null : $this->states->encodeValue($value->to), | |
| 43 | 'fired_at_us' => $value->firedAt === null ? null : $this->instants->encodeValue($value->firedAt), | |
| 44 | 'context' => $value->context === null ? null : $this->contexts->encodeValue($value->context), | |
| 45 | 'origin' => $value->origin->value, | |
| 46 | ]; | |
| 47 | } | |
| 48 | ||
| 49 | /** @throws JsonShapeException */ | |
| 50 | public function decodeValue(mixed $value, string $path): StateUpdate | |
| 51 | { | |
| 52 | if (!\is_array($value)) { | |
| 53 | throw JsonShapeException::wrongType($path, 'an object', get_debug_type($value)); | |
| 54 | } | |
| 55 | ||
| 56 | $to = $value['to'] ?? null; | |
| 57 | $firedAt = $value['fired_at_us'] ?? null; | |
| 58 | $context = $value['context'] ?? null; | |
| 59 | ||
| 60 | return new StateUpdate( | |
| 61 | $this->entityIds->decodeValue($value['entity_id'] ?? null, $path . '.entity_id'), | |
| 62 | $to === null ? null : $this->states->decodeValue($to, $path . '.to'), | |
| 63 | $firedAt === null ? null : $this->instants->decodeValue($firedAt, $path . '.fired_at_us'), | |
| 64 | $context === null ? null : $this->contexts->decodeValue($context, $path . '.context'), | |
| 65 | $this->decodeOrigin($value['origin'] ?? null, $path), | |
| 66 | ); | |
| 67 | } | |
| 68 | ||
| 69 | /** @throws JsonShapeException */ | |
| 70 | private function decodeOrigin(mixed $origin, string $path): StateChangeOrigin | |
| 71 | { | |
| 72 | $decoded = \is_string($origin) ? StateChangeOrigin::tryFrom($origin) : null; | |
| 73 | ||
| 74 | return $decoded ?? throw JsonShapeException::unexpectedValue( | |
| 75 | $path . '.origin', | |
| 76 | 'one of ' . implode(', ', array_map(static fn(StateChangeOrigin $case): string => $case->value, StateChangeOrigin::cases())), | |
| 77 | $origin, | |
| 78 | ); | |
| 79 | } | |
| 80 | } |