Lines
100.00%
7 / 7
Functions and Methods
100.00%
2 / 2
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ControlRequestDispatcher | 100.00% | 7 / 7 | 100.00% | 2 / 2 | 8 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| answerRequest | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 7 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Control\Request; | |
| 6 | ||
| 7 | use Stewart\Contracts\App\AppId; | |
| 8 | use Stewart\Contracts\Exception\StewartException; | |
| 9 | use Stewart\Runtime\Admin\RuntimeAdmin; | |
| 10 | use Stewart\Runtime\Control\Protocol\Frame\ClientFrame; | |
| 11 | use Stewart\Runtime\Control\Protocol\Frame\PauseAppRequest; | |
| 12 | use Stewart\Runtime\Control\Protocol\Frame\ResetAppRequest; | |
| 13 | use Stewart\Runtime\Control\Protocol\Frame\RestartWorkerRequest; | |
| 14 | use Stewart\Runtime\Control\Protocol\Frame\ResumeAppRequest; | |
| 15 | use Stewart\Runtime\Control\Protocol\Frame\ServerFrame; | |
| 16 | use Stewart\Runtime\Control\Protocol\Frame\SnapshotFrame; | |
| 17 | use Stewart\Runtime\Control\Protocol\Frame\SnapshotRequest; | |
| 18 | use Stewart\Runtime\Control\SnapshotSource; | |
| 19 | use Stewart\Runtime\Exception\ControlError; | |
| 20 | use Stewart\Runtime\Exception\ControlException; | |
| 21 | use Stewart\Runtime\Lifecycle\AppPauseSource; | |
| 22 | ||
| 23 | final readonly class ControlRequestDispatcher | |
| 24 | { | |
| 25 | public function __construct( | |
| 26 | private SnapshotSource $snapshots, | |
| 27 | private RuntimeAdmin $admin, | |
| 28 | ) {} | |
| 29 | ||
| 30 | /** @throws StewartException */ | |
| 31 | public function answerRequest(ClientFrame $request): ServerFrame | |
| 32 | { | |
| 33 | return match (true) { | |
| 34 | $request instanceof SnapshotRequest => new SnapshotFrame($this->snapshots->takeSnapshot()), | |
| 35 | $request instanceof PauseAppRequest => $this->admin->pauseApp(new AppId($request->appId), AppPauseSource::Control), | |
| 36 | $request instanceof ResumeAppRequest => $this->admin->resumeApp(new AppId($request->appId), AppPauseSource::Control), | |
| 37 | $request instanceof ResetAppRequest => $this->admin->resetApp(new AppId($request->appId), AppPauseSource::Control), | |
| 38 | $request instanceof RestartWorkerRequest => $this->admin->restartWorker($request->workerId), | |
| 39 | default => throw ControlException::createForReason(ControlError::RequestUnexpected, frameClass: $request::class), | |
| 40 | }; | |
| 41 | } | |
| 42 | } |