Lines 87.50% 14 / 16
Functions and Methods 80.00% 4 / 5
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ReloadCheckCommand 87.50% 14 / 16 80.00% 4 / 5 10.20 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 configure 100.00% 2 / 2 100.00% 1 / 1 1
 execute 80.00% 8 / 10 0.00% 0 / 1 4.13
 findGeneratedManifest 100.00% 1 / 1 100.00% 1 / 1 2
 writeReport 100.00% 2 / 2 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Console;
6
7use Stewart\Contracts\Exception\IdentifierException;
8use Stewart\Runtime\Broker\GeneratedManifest;
9use Stewart\Runtime\Broker\Reload\ReloadCheckReport;
10use Stewart\Runtime\Broker\Reload\ReloadCheckReportCodec;
11use Stewart\Runtime\Config\ConfigLoader;
12use Stewart\Runtime\Console\Check\ProjectCheck;
13use Stewart\Runtime\Console\Check\ProjectCheckResult;
14use Symfony\Component\Console\Attribute\AsCommand;
15use Symfony\Component\Console\Command\Command;
16use Symfony\Component\Console\Input\InputInterface;
17use Symfony\Component\Console\Input\InputOption;
18use Symfony\Component\Console\Output\OutputInterface;
19
20// Run by the daemon in a child process: unlike the daemon, it loads the app classes as they are now.
21#[AsCommand(name: self::NAME, description: 'Check the project for a reload and print what it found as JSON', hidden: true)]
22final class ReloadCheckCommand extends StewartCommand
23{
24    public const string NAME = 'reload:check';
25
26    public function __construct(
27        private readonly ConfigLoader $config,
28        private readonly ProjectCheck $projects,
29        private readonly ReloadCheckReportCodec $reports,
30    ) {
31        parent::__construct();
32    }
33
34    protected function configure(): void
35    {
36        parent::configure();
37        $this->addOption('only', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Check only this automation, even if disabled; repeat for more');
38    }
39
40    protected function execute(InputInterface $input, OutputInterface $output): int
41    {
42        $configFile = $this->findConfigOption($input);
43        $fingerprint = $this->config->fingerprintConfigFile($configFile);
44
45        try {
46            $result = $this->projects->checkProject($configFile, $this->readAppSelectionOption($input));
47        } catch (IdentifierException $e) {
48            return $this->writeReport($output, ReloadCheckReport::failed([$e->getMessage()], $fingerprint));
49        }
50
51        if (!$result->hasPassed()) {
52            return $this->writeReport($output, ReloadCheckReport::failed($result->errors, $fingerprint));
53        }
54
55        // A reload keeps the running listeners, but must not accept a config the next start refuses.
56        if ($result->httpListenerError !== null) {
57            return $this->writeReport($output, ReloadCheckReport::failed([$result->httpListenerError], $fingerprint));
58        }
59
60        return $this->writeReport($output, ReloadCheckReport::fromDiscovery($result->discovered, $fingerprint, $this->findGeneratedManifest($result)));
61    }
62
63    private function findGeneratedManifest(ProjectCheckResult $result): ?GeneratedManifest
64    {
65        return $result->codegen === null ? null : GeneratedManifest::findForGeneratedCode($result->codegen);
66    }
67
68    private function writeReport(OutputInterface $output, ReloadCheckReport $report): int
69    {
70        $output->writeln($this->reports->encodeReport($report), OutputInterface::OUTPUT_RAW);
71
72        return $report->findFirstError() === null ? Command::SUCCESS : Command::FAILURE;
73    }
74}