Lines
100.00%
14 / 14
Functions and Methods
100.00%
8 / 8
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| StewartCommand | 100.00% | 14 / 14 | 100.00% | 8 / 8 | 16 | 100.00% | 1 / 1 | |||
| configure | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| findConfigOption | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| readAppSelectionOption | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| findAppIdsOption | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 4 | |||||
| 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 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Console; | |
| 6 | ||
| 7 | use Stewart\Contracts\App\AppId; | |
| 8 | use Stewart\Contracts\App\Collection\AppIdCollection; | |
| 9 | use Stewart\Contracts\Exception\IdentifierException; | |
| 10 | use Stewart\Runtime\App\AppSelection; | |
| 11 | use Symfony\Component\Console\Command\Command; | |
| 12 | use Symfony\Component\Console\Input\InputInterface; | |
| 13 | use Symfony\Component\Console\Input\InputOption; | |
| 14 | use Symfony\Component\Console\Output\ConsoleOutputInterface; | |
| 15 | use Symfony\Component\Console\Output\OutputInterface; | |
| 16 | ||
| 17 | abstract class StewartCommand extends Command | |
| 18 | { | |
| 19 | protected function configure(): void | |
| 20 | { | |
| 21 | $this->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'Path to stewart.yaml; defaults to stewart.yaml in the project root'); | |
| 22 | } | |
| 23 | ||
| 24 | protected function findConfigOption(InputInterface $input): ?string | |
| 25 | { | |
| 26 | return $this->nonEmptyStringOption($input, 'config'); | |
| 27 | } | |
| 28 | ||
| 29 | /** @throws IdentifierException */ | |
| 30 | protected function readAppSelectionOption(InputInterface $input): AppSelection | |
| 31 | { | |
| 32 | return new AppSelection($this->findAppIdsOption($input, 'only'), $this->findAppIdsOption($input, 'trace')); | |
| 33 | } | |
| 34 | ||
| 35 | /** @throws IdentifierException */ | |
| 36 | private function findAppIdsOption(InputInterface $input, string $name): ?AppIdCollection | |
| 37 | { | |
| 38 | $ids = $input->hasOption($name) ? $input->getOption($name) : null; | |
| 39 | ||
| 40 | if (!\is_array($ids) || $ids === []) { | |
| 41 | return null; | |
| 42 | } | |
| 43 | ||
| 44 | return AppIdCollection::fromList(array_map(static fn(string $id): AppId => new AppId($id), array_values(array_filter($ids, \is_string(...))))); | |
| 45 | } | |
| 46 | ||
| 47 | protected function nonEmptyStringOption(InputInterface $input, string $name): ?string | |
| 48 | { | |
| 49 | $value = $input->getOption($name); | |
| 50 | ||
| 51 | return \is_string($value) && $value !== '' ? $value : null; | |
| 52 | } | |
| 53 | ||
| 54 | protected function writeMissingConfigFileNotice(OutputInterface $output, ?string $missingConfigFile): void | |
| 55 | { | |
| 56 | if ($missingConfigFile !== null) { | |
| 57 | $this->writeNotice($output, \sprintf('No config file at %s; using the environment only.', $missingConfigFile)); | |
| 58 | } | |
| 59 | } | |
| 60 | ||
| 61 | protected function writeNotice(OutputInterface $output, string $notice): void | |
| 62 | { | |
| 63 | ($output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output)->writeln('<comment>' . $notice . '</comment>'); | |
| 64 | } | |
| 65 | ||
| 66 | protected function readStringArgument(InputInterface $input, string $name): string | |
| 67 | { | |
| 68 | $value = $input->getArgument($name); | |
| 69 | ||
| 70 | return \is_string($value) ? $value : ''; | |
| 71 | } | |
| 72 | } |