Lines
87.50%
7 / 8
Functions and Methods
75.00%
3 / 4
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| AppPauseOverrideCodec | 87.50% | 7 / 8 | 75.00% | 3 / 4 | 5.05 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| createOverrideWireMapper | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| encodeOverride | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| decodeOverride | 80.00% | 4 / 5 | 0.00% | 0 / 1 | 2.03 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Broker; | |
| 6 | ||
| 7 | use JsonException; | |
| 8 | use Stewart\Contracts\App\AppId; | |
| 9 | use Stewart\Contracts\Exception\StewartException; | |
| 10 | use Stewart\Runtime\App\AppPauseOverride; | |
| 11 | use Stewart\Runtime\Json\ClassShapeReader; | |
| 12 | use Stewart\Runtime\Json\Collection\ValueConverterCollection; | |
| 13 | use Stewart\Runtime\Json\IsoInstantConverter; | |
| 14 | use Stewart\Runtime\Json\WireMapper; | |
| 15 | use Stewart\Support\Exception\JsonShapeException; | |
| 16 | use Stewart\Support\Json\JsonDecoder; | |
| 17 | ||
| 18 | final readonly class AppPauseOverrideCodec | |
| 19 | { | |
| 20 | private const int DEPTH = 8; | |
| 21 | ||
| 22 | public function __construct(private WireMapper $appPauseOverrideWireMapper) {} | |
| 23 | ||
| 24 | public static function createOverrideWireMapper(): WireMapper | |
| 25 | { | |
| 26 | return new WireMapper(new ClassShapeReader(ValueConverterCollection::keyedByHandledClass([new IsoInstantConverter()]))); | |
| 27 | } | |
| 28 | ||
| 29 | /** @throws JsonException|StewartException */ | |
| 30 | public function encodeOverride(AppPauseOverride $override): string | |
| 31 | { | |
| 32 | return $this->appPauseOverrideWireMapper->encodeObject(new StoredAppPauseOverride($override->paused, $override->since, $override->source)); | |
| 33 | } | |
| 34 | ||
| 35 | /** @throws JsonException|StewartException */ | |
| 36 | public function decodeOverride(AppId $appId, string $json): AppPauseOverride | |
| 37 | { | |
| 38 | $data = JsonDecoder::decodeJson($json, self::DEPTH); | |
| 39 | ||
| 40 | if (!\is_array($data)) { | |
| 41 | throw JsonShapeException::wrongType('override', 'an object', get_debug_type($data)); | |
| 42 | } | |
| 43 | ||
| 44 | $stored = $this->appPauseOverrideWireMapper->decodeObject(StoredAppPauseOverride::class, $data); | |
| 45 | ||
| 46 | return new AppPauseOverride($appId, $stored->paused, $stored->since, $stored->source); | |
| 47 | } | |
| 48 | } |