Lines
100.00%
18 / 18
Methods
100.00%
4 / 4
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| configure | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| execute | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 7 | ||
| writeTemplateEntry | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| 20 | final class MakeAppCommand extends Command | |
| 21 | { | |
| 22 | public const string NAME = 'make:app'; | |
| 23 | ||
| 24 | public function __construct(private readonly AppScaffold $scaffold) | |
| 25 | { | |
| 26 | parent::__construct(); | |
| 27 | } | |
| 28 | ||
| 29 | protected function configure(): void | |
| 30 | { | |
| 31 | $this->addArgument('name', InputArgument::REQUIRED, 'The class name, such as PorchLight'); | |
| 32 | $this->addOption('id', null, InputOption::VALUE_REQUIRED, 'The automation ID, or with --template the first entry\'s; derived from the name when left out, such as porch-light'); | |
| 33 | $this->addOption('template', null, InputOption::VALUE_NONE, 'Create an #[AppTemplate] class, which runs once per apps.<id>.class entry in stewart.yaml'); | |
| 34 | } | |
| 35 | ||
| 36 | protected function execute(InputInterface $input, OutputInterface $output): int | |
| 37 | { | |
| 38 | $name = $input->getArgument('name'); | |
| 39 | $id = $input->getOption('id'); | |
| 40 | ||
| 41 | try { | |
| 42 | $kind = $input->getOption('template') === true ? AppScaffoldKind::Template : AppScaffoldKind::Automation; | |
| 43 | $app = $this->scaffold->scaffoldApp(\is_string($name) ? $name : '', \is_string($id) ? $id : null, $kind); | |
| 44 | } catch (StewartException $e) { | |
| 45 | ($output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output)->writeln('<error>' . $e->getMessage() . '</error>'); | |
| 46 | ||
| 47 | return Command::FAILURE; | |
| 48 | } | |
| 49 | ||
| 50 | $output->writeln(\sprintf('Created %s and %s.', $app->appFile, $app->testFile)); | |
| 51 | ||
| 52 | if ($app->kind === AppScaffoldKind::Template) { | |
| 53 | $this->writeTemplateEntry($output, $app); | |
| 54 | } else { | |
| 55 | $output->writeln(\sprintf('Its ID is %1$s; its options go under apps.%1$s in stewart.yaml.', $app->id->value)); | |
| 56 | } | |
| 57 | ||
| 58 | return Command::SUCCESS; | |
| 59 | } | |
| 60 | ||
| 61 | private function writeTemplateEntry(OutputInterface $output, ScaffoldedApp $app): void | |
| 62 | { | |
| 63 | $output->writeln('It runs once for each entry that names it. Add one to stewart.yaml, with an ID of its own:'); | |
| 64 | $output->writeln(['', 'apps:', \sprintf(' %s:', $app->id->value), \sprintf(' class: %s', $app->className), ' options:', ' entity: light.example']); | |
| 65 | } | |
| 66 | } |