Lines 100.00% 25 / 25
Methods 100.00% 11 / 11
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 listHostedAppStatuses 100.00% 1 / 1 100.00% 1 / 1 1
 requireAppStatus 100.00% 2 / 2 100.00% 1 / 1 1
 pauseApp 100.00% 3 / 3 100.00% 1 / 1 2
 resumeApp 100.00% 3 / 3 100.00% 1 / 1 2
 resetApp 100.00% 2 / 2 100.00% 1 / 1 1
 restartWorker 100.00% 3 / 3 100.00% 1 / 1 1
 reload 100.00% 2 / 2 100.00% 1 / 1 1
 findChangeWarning 100.00% 1 / 1 100.00% 1 / 1 1
 describeResetOutcome 100.00% 6 / 6 100.00% 1 / 1 5
 findResetWarning 100.00% 1 / 1 100.00% 1 / 1 3
27final readonly class RuntimeAdmin
28{
29    public function __construct(
30        private AppPauseService $pauses,
31        private AppStatusBuilder $appStatuses,
32        private AppTopology $topology,
33        private WorkerPool $pool,
34        private ReloadCoordinator $reloads,
35    ) {}
36
37    /** @return list<AppStatus> */
38    public function listHostedAppStatuses(): array
39    {
40        return $this->appStatuses->buildHostedAppStatuses();
41    }
42
43    /** @throws AppException */
44    public function requireAppStatus(AppId $appId): AppStatus
45    {
46        $this->pauses->assertAppLoaded($appId);
47
48        return $this->appStatuses->findAppStatus($appId) ?? throw AppException::unknown($appId);
49    }
50
51    /** @throws AppException */
52    public function pauseApp(AppId $appId, AppPauseSource $source): CommandResult
53    {
54        $outcome = $this->pauses->pauseApp($appId, $source);
55        $message = \sprintf($outcome->changed ? 'App %s paused.' : 'App %s was already paused.', $appId);
56
57        return new CommandResult($outcome->changed, $message, $this->findChangeWarning($outcome));
58    }
59
60    /** @throws AppException */
61    public function resumeApp(AppId $appId, AppPauseSource $source): CommandResult
62    {
63        $outcome = $this->pauses->resumeApp($appId, $source);
64        $message = \sprintf($outcome->changed ? 'App %s resumed.' : 'App %s was not paused.', $appId);
65
66        return new CommandResult($outcome->changed, $message, $this->findChangeWarning($outcome));
67    }
68
69    /** @throws AppException|StoreException */
70    public function resetApp(AppId $appId, AppPauseSource $source): CommandResult
71    {
72        $outcome = $this->pauses->resetApp($appId, $source);
73
74        return new CommandResult($outcome->overrideRemoved, $this->describeResetOutcome($appId, $outcome), $this->findResetWarning($outcome));
75    }
76
77    /** @throws WorkerException */
78    public function restartWorker(string $workerId): CommandResult
79    {
80        $slot = $this->topology->getWorkerSlots()->requireSlotById($workerId);
81        $outcome = $this->pool->restartWorker($slot);
82
83        return new CommandResult($outcome === WorkerRestartOutcome::Begun, $outcome->describeForWorker($slot->workerId), null);
84    }
85
86    public function reload(ReloadTrigger $trigger): CommandResult
87    {
88        $outcome = $this->reloads->requestReload($trigger);
89
90        return new CommandResult($outcome === ReloadRequestOutcome::Started, $outcome->describeOutcome(), null);
91    }
92
93    private function findChangeWarning(AppPauseOutcome $outcome): ?string
94    {
95        return $outcome->persistence->findWarning();
96    }
97
98    private function describeResetOutcome(AppId $appId, AppPauseResetOutcome $outcome): string
99    {
100        if (!$outcome->overrideRemoved) {
101            return \sprintf('App %s had no pause override.', $appId);
102        }
103
104        return \sprintf(match ($outcome->stateAfter) {
105            AppStateAfterReset::PausedByConfig => 'App %s pause override removed; config keeps it paused.',
106            AppStateAfterReset::Running => 'App %s pause override removed; it is running.',
107            AppStateAfterReset::NotLoaded => 'App %s pause override removed.',
108        }, $appId);
109    }
110
111    private function findResetWarning(AppPauseResetOutcome $outcome): ?string
112    {
113        return $outcome->overrideRemoved || $outcome->persistence !== AppPauseOverridePersistence::NotConfigured ? $outcome->persistence->findWarning() : null;
114    }
115}