Lines
85.71%
6 / 7
Functions and Methods
75.00%
3 / 4
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| WorkerIdConverter | 85.71% | 6 / 7 | 75.00% | 3 / 4 | 5.07 | 0.00% | 0 / 1 | |||
| handledClass | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| keySuffix | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| encodeValue | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | |||||
| decodeValue | 66.66% | 2 / 3 | 0.00% | 0 / 1 | 2.15 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Ipc\Wire; | |
| 6 | ||
| 7 | use Stewart\Runtime\Json\ValueConverter; | |
| 8 | use Stewart\Runtime\Model\WorkerId; | |
| 9 | use Stewart\Support\Exception\JsonShapeException; | |
| 10 | ||
| 11 | final readonly class WorkerIdConverter implements ValueConverter | |
| 12 | { | |
| 13 | private const string EXPECTED = 'a worker index'; | |
| 14 | ||
| 15 | public function handledClass(): string | |
| 16 | { | |
| 17 | return WorkerId::class; | |
| 18 | } | |
| 19 | ||
| 20 | public function keySuffix(): string | |
| 21 | { | |
| 22 | return ''; | |
| 23 | } | |
| 24 | ||
| 25 | public function encodeValue(object $value): int | |
| 26 | { | |
| 27 | \assert($value instanceof WorkerId); | |
| 28 | ||
| 29 | return $value->value; | |
| 30 | } | |
| 31 | ||
| 32 | public function decodeValue(mixed $value, string $path): WorkerId | |
| 33 | { | |
| 34 | if (!\is_int($value)) { | |
| 35 | throw JsonShapeException::wrongType($path, self::EXPECTED, get_debug_type($value)); | |
| 36 | } | |
| 37 | ||
| 38 | return WorkerId::fromInt($value); | |
| 39 | } | |
| 40 | } |