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
ReloadCheckReportCodec 85.71% 6 / 7 75.00% 3 / 4 5.07 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 createReportWireMapper 100.00% 1 / 1 100.00% 1 / 1 1
 encodeReport 100.00% 1 / 1 100.00% 1 / 1 1
 decodeReport 75.00% 3 / 4 0.00% 0 / 1 2.06
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Broker\Reload;
6
7use JsonException;
8use Stewart\Contracts\Exception\StewartException;
9use Stewart\Runtime\Json\ClassShapeReader;
10use Stewart\Runtime\Json\Collection\ValueConverterCollection;
11use Stewart\Runtime\Json\WireMapper;
12use Stewart\Support\Exception\JsonShapeException;
13use Stewart\Support\Json\JsonDecoder;
14
15final readonly class ReloadCheckReportCodec
16{
17    private const int DEPTH = 8;
18
19    public function __construct(private WireMapper $reloadCheckReportWireMapper) {}
20
21    public static function createReportWireMapper(): WireMapper
22    {
23        return new WireMapper(new ClassShapeReader(ValueConverterCollection::keyedByHandledClass([])));
24    }
25
26    /** @throws JsonException|StewartException */
27    public function encodeReport(ReloadCheckReport $report): string
28    {
29        return $this->reloadCheckReportWireMapper->encodeObject($report);
30    }
31
32    /** @throws JsonException|StewartException */
33    public function decodeReport(string $json): ReloadCheckReport
34    {
35        $data = JsonDecoder::decodeJson($json, self::DEPTH);
36
37        if (!\is_array($data)) {
38            throw JsonShapeException::wrongType('report', 'an object', get_debug_type($data));
39        }
40
41        return $this->reloadCheckReportWireMapper->decodeObject(ReloadCheckReport::class, $data);
42    }
43}