Lines 100.00% 6 / 6
Functions and Methods 100.00% 5 / 5
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
AppStatusBuilder 100.00% 6 / 6 100.00% 5 / 5 5 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 buildAppStatuses 100.00% 1 / 1 100.00% 1 / 1 1
 buildHostedAppStatuses 100.00% 2 / 2 100.00% 1 / 1 1
 findAppStatus 100.00% 1 / 1 100.00% 1 / 1 1
 buildAppStatus 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Control\Assembler;
6
7use Stewart\Contracts\App\AppId;
8use Stewart\Runtime\Broker\AppMetrics;
9use Stewart\Runtime\Broker\AppPauseRegistry;
10use Stewart\Runtime\Broker\AppRunningTotals;
11use Stewart\Runtime\Broker\Exposure\ExposureLink;
12use Stewart\Runtime\Control\Protocol\Status\AppStatus;
13
14final readonly class AppStatusBuilder
15{
16    public function __construct(
17        private AppMetrics $metrics,
18        private AppPauseRegistry $pausedApps,
19        private ExposureLink $exposures,
20    ) {}
21
22    /** @return list<AppStatus> */
23    public function buildAppStatuses(): array
24    {
25        return array_map($this->buildAppStatus(...), $this->metrics->listRunningTotals());
26    }
27
28    /** @return list<AppStatus> */
29    public function buildHostedAppStatuses(): array
30    {
31        $appTotals = array_filter($this->metrics->listRunningTotals(), static fn(AppRunningTotals $totals): bool => !$totals->isSharedScope());
32
33        return array_values(array_map($this->buildAppStatus(...), $appTotals));
34    }
35
36    public function findAppStatus(AppId $appId): ?AppStatus
37    {
38        return $this->metrics->findAppRunningTotals($appId)?->buildAppStatus($this->pausedApps, $this->exposures);
39    }
40
41    private function buildAppStatus(AppRunningTotals $totals): AppStatus
42    {
43        return $totals->buildAppStatus($this->pausedApps, $this->exposures);
44    }
45}