Lines 100.00% 9 / 9
Methods 100.00% 1 / 1
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 readUpsertResult 100.00% 9 / 9 100.00% 1 / 1 7
14final class ExposedEntitySnapshotReader
15{
16    /**
17     * @param array<array-key, mixed> $result
18     * @throws HaClientException
19     */
20    public static function readUpsertResult(array $result): ExposedEntitySnapshot
21    {
22        $entityId = $result['entity_id'] ?? null;
23        $state = $result['state'] ?? null;
24        $attributes = $result['attributes'] ?? null;
25        $available = $result['available'] ?? null;
26
27        if (!\is_string($entityId) || !\is_array($attributes) || !\is_bool($available) || !(\is_scalar($state) || $state === null)) {
28            throw HaClientException::protocolViolation('a stewart/entity/upsert result without entity_id, state, attributes and available');
29        }
30
31        try {
32            return new ExposedEntitySnapshot(new EntityId($entityId), new ExposedState($state), JsonShape::treatKeysAsStrings($attributes), $available);
33        } catch (IdentifierException $e) {
34            throw HaClientException::protocolViolation('a stewart/entity/upsert result with an invalid entity_id', $e);
35        }
36    }
37}