Lines 92.50% 37 / 40
Methods 81.81% 9 / 11
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 2 / 2 100.00% 1 / 1 2
 replaceConfigPauses 100.00% 10 / 10 100.00% 1 / 1 4
 forgetOverridesExcept 100.00% 1 / 1 100.00% 1 / 1 1
 restoreOverrides 0.00% 0 / 2 0.00% 0 / 1 6
 recordOverride 100.00% 5 / 5 100.00% 1 / 1 2
 forgetOverride 100.00% 3 / 3 100.00% 1 / 1 1
 findOverride 100.00% 1 / 1 100.00% 1 / 1 1
 isPaused 100.00% 1 / 1 100.00% 1 / 1 1
 findPause 83.33% 5 / 6 0.00% 0 / 1 4.07
 findConfigPauseOverride 100.00% 2 / 2 100.00% 1 / 1 4
 listPausedAppIds 100.00% 7 / 7 100.00% 1 / 1 1
17final class AppPauseRegistry
18{
19    /** @var array<string, AppId> */
20    private array $configPausedAppIds = [];
21
22    /** @var array<string, Instant> */
23    private array $configPausedSince = [];
24
25    /** @var array<string, AppPauseOverride> */
26    private array $overrides = [];
27
28    public function __construct(
29        AppDefinitionCollection $enabledApps,
30        private readonly DaemonStartTime $startTime,
31    ) {
32        foreach ($enabledApps->filter(static fn(AppDefinition $app): bool => $app->startsPaused) as $app) {
33            $this->configPausedAppIds[$app->id->value] = $app->id;
34        }
35    }
36
37    // A config pause a reload adds holds from then; one held since the start keeps its start.
38    public function replaceConfigPauses(AppDefinitionCollection $enabledApps, Instant $since): void
39    {
40        $pausedAppIds = [];
41        $pausedSince = [];
42
43        foreach ($enabledApps->filter(static fn(AppDefinition $app): bool => $app->startsPaused) as $app) {
44            $id = $app->id->value;
45            $pausedAppIds[$id] = $app->id;
46            $kept = isset($this->configPausedAppIds[$id]) ? $this->configPausedSince[$id] ?? null : $since;
47
48            if ($kept !== null) {
49                $pausedSince[$id] = $kept;
50            }
51        }
52
53        $this->configPausedAppIds = $pausedAppIds;
54        $this->configPausedSince = $pausedSince;
55    }
56
57    public function forgetOverridesExcept(AppIdCollection $appIds): void
58    {
59        $this->overrides = array_filter($this->overrides, static fn(AppPauseOverride $override): bool => $appIds->containsId($override->appId));
60    }
61
62    public function restoreOverrides(AppPauseOverrideCollection $overrides): void
63    {
64        foreach ($overrides as $override) {
65            $this->overrides[$override->appId->value] = $override;
66        }
67    }
68
69    public function recordOverride(AppPauseOverride $override): bool
70    {
71        $changed = $this->isPaused($override->appId) !== $override->paused;
72
73        if ($changed) {
74            unset($this->overrides[$override->appId->value]);
75        }
76
77        $this->overrides[$override->appId->value] = $override;
78
79        return $changed;
80    }
81
82    public function forgetOverride(AppId $appId): bool
83    {
84        $existed = isset($this->overrides[$appId->value]);
85        unset($this->overrides[$appId->value]);
86
87        return $existed;
88    }
89
90    public function findOverride(AppId $appId): ?AppPauseOverride
91    {
92        return $this->overrides[$appId->value] ?? null;
93    }
94
95    public function isPaused(AppId $appId): bool
96    {
97        return $this->findOverride($appId)->paused ?? isset($this->configPausedAppIds[$appId->value]);
98    }
99
100    public function findPause(AppId $appId): ?AppPause
101    {
102        $override = $this->findOverride($appId);
103
104        if ($override !== null) {
105            return $override->paused ? new AppPause($appId, $override->since, $override->source) : null;
106        }
107
108        if (isset($this->configPausedAppIds[$appId->value])) {
109            // A config pause holds from daemon start, which is recorded after this registry is built.
110            return new AppPause($appId, $this->configPausedSince[$appId->value] ?? $this->startTime->getStartedAt(), AppPauseSource::Config);
111        }
112
113        return null;
114    }
115
116    public function findConfigPauseOverride(AppId $appId): ?AppPauseOverride
117    {
118        $override = $this->findOverride($appId);
119
120        return $override !== null && !$override->paused && isset($this->configPausedAppIds[$appId->value]) ? $override : null;
121    }
122
123    public function listPausedAppIds(): AppIdCollection
124    {
125        return AppIdCollection::fromList([
126            ...array_values(array_diff_key($this->configPausedAppIds, $this->overrides)),
127            ...array_map(
128                static fn(AppPauseOverride $override): AppId => $override->appId,
129                array_values(array_filter($this->overrides, static fn(AppPauseOverride $override): bool => $override->paused)),
130            ),
131        ]);
132    }
133}