Lines 75.00% 3 / 4
Functions and Methods 66.66% 2 / 3
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
WorkerSlotCollection 75.00% 3 / 4 66.66% 2 / 3 3.14 0.00% 0 / 1
 requireSlotById 100.00% 2 / 2 100.00% 1 / 1 1
 findSlot 0.00% 0 / 1 0.00% 0 / 1 2
 findWorkerOfApp 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Broker\Collection;
6
7use Stewart\Contracts\App\AppId;
8use Stewart\Contracts\Collection\ListCollection;
9use Stewart\Runtime\Broker\WorkerSlot;
10use Stewart\Runtime\Exception\WorkerError;
11use Stewart\Runtime\Exception\WorkerException;
12use Stewart\Runtime\Model\WorkerId;
13
14/** @extends ListCollection<WorkerSlot> */
15final readonly class WorkerSlotCollection extends ListCollection
16{
17    /** @throws WorkerException */
18    public function requireSlotById(string $workerId): WorkerSlot
19    {
20        return $this->findFirstWhere(static fn(WorkerSlot $slot): bool => (string) $slot->workerId === $workerId)
21            ?? throw WorkerException::createForReason(WorkerError::Unknown, workerId: $workerId);
22    }
23
24    public function findSlot(WorkerId $workerId): ?WorkerSlot
25    {
26        return $this->findFirstWhere(static fn(WorkerSlot $slot): bool => $slot->workerId->equals($workerId));
27    }
28
29    public function findWorkerOfApp(AppId $appId): ?WorkerId
30    {
31        return $this->findFirstWhere(static fn(WorkerSlot $slot): bool => $slot->apps->find($appId) !== null)?->workerId;
32    }
33}