Lines 100.00% 27 / 27
Methods 100.00% 9 / 9
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 execute 100.00% 15 / 15 100.00% 1 / 1 5
 describeUnloadableFile 100.00% 2 / 2 100.00% 1 / 1 1
 [Stewart\Runtime\Console\StewartCommand] configure 100.00% 1 / 1 100.00% 1 / 1 1
 [Stewart\Runtime\Console\StewartCommand] findConfigOption 100.00% 1 / 1 100.00% 1 / 1 1
 [Stewart\Runtime\Console\StewartCommand] nonEmptyStringOption 100.00% 2 / 2 100.00% 1 / 1 3
 [Stewart\Runtime\Console\StewartCommand] writeMissingConfigFileNotice 100.00% 2 / 2 100.00% 1 / 1 2
 [Stewart\Runtime\Console\StewartCommand] writeNotice 100.00% 1 / 1 100.00% 1 / 1 2
 [Stewart\Runtime\Console\StewartCommand] readStringArgument 100.00% 2 / 2 100.00% 1 / 1 2
19final class CheckCommand extends StewartCommand
20{
21    public const string NAME = 'check';
22
23    public function __construct(
24        private readonly ConfigLoader $config,
25        private readonly AppDiscovery $discovery,
26        private readonly AppCatalogResolver $catalogs,
27    ) {
28        parent::__construct();
29    }
30
31    protected function execute(InputInterface $input, OutputInterface $output): int
32    {
33        $errors = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
34
35        try {
36            $config = $this->config->loadConfig($this->findConfigOption($input));
37            $config->requireHomeAssistant();
38            $discovered = $this->discovery->discover();
39            $apps = $this->catalogs->resolveCatalog($discovered, $config->apps, $config->workers);
40        } catch (StewartException $e) {
41            $errors->writeln('<error>' . $e->getMessage() . '</error>');
42
43            return Command::FAILURE;
44        }
45
46        if (!$discovered->unloadable->isEmpty()) {
47            foreach ($discovered->unloadable as $file) {
48                $errors->writeln('<error>' . $this->describeUnloadableFile($file) . '</error>');
49            }
50
51            return Command::FAILURE;
52        }
53
54        $this->writeMissingConfigFileNotice($output, $this->config->findMissingDefaultFile($this->findConfigOption($input)));
55        $output->writeln(\sprintf('Configuration and %d automations load; %d enabled.', $discovered->apps->count(), $apps->enabled->count()));
56
57        return Command::SUCCESS;
58    }
59
60    private function describeUnloadableFile(UnloadableAppFile $file): string
61    {
62        $cause = str_replace(["\r", "\n"], ' ', $file->cause->getMessage());
63
64        return \sprintf('%s failed to load: %s', $file->path, $cause);
65    }
66}

Inherited from Stewart\Runtime\Console\StewartCommand

15    protected function configure(): void
16    {
17        $this->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'Path to stewart.yaml; defaults to stewart.yaml in the project root');
18    }
20    protected function findConfigOption(InputInterface $input): ?string
21    {
22        return $this->nonEmptyStringOption($input, 'config');
23    }
25    protected function nonEmptyStringOption(InputInterface $input, string $name): ?string
26    {
27        $value = $input->getOption($name);
28
29        return \is_string($value) && $value !== '' ? $value : null;
30    }
32    protected function writeMissingConfigFileNotice(OutputInterface $output, ?string $missingConfigFile): void
33    {
34        if ($missingConfigFile !== null) {
35            $this->writeNotice($output, \sprintf('No config file at %s; using the environment only.', $missingConfigFile));
36        }
37    }
39    protected function writeNotice(OutputInterface $output, string $notice): void
40    {
41        ($output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output)->writeln('<comment>' . $notice . '</comment>');
42    }
44    protected function readStringArgument(InputInterface $input, string $name): string
45    {
46        $value = $input->getArgument($name);
47
48        return \is_string($value) ? $value : '';
49    }