Lines 79.16% 19 / 24
Methods 90.00% 9 / 10
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 forEntities 100.00% 1 / 1 100.00% 1 / 1 1
 forDevices 100.00% 1 / 1 100.00% 1 / 1 1
 forAreas 100.00% 1 / 1 100.00% 1 / 1 1
 forFloors 100.00% 1 / 1 100.00% 1 / 1 1
 forLabels 100.00% 1 / 1 100.00% 1 / 1 1
 toServiceTarget 100.00% 1 / 1 100.00% 1 / 1 1
 isEmpty 0.00% 0 / 5 0.00% 0 / 1 30
 toArray 100.00% 11 / 11 100.00% 1 / 1 3
 listIdValues 100.00% 1 / 1 100.00% 1 / 1 1
16final readonly class ServiceTarget implements ServiceTargetSource
17{
18    /**
19     * @param list<EntityId> $entityIds
20     * @param list<string> $deviceIds
21     * @param list<string> $areaIds
22     * @param list<string> $floorIds
23     * @param list<string> $labelIds
24     */
25    public function __construct(
26        #[ListOf(EntityId::class)]
27        public array $entityIds = [],
28        #[ListOf('string')]
29        public array $deviceIds = [],
30        #[ListOf('string')]
31        public array $areaIds = [],
32        #[ListOf('string')]
33        public array $floorIds = [],
34        #[ListOf('string')]
35        public array $labelIds = [],
36    ) {}
37
38    /** @throws IdentifierException */
39    public static function forEntities(EntityId|string ...$entityIds): self
40    {
41        return new self(entityIds: array_values(array_map(EntityId::fromStringOrId(...), $entityIds)));
42    }
43
44    public static function forDevices(DeviceId|string ...$deviceIds): self
45    {
46        return new self(deviceIds: self::listIdValues($deviceIds));
47    }
48
49    public static function forAreas(AreaId|string ...$areaIds): self
50    {
51        return new self(areaIds: self::listIdValues($areaIds));
52    }
53
54    public static function forFloors(FloorId|string ...$floorIds): self
55    {
56        return new self(floorIds: self::listIdValues($floorIds));
57    }
58
59    public static function forLabels(LabelId|string ...$labelIds): self
60    {
61        return new self(labelIds: self::listIdValues($labelIds));
62    }
63
64    public function toServiceTarget(): self
65    {
66        return $this;
67    }
68
69    public function isEmpty(): bool
70    {
71        return $this->entityIds === []
72            && $this->deviceIds === []
73            && $this->areaIds === []
74            && $this->floorIds === []
75            && $this->labelIds === [];
76    }
77
78    /** @return array<string, list<string>> */
79    public function toArray(): array
80    {
81        $out = [];
82
83        foreach ([
84            'entity_id' => array_map(static fn(EntityId $entityId): string => $entityId->value, $this->entityIds),
85            'device_id' => $this->deviceIds,
86            'area_id' => $this->areaIds,
87            'floor_id' => $this->floorIds,
88            'label_id' => $this->labelIds,
89        ] as $key => $values) {
90            if ($values !== []) {
91                $out[$key] = $values;
92            }
93        }
94
95        return $out;
96    }
97
98    /**
99     * @param array<RegistryId|string> $ids
100     * @return list<string>
101     */
102    private static function listIdValues(array $ids): array
103    {
104        return array_values(array_map(static fn(RegistryId|string $id): string => (string) $id, $ids));
105    }
106}