Lines 90.00% 18 / 20
Methods 66.66% 2 / 3
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __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
19final 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}