Lines
96.77%
30 / 31
Functions and Methods
83.33%
5 / 6
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| EntityStateConverter | 96.77% | 30 / 31 | 83.33% | 5 / 6 | 15 | 0.00% | 0 / 1 | |||
| __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% | 9 / 9 | 100.00% | 1 / 1 | 5 | |||||
| decodeValue | 100.00% | 17 / 17 | 100.00% | 1 / 1 | 5 | |||||
| findInstant | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Ipc\Wire; | |
| 6 | ||
| 7 | use Stewart\Contracts\Entity\EntityId; | |
| 8 | use Stewart\Contracts\State\EntityState; | |
| 9 | use Stewart\Contracts\Time\Instant; | |
| 10 | use Stewart\Runtime\Json\EpochInstantConverter; | |
| 11 | use Stewart\Runtime\Json\StringIdentifierConverter; | |
| 12 | use Stewart\Runtime\Json\ValueConverter; | |
| 13 | use Stewart\Support\Exception\JsonShapeException; | |
| 14 | use Stewart\Support\Json\JsonShape; | |
| 15 | ||
| 16 | final readonly class EntityStateConverter implements ValueConverter | |
| 17 | { | |
| 18 | /** @param StringIdentifierConverter<EntityId> $entityIds */ | |
| 19 | public function __construct( | |
| 20 | private StringIdentifierConverter $entityIds, | |
| 21 | private EpochInstantConverter $instants, | |
| 22 | private EventContextConverter $contexts, | |
| 23 | ) {} | |
| 24 | ||
| 25 | public function handledClass(): string | |
| 26 | { | |
| 27 | return EntityState::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 EntityState); | |
| 39 | ||
| 40 | return [ | |
| 41 | 'entity_id' => $this->entityIds->encodeValue($value->entityId), | |
| 42 | 'state' => $value->state, | |
| 43 | // Cast so list-shaped attributes encode as a JSON object, not an array. | |
| 44 | 'attributes' => array_is_list($value->attributes) ? (object) $value->attributes : $value->attributes, | |
| 45 | 'last_changed_at_us' => $value->lastChangedAt === null ? null : $this->instants->encodeValue($value->lastChangedAt), | |
| 46 | 'last_updated_at_us' => $value->lastUpdatedAt === null ? null : $this->instants->encodeValue($value->lastUpdatedAt), | |
| 47 | 'context' => $value->context === null ? null : $this->contexts->encodeValue($value->context), | |
| 48 | ]; | |
| 49 | } | |
| 50 | ||
| 51 | /** @throws JsonShapeException */ | |
| 52 | public function decodeValue(mixed $value, string $path): EntityState | |
| 53 | { | |
| 54 | if (!\is_array($value)) { | |
| 55 | throw JsonShapeException::wrongType($path, 'an object', get_debug_type($value)); | |
| 56 | } | |
| 57 | ||
| 58 | $state = $value['state'] ?? null; | |
| 59 | $attributes = $value['attributes'] ?? null; | |
| 60 | $context = $value['context'] ?? null; | |
| 61 | ||
| 62 | if (!\is_string($state)) { | |
| 63 | throw JsonShapeException::wrongType($path . '.state', 'a string', get_debug_type($state)); | |
| 64 | } | |
| 65 | ||
| 66 | if (!\is_array($attributes)) { | |
| 67 | throw JsonShapeException::wrongType($path . '.attributes', 'an object', get_debug_type($attributes)); | |
| 68 | } | |
| 69 | ||
| 70 | return new EntityState( | |
| 71 | $this->entityIds->decodeValue($value['entity_id'] ?? null, $path . '.entity_id'), | |
| 72 | $state, | |
| 73 | JsonShape::treatKeysAsStrings($attributes), | |
| 74 | $this->findInstant($value, 'last_changed_at_us', $path), | |
| 75 | $this->findInstant($value, 'last_updated_at_us', $path), | |
| 76 | $context === null ? null : $this->contexts->decodeValue($context, $path . '.context'), | |
| 77 | ); | |
| 78 | } | |
| 79 | ||
| 80 | /** | |
| 81 | * @param array<array-key, mixed> $state | |
| 82 | * @throws JsonShapeException | |
| 83 | */ | |
| 84 | private function findInstant(array $state, string $key, string $path): ?Instant | |
| 85 | { | |
| 86 | $value = $state[$key] ?? null; | |
| 87 | ||
| 88 | return $value === null ? null : $this->instants->decodeValue($value, $path . '.' . $key); | |
| 89 | } | |
| 90 | } |