Lines
90.00%
18 / 20
Functions and Methods
66.66%
2 / 3
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| WorkerKernel | 90.00% | 18 / 20 | 66.66% | 2 / 3 | 5.03 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| run | 83.33% | 10 / 12 | 0.00% | 0 / 1 | 3.04 | |||||
| buildSyntheticServicesFor | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Kernel; | |
| 6 | ||
| 7 | use DateTimeZone; | |
| 8 | use Stewart\Contracts\Time\Clock; | |
| 9 | use Stewart\Runtime\Exception\TransportException; | |
| 10 | use Stewart\Runtime\Ipc\Message\Bootstrap; | |
| 11 | use Stewart\Runtime\Ipc\Transport; | |
| 12 | use Stewart\Runtime\Ipc\WorkerSettings; | |
| 13 | use Stewart\Runtime\Model\WorkerId; | |
| 14 | use Stewart\Runtime\Time\ProcessTimeZone; | |
| 15 | use Stewart\Runtime\Time\SystemClock; | |
| 16 | use Stewart\Runtime\Worker\WorkerSession; | |
| 17 | use Stewart\Support\State\StewartIdentity; | |
| 18 | ||
| 19 | final readonly class WorkerKernel | |
| 20 | { | |
| 21 | public function __construct(private SyntheticServices $overrides = new SyntheticServices()) {} | |
| 22 | ||
| 23 | public function run(Transport $transport): string | |
| 24 | { | |
| 25 | try { | |
| 26 | $bootstrap = $transport->receive(); | |
| 27 | } catch (TransportException $e) { | |
| 28 | return 'worker stopped before bootstrap: ' . $e->getMessage(); | |
| 29 | } | |
| 30 | ||
| 31 | if (!$bootstrap instanceof Bootstrap) { | |
| 32 | return \sprintf('worker stopped: expected Bootstrap first, got %s', get_debug_type($bootstrap)); | |
| 33 | } | |
| 34 | ||
| 35 | $clock = new SystemClock(new DateTimeZone($bootstrap->timeZone)); | |
| 36 | $container = new ProfileCompiler()->compileProfileContainer( | |
| 37 | ContainerProfile::Worker, | |
| 38 | $this->buildSyntheticServicesFor($transport, $bootstrap, $clock)->withOverrides($this->overrides), | |
| 39 | ); | |
| 40 | ||
| 41 | $container->resolveService(ProcessTimeZone::class)->useAsProcessDefault($clock->getTimeZone()); | |
| 42 | ||
| 43 | return $container->resolveService(WorkerSession::class)->run(); | |
| 44 | } | |
| 45 | ||
| 46 | private function buildSyntheticServicesFor(Transport $transport, Bootstrap $bootstrap, SystemClock $clock): SyntheticServices | |
| 47 | { | |
| 48 | return new SyntheticServices() | |
| 49 | ->withService(Transport::class, $transport) | |
| 50 | ->withService(Bootstrap::class, $bootstrap) | |
| 51 | ->withService(WorkerSettings::class, $bootstrap->settings) | |
| 52 | ->withService(WorkerId::class, $bootstrap->workerId) | |
| 53 | ->withService(Clock::class, $clock) | |
| 54 | ->withService(StewartIdentity::class, new StewartIdentity($bootstrap->haUserId)); | |
| 55 | } | |
| 56 | } |