Lines
78.94%
15 / 19
Functions and Methods
60.00%
6 / 10
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| AppSlot | 78.94% | 15 / 19 | 60.00% | 6 / 10 | 18.39 | 0.00% | 0 / 1 | |||
| __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 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Worker; | |
| 6 | ||
| 7 | use LogicException; | |
| 8 | use Stewart\Contracts\App; | |
| 9 | use Stewart\Contracts\App\AppId; | |
| 10 | use Stewart\Runtime\Ipc\WorkerApp; | |
| 11 | use Stewart\Runtime\Lifecycle\AppState; | |
| 12 | use Stewart\Runtime\Model\ResourceScope; | |
| 13 | ||
| 14 | final 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 | } |