Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.48% |
76 / 84 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ApieMetadataDirectedConsoleCommand | |
90.48% |
76 / 84 |
|
25.00% |
1 / 4 |
26.58 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCommandName | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getCommandHelp | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getSuccessMessage | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getMetadata | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| requiresId | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| configure | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
5.01 | |||
| addInputOption | |
95.00% |
19 / 20 |
|
0.00% |
0 / 1 |
6 | |||
| execute | |
88.24% |
45 / 51 |
|
0.00% |
0 / 1 |
14.32 | |||
| 1 | <?php |
| 2 | namespace Apie\Console\Commands; |
| 3 | |
| 4 | use Apie\Console\ApieInputHelper; |
| 5 | use Apie\Console\ConsoleCliStorage; |
| 6 | use Apie\Core\Actions\ActionInterface; |
| 7 | use Apie\Core\Actions\ActionResponse; |
| 8 | use Apie\Core\BoundedContext\BoundedContext; |
| 9 | use Apie\Core\Context\ApieContext; |
| 10 | use Apie\Core\ContextConstants; |
| 11 | use Apie\Core\Datalayers\ApieDatalayer; |
| 12 | use Apie\Core\Entities\EntityInterface; |
| 13 | use Apie\Core\IdentifierUtils; |
| 14 | use Apie\Core\Metadata\Fields\FieldInterface; |
| 15 | use Apie\Core\Metadata\Fields\FieldWithPossibleDefaultValue; |
| 16 | use Apie\Core\Metadata\MetadataInterface; |
| 17 | use BackedEnum; |
| 18 | use Exception; |
| 19 | use ReflectionClass; |
| 20 | use ReflectionMethod; |
| 21 | use ReflectionProperty; |
| 22 | use Symfony\Component\Console\Command\Command; |
| 23 | use Symfony\Component\Console\Input\InputArgument; |
| 24 | use Symfony\Component\Console\Input\InputInterface; |
| 25 | use Symfony\Component\Console\Input\InputOption; |
| 26 | use Symfony\Component\Console\Output\OutputInterface; |
| 27 | |
| 28 | abstract class ApieMetadataDirectedConsoleCommand extends Command |
| 29 | { |
| 30 | /** |
| 31 | * @param ReflectionClass<EntityInterface> $reflectionClass |
| 32 | */ |
| 33 | final public function __construct( |
| 34 | protected readonly ActionInterface $apieFacadeAction, |
| 35 | protected readonly ApieContext $apieContext, |
| 36 | protected readonly ReflectionClass $reflectionClass, |
| 37 | protected readonly ApieInputHelper $apieInputHelper, |
| 38 | protected readonly ConsoleCliStorage $consoleCliStorage, |
| 39 | protected readonly ?ReflectionMethod $reflectionMethod = null |
| 40 | ) { |
| 41 | parent::__construct(); |
| 42 | } |
| 43 | |
| 44 | abstract protected function getCommandName(): string; |
| 45 | |
| 46 | abstract protected function getCommandHelp(): string; |
| 47 | |
| 48 | abstract protected function getSuccessMessage(ActionResponse $actionResponse): string; |
| 49 | |
| 50 | abstract protected function getMetadata(): MetadataInterface; |
| 51 | |
| 52 | abstract protected function requiresId(): bool; |
| 53 | |
| 54 | final protected function configure(): void |
| 55 | { |
| 56 | $boundedContext = $this->apieContext->hasContext(BoundedContext::class) |
| 57 | ? $this->apieContext->getContext(BoundedContext::class) |
| 58 | : null; |
| 59 | $this->setName('apie:' . ($boundedContext ? $boundedContext->getId() : 'unknown') . ':'. $this->getCommandName()); |
| 60 | $this->setDescription($this->getCommandHelp()); |
| 61 | $this->setHelp($this->getCommandHelp()); |
| 62 | if ($this->requiresId()) { |
| 63 | $this->addArgument('id', InputArgument::REQUIRED, 'id of entity'); |
| 64 | } |
| 65 | $this->addOption('interactive', 'i', InputOption::VALUE_NEGATABLE, 'Fill in the fields interactively'); |
| 66 | $metadata = $this->getMetadata(); |
| 67 | foreach ($metadata->getHashmap() as $fieldName => $field) { |
| 68 | $this->addInputOption($fieldName, $field); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | private function addInputOption(string $name, FieldInterface $field): void |
| 73 | { |
| 74 | if (!$field->isField()) { |
| 75 | return; |
| 76 | } |
| 77 | $flags = $field->isRequired() ? InputOption::VALUE_REQUIRED : InputOption::VALUE_OPTIONAL; |
| 78 | //if ($parameter->isVariadic()) { |
| 79 | // $flags |= InputOption::VALUE_IS_ARRAY; |
| 80 | //} |
| 81 | |
| 82 | if ($field instanceof FieldWithPossibleDefaultValue && $field->hasDefaultValue()) { |
| 83 | $defaultValue = $field->getDefaultValue(); |
| 84 | // maybe more fine tuning |
| 85 | if ($defaultValue instanceof BackedEnum) { |
| 86 | $defaultValue = $defaultValue->value; |
| 87 | } |
| 88 | $this->addOption( |
| 89 | 'input-' . $name, |
| 90 | null, |
| 91 | $flags, |
| 92 | 'provide ' . $name . ' value', |
| 93 | $defaultValue |
| 94 | ); |
| 95 | } else { |
| 96 | $this->addOption( |
| 97 | 'input-' . $name, |
| 98 | null, |
| 99 | $flags, |
| 100 | 'provide ' . $name . ' value' |
| 101 | ); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | final protected function execute(InputInterface $input, OutputInterface $output): int |
| 106 | { |
| 107 | $rawContents = []; |
| 108 | foreach ($input->getOptions() as $optionName => $optionValue) { |
| 109 | if (str_starts_with($optionName, 'input-')) { |
| 110 | if ($optionValue === null) { |
| 111 | continue; |
| 112 | } |
| 113 | $data = json_decode($optionValue, true); |
| 114 | if (json_last_error()) { |
| 115 | $rawContents[substr($optionName, strlen('input-'))] = $optionValue; |
| 116 | } else { |
| 117 | $rawContents[substr($optionName, strlen('input-'))] = $data; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | $apieContext = $this->apieContext |
| 122 | ->withContext(ContextConstants::RESOURCE_NAME, $this->reflectionClass->name) |
| 123 | ->withContext(ContextConstants::APIE_ACTION, get_class($this->apieFacadeAction)); |
| 124 | if ($this->reflectionMethod) { |
| 125 | $apieContext = $apieContext |
| 126 | ->withContext(ContextConstants::METHOD_CLASS, $this->reflectionMethod->getDeclaringClass()->name) |
| 127 | ->withContext(ContextConstants::METHOD_NAME, $this->reflectionMethod->getName()); |
| 128 | } |
| 129 | $routeAttributes = $this->apieFacadeAction->getRouteAttributes($this->reflectionClass, $this->reflectionMethod); |
| 130 | foreach ($routeAttributes as $key => $value) { |
| 131 | $apieContext = $apieContext->withContext($key, $value); |
| 132 | } |
| 133 | if ($this->requiresId()) { |
| 134 | $id = $input->getArgument('id'); |
| 135 | $apieContext = $apieContext->withContext(ContextConstants::RESOURCE_ID, $id); |
| 136 | try { |
| 137 | $resource = $apieContext->getContext(ApieDatalayer::class)->find( |
| 138 | IdentifierUtils::idStringToIdentifier($id, $apieContext), |
| 139 | $apieContext->getContext(BoundedContext::class)->getId() |
| 140 | ); |
| 141 | } catch (Exception $exception) { |
| 142 | $output->writeln('<error>' . $exception->getMessage() . '</error>'); |
| 143 | return Command::FAILURE; |
| 144 | } |
| 145 | $apieContext = $apieContext->withContext(ContextConstants::RESOURCE, $resource); |
| 146 | } |
| 147 | if ($input->getOption('interactive')) { |
| 148 | $this->getHelperSet()->set($this->apieInputHelper); |
| 149 | $interactiveRawContents = $this->apieInputHelper->interactUsingMetadata( |
| 150 | $this->getMetadata(), |
| 151 | $input, |
| 152 | $output, |
| 153 | $apieContext |
| 154 | ); |
| 155 | if (is_array($interactiveRawContents)) { |
| 156 | $rawContents += $interactiveRawContents; |
| 157 | } |
| 158 | } |
| 159 | if ($output->isDebug()) { |
| 160 | $output->writeln("<info>Raw data entered:</info>"); |
| 161 | $output->writeln(json_encode($rawContents, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); |
| 162 | } |
| 163 | |
| 164 | $response = ($this->apieFacadeAction)($apieContext, $rawContents); |
| 165 | if ((new ReflectionProperty(ActionResponse::class, 'resource'))->isInitialized($response)) { |
| 166 | $output->writeln('<info>' . $this->getSuccessMessage($response) . '</info>'); |
| 167 | return Command::SUCCESS; |
| 168 | }; |
| 169 | $output->writeln('<error>' . $response->error->getMessage() . '</error>'); |
| 170 | if ($output->isDebug()) { |
| 171 | $output->writeln('<error>' . $response->error->getTraceAsString() . '</error>'); |
| 172 | } |
| 173 | return Command::FAILURE; |
| 174 | } |
| 175 | } |