Lines 100.00% 9 / 9
Functions and Methods 100.00% 6 / 6
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
StewartCommand 100.00% 9 / 9 100.00% 6 / 6 11 100.00% 1 / 1
 configure 100.00% 1 / 1 100.00% 1 / 1 1
 findConfigOption 100.00% 1 / 1 100.00% 1 / 1 1
 nonEmptyStringOption 100.00% 2 / 2 100.00% 1 / 1 3
 writeMissingConfigFileNotice 100.00% 2 / 2 100.00% 1 / 1 2
 writeNotice 100.00% 1 / 1 100.00% 1 / 1 2
 readStringArgument 100.00% 2 / 2 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Console;
6
7use Symfony\Component\Console\Command\Command;
8use Symfony\Component\Console\Input\InputInterface;
9use Symfony\Component\Console\Input\InputOption;
10use Symfony\Component\Console\Output\ConsoleOutputInterface;
11use Symfony\Component\Console\Output\OutputInterface;
12
13abstract class StewartCommand extends Command
14{
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    }
19
20    protected function findConfigOption(InputInterface $input): ?string
21    {
22        return $this->nonEmptyStringOption($input, 'config');
23    }
24
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    }
31
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    }
38
39    protected function writeNotice(OutputInterface $output, string $notice): void
40    {
41        ($output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output)->writeln('<comment>' . $notice . '</comment>');
42    }
43
44    protected function readStringArgument(InputInterface $input, string $name): string
45    {
46        $value = $input->getArgument($name);
47
48        return \is_string($value) ? $value : '';
49    }
50}