Lines 85.71% 6 / 7
Methods 75.00% 3 / 4
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 decideRestart 100.00% 4 / 4 100.00% 1 / 1 2
 forgetRestarts 100.00% 1 / 1 100.00% 1 / 1 1
 countRestartsInWindow 0.00% 0 / 1 0.00% 0 / 1 2
11final readonly class WorkerRestartPolicy
12{
13    public function __construct(
14        private RestartBudget $budget,
15        private SupervisionConfig $supervision,
16        private Clock $clock,
17    ) {}
18
19    public function decideRestart(WorkerId $workerId): RestartDecision
20    {
21        $attempt = $this->budget->claimAttempt($workerId, $this->clock->getMonotonicTime());
22
23        return $attempt === null
24            ? RestartDecision::quarantine()
25            : RestartDecision::restartAfter($this->supervision->restartBackoff->delayFor($attempt));
26    }
27
28    public function forgetRestarts(WorkerId $workerId): void
29    {
30        $this->budget->forgetAttempts($workerId);
31    }
32
33    public function countRestartsInWindow(WorkerId $workerId): int
34    {
35        return $this->budget->countUsedAttempts($workerId, $this->clock->getMonotonicTime());
36    }
37}