Lines 78.94% 15 / 19
Methods 60.00% 6 / 10
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 id 100.00% 1 / 1 100.00% 1 / 1 1
 scope 0.00% 0 / 1 0.00% 0 / 1 2
 isInState 0.00% 0 / 1 0.00% 0 / 1 2
 app 0.00% 0 / 1 0.00% 0 / 1 2
 moveTo 100.00% 3 / 3 100.00% 1 / 1 2
 markConstructed 100.00% 2 / 2 100.00% 1 / 1 1
 claimDisposal 100.00% 6 / 6 100.00% 1 / 1 4
 markDisposed 50.00% 1 / 2 0.00% 0 / 1 2.50
 needsDisposal 100.00% 1 / 1 100.00% 1 / 1 2
14final class AppSlot
15{
16    private ?App $app = null;
17
18    public private(set) AppState $state = AppState::Declared;
19
20    private bool $disposalClaimed = false;
21
22    public function __construct(public readonly WorkerApp $definition) {}
23
24    public function id(): AppId
25    {
26        return $this->definition->id;
27    }
28
29    public function scope(): ResourceScope
30    {
31        return ResourceScope::forApp($this->definition->id);
32    }
33
34    public function isInState(AppState $state): bool
35    {
36        return $this->state === $state;
37    }
38
39    public function app(): App
40    {
41        return $this->app ?? throw new LogicException(\sprintf('App "%s" was never constructed.', $this->id()));
42    }
43
44    public function moveTo(AppState $state): void
45    {
46        if (!$this->state->canEnter($state)) {
47            throw new LogicException(\sprintf('App "%s" cannot move from %s to %s.', $this->id(), $this->state->value, $state->value));
48        }
49
50        $this->state = $state;
51    }
52
53    public function markConstructed(App $app): void
54    {
55        $this->moveTo(AppState::Constructed);
56        $this->app = $app;
57    }
58
59    public function claimDisposal(): bool
60    {
61        if ($this->disposalClaimed || $this->app === null) {
62            return false;
63        }
64
65        $this->disposalClaimed = true;
66
67        if ($this->needsDisposal()) {
68            $this->moveTo(AppState::Disposing);
69        }
70
71        return true;
72    }
73
74    public function markDisposed(): void
75    {
76        if ($this->state === AppState::Disposing) {
77            $this->moveTo(AppState::Disposed);
78        }
79    }
80
81    public function needsDisposal(): bool
82    {
83        return $this->state === AppState::Running || $this->state === AppState::Initialized;
84    }
85}