Lines 100.00% 3 / 3
Functions and Methods 100.00% 3 / 3
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
AdminApiCodec 100.00% 3 / 3 100.00% 3 / 3 3 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 createAdminApiWireMapper 100.00% 1 / 1 100.00% 1 / 1 1
 encodeResponse 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Http\Admin;
6
7use JsonException;
8use Stewart\Contracts\Exception\StewartException;
9use Stewart\Runtime\Control\Protocol\Frame\CommandResult;
10use Stewart\Runtime\Control\Protocol\Frame\RequestFailed;
11use Stewart\Runtime\Http\Admin\Response\AdminAppList;
12use Stewart\Runtime\Http\Admin\Response\AdminAppView;
13use Stewart\Runtime\Json\ClassShapeReader;
14use Stewart\Runtime\Json\Collection\ValueConverterCollection;
15use Stewart\Runtime\Json\IsoInstantConverter;
16use Stewart\Runtime\Json\WireMapper;
17
18final readonly class AdminApiCodec
19{
20    public function __construct(private WireMapper $adminApiWireMapper) {}
21
22    public static function createAdminApiWireMapper(): WireMapper
23    {
24        return new WireMapper(new ClassShapeReader(ValueConverterCollection::keyedByHandledClass([new IsoInstantConverter()])));
25    }
26
27    /** @throws JsonException|StewartException */
28    public function encodeResponse(AdminAppList|AdminAppView|CommandResult|RequestFailed $response): string
29    {
30        return $this->adminApiWireMapper->encodeObject($response);
31    }
32}