Lines 100.00% 11 / 11
Functions and Methods 100.00% 1 / 1
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ReconcileResultReader 100.00% 11 / 11 100.00% 1 / 1 6 100.00% 1 / 1
 readRemovedEntityIds 100.00% 11 / 11 100.00% 1 / 1 6
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Client\Component;
6
7use Stewart\Client\Exception\HaClientException;
8use Stewart\Contracts\Entity\Collection\EntityIdCollection;
9use Stewart\Contracts\Entity\EntityId;
10use Stewart\Contracts\Exception\IdentifierException;
11
12final class ReconcileResultReader
13{
14    /**
15     * @param array<array-key, mixed> $result
16     * @throws HaClientException
17     */
18    public static function readRemovedEntityIds(array $result): EntityIdCollection
19    {
20        $removed = $result['removed'] ?? null;
21
22        if (!\is_array($removed) || !array_is_list($removed)) {
23            throw HaClientException::protocolViolation('a stewart/entity/reconcile result without removed');
24        }
25
26        $entityIds = [];
27
28        foreach ($removed as $entityId) {
29            if (!\is_string($entityId)) {
30                throw HaClientException::protocolViolation('a stewart/entity/reconcile result with a non-string entity id');
31            }
32
33            try {
34                $entityIds[] = new EntityId($entityId);
35            } catch (IdentifierException $e) {
36                throw HaClientException::protocolViolation('a stewart/entity/reconcile result with an invalid entity id', $e);
37            }
38        }
39
40        return EntityIdCollection::fromList($entityIds);
41    }
42}