Lines
100.00%
31 / 31
Methods
100.00%
4 / 4
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| buildSubscriptionInfos | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 6 | ||
| buildReportedInfo | 100.00% | 13 / 13 | 100.00% | 1 / 1 | 1 | ||
| buildRoutedInfo | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 1 | ||
| 15 | final readonly class SubscriptionInfoBuilder | |
| 16 | { | |
| 17 | public function __construct( | |
| 18 | private WorkerSlotRegistry $slots, | |
| 19 | private SubscriptionRegistry $registry, | |
| 20 | ) {} | |
| 21 | ||
| 22 | /** @return list<RegistrationInfo> */ | |
| 23 | public function buildSubscriptionInfos(): array | |
| 24 | { | |
| 25 | $infos = []; | |
| 26 | ||
| 27 | foreach ($this->slots->listLiveHandles() as $handle) { | |
| 28 | foreach ($handle->lastPong->apps ?? [] as $app) { | |
| 29 | foreach ($app->subscriptionReports as $report) { | |
| 30 | $infos[$report->subscriptionId->value] = $this->buildReportedInfo($handle->id, $app->scope, $report); | |
| 31 | } | |
| 32 | } | |
| 33 | } | |
| 34 | ||
| 35 | // A worker reports its subscriptions with its first probe; until then only the ones the broker routes are known. | |
| 36 | foreach ($this->registry->listSubscriptions() as $subscription) { | |
| 37 | $infos[$subscription->subscriptionId->value] ??= $this->buildRoutedInfo($subscription); | |
| 38 | } | |
| 39 | ||
| 40 | usort($infos, static fn(RegistrationInfo $a, RegistrationInfo $b): int => $a->workerId <=> $b->workerId ?: strnatcmp($a->subscriptionId, $b->subscriptionId)); | |
| 41 | ||
| 42 | return $infos; | |
| 43 | } | |
| 44 | ||
| 45 | private function buildReportedInfo(WorkerId $workerId, ResourceScope $scope, SubscriptionActivityReport $report): RegistrationInfo | |
| 46 | { | |
| 47 | return new RegistrationInfo( | |
| 48 | subscriptionId: $report->subscriptionId->value, | |
| 49 | workerId: $workerId->value, | |
| 50 | appId: $scope->wireValue(), | |
| 51 | kind: $report->kind, | |
| 52 | selector: $report->match, | |
| 53 | exact: $report->exact, | |
| 54 | received: $report->received, | |
| 55 | handled: $report->handled, | |
| 56 | dropped: $report->dropped, | |
| 57 | lastReceivedAt: $report->lastReceivedAt, | |
| 58 | lastHandledAt: $report->lastHandledAt, | |
| 59 | ); | |
| 60 | } | |
| 61 | ||
| 62 | private function buildRoutedInfo(BrokerSubscription $subscription): RegistrationInfo | |
| 63 | { | |
| 64 | return new RegistrationInfo( | |
| 65 | subscriptionId: $subscription->subscriptionId->value, | |
| 66 | workerId: $subscription->workerId->value, | |
| 67 | appId: $subscription->scope->wireValue(), | |
| 68 | kind: $subscription->kind, | |
| 69 | selector: $subscription->selector->toCanonicalKey(), | |
| 70 | exact: $subscription->selector->findExactPattern() !== null, | |
| 71 | ); | |
| 72 | } | |
| 73 | } |