Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| UnionInteractor | |
100.00% |
17 / 17 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| interactWith | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Console\Helpers; |
| 3 | |
| 4 | use Apie\Console\ApieInputHelper; |
| 5 | use Apie\Core\Context\ApieContext; |
| 6 | use Apie\Core\Metadata\MetadataInterface; |
| 7 | use Apie\Core\Metadata\UnionTypeMetadata; |
| 8 | use Symfony\Component\Console\Helper\HelperSet; |
| 9 | use Symfony\Component\Console\Helper\QuestionHelper; |
| 10 | use Symfony\Component\Console\Input\InputInterface; |
| 11 | use Symfony\Component\Console\Output\OutputInterface; |
| 12 | use Symfony\Component\Console\Question\ChoiceQuestion; |
| 13 | |
| 14 | final class UnionInteractor implements InputInteractorInterface |
| 15 | { |
| 16 | public function supports(MetadataInterface $metadata): bool |
| 17 | { |
| 18 | return $metadata instanceof UnionTypeMetadata; |
| 19 | } |
| 20 | public function interactWith( |
| 21 | MetadataInterface $metadata, |
| 22 | HelperSet $helperSet, |
| 23 | InputInterface $input, |
| 24 | OutputInterface $output, |
| 25 | ApieContext $context |
| 26 | ): mixed { |
| 27 | assert($metadata instanceof UnionTypeMetadata); |
| 28 | $choices = []; |
| 29 | /** @var array<string, MetadataInterface> */ |
| 30 | $mapping = []; |
| 31 | foreach ($metadata->getTypes() as $metadata) { |
| 32 | $name = $metadata->toClass()->name ?? $metadata->toScalarType()->value; |
| 33 | $choices[] = $name; |
| 34 | $mapping[$name] = $metadata; |
| 35 | } |
| 36 | $choice = new ChoiceQuestion('Which type? ', $choices); |
| 37 | $choice->setAutocompleterValues($choices); |
| 38 | $questionHelper = $helperSet->get('question'); |
| 39 | assert($questionHelper instanceof QuestionHelper); |
| 40 | $pickedChoice = $questionHelper->ask($input, $output, $choice); |
| 41 | $output->writeln(''); |
| 42 | $apieInputHelper = $helperSet->get('apie'); |
| 43 | assert($apieInputHelper instanceof ApieInputHelper); |
| 44 | return $apieInputHelper->interactUsingMetadata($mapping[$pickedChoice], $input, $output, $context); |
| 45 | } |
| 46 | } |