Lines 90.47% 19 / 21
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% 8 / 8 100.00% 1 / 1 1
20final readonly class WorkerKernel
21{
22    public function __construct(private SyntheticServices $overrides = new SyntheticServices()) {}
23
24    public function run(Transport $transport): string
25    {
26        try {
27            $bootstrap = $transport->receive();
28        } catch (TransportException $e) {
29            return 'worker stopped before bootstrap: ' . $e->getMessage();
30        }
31
32        if (!$bootstrap instanceof Bootstrap) {
33            return \sprintf('worker stopped: expected Bootstrap first, got %s', get_debug_type($bootstrap));
34        }
35
36        $clock = new SystemClock(new DateTimeZone($bootstrap->timeZone));
37        $container = new ProfileCompiler()->compileProfileContainer(
38            ContainerProfile::Worker,
39            $this->buildSyntheticServicesFor($transport, $bootstrap, $clock)->withOverrides($this->overrides),
40        );
41
42        $container->resolveService(ProcessTimeZone::class)->useAsProcessDefault($clock->getTimeZone());
43
44        return $container->resolveService(WorkerSession::class)->run();
45    }
46
47    private function buildSyntheticServicesFor(Transport $transport, Bootstrap $bootstrap, SystemClock $clock): SyntheticServices
48    {
49        return new SyntheticServices()
50            ->withService(Transport::class, $transport)
51            ->withService(Bootstrap::class, $bootstrap)
52            ->withService(WorkerSettings::class, $bootstrap->settings)
53            ->withService(LogVerbosityPolicy::class, new LogVerbosityPolicy($bootstrap->settings->logLevel, $bootstrap->apps->collection->listVerbositiesByAppId()))
54            ->withService(WorkerId::class, $bootstrap->workerId)
55            ->withService(Clock::class, $clock)
56            ->withService(StewartIdentity::class, new StewartIdentity($bootstrap->haUserId));
57    }
58}