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
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Broker;
6
7use JsonException;
8use Stewart\Contracts\App\AppId;
9use Stewart\Contracts\Exception\StewartException;
10use Stewart\Runtime\App\AppPauseOverride;
11use Stewart\Runtime\Json\ClassShapeReader;
12use Stewart\Runtime\Json\Collection\ValueConverterCollection;
13use Stewart\Runtime\Json\IsoInstantConverter;
14use Stewart\Runtime\Json\WireMapper;
15use Stewart\Support\Exception\JsonShapeException;
16use Stewart\Support\Json\JsonDecoder;
17
18final 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}