Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
91.43% |
32 / 35 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ApieInputHelper | |
91.43% |
32 / 35 |
|
60.00% |
3 / 5 |
9.05 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 | |||
| getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| interactUsingTypehint | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| interactUsingMetadata | |
84.62% |
11 / 13 |
|
0.00% |
0 / 1 |
4.06 | |||
| 1 | <?php |
| 2 | namespace Apie\Console; |
| 3 | |
| 4 | use Apie\Console\Helpers\BooleanInteractor; |
| 5 | use Apie\Console\Helpers\DefaultObjectInteractor; |
| 6 | use Apie\Console\Helpers\EnumInteractor; |
| 7 | use Apie\Console\Helpers\InputInteractorInterface; |
| 8 | use Apie\Console\Helpers\ItemHashmapInteractor; |
| 9 | use Apie\Console\Helpers\ItemListInteractor; |
| 10 | use Apie\Console\Helpers\NullInteractor; |
| 11 | use Apie\Console\Helpers\NumberInteractor; |
| 12 | use Apie\Console\Helpers\PolymorphicObjectInteractor; |
| 13 | use Apie\Console\Helpers\StringInteractor; |
| 14 | use Apie\Console\Helpers\UnionInteractor; |
| 15 | use Apie\Console\Helpers\UploadedFileInteractor; |
| 16 | use Apie\Console\Helpers\VerifyOtpInteractor; |
| 17 | use Apie\Core\Context\ApieContext; |
| 18 | use Apie\Core\Metadata\MetadataFactory; |
| 19 | use Apie\Core\Metadata\MetadataInterface; |
| 20 | use ReflectionType; |
| 21 | use Symfony\Component\Console\Helper\Helper; |
| 22 | use Symfony\Component\Console\Input\InputInterface; |
| 23 | use Symfony\Component\Console\Output\OutputInterface; |
| 24 | |
| 25 | final class ApieInputHelper extends Helper |
| 26 | { |
| 27 | /** @var array<int, InputInteractorInterface> */ |
| 28 | private array $inputInteractors; |
| 29 | |
| 30 | public function __construct(InputInteractorInterface... $inputInteractors) |
| 31 | { |
| 32 | $this->inputInteractors = $inputInteractors; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @param iterable<int, InputInteractorInterface> $additionalInteractors |
| 37 | */ |
| 38 | public static function create(iterable $additionalInteractors): self |
| 39 | { |
| 40 | return new self(...[ |
| 41 | ...$additionalInteractors, |
| 42 | new VerifyOtpInteractor(), |
| 43 | new UploadedFileInteractor(), |
| 44 | new ItemListInteractor(), |
| 45 | new ItemHashmapInteractor(), |
| 46 | new EnumInteractor(), |
| 47 | new UnionInteractor(), |
| 48 | new StringInteractor(), |
| 49 | new NullInteractor(), |
| 50 | new NumberInteractor(), |
| 51 | new BooleanInteractor(), |
| 52 | new PolymorphicObjectInteractor(), |
| 53 | new DefaultObjectInteractor() |
| 54 | ]); |
| 55 | } |
| 56 | |
| 57 | public function getName(): string |
| 58 | { |
| 59 | return 'apie'; |
| 60 | } |
| 61 | |
| 62 | public function interactUsingTypehint( |
| 63 | ReflectionType $type, |
| 64 | InputInterface $input, |
| 65 | OutputInterface $output, |
| 66 | ?ApieContext $context = null |
| 67 | ): mixed { |
| 68 | if (!$input->isInteractive()) { |
| 69 | throw new \RuntimeException('Input is not interactive!'); |
| 70 | } |
| 71 | $context??= new ApieContext(); |
| 72 | $metadata = MetadataFactory::getCreationMetadata($type, $context); |
| 73 | return $this->interactUsingMetadata($metadata, $input, $output, $context); |
| 74 | } |
| 75 | |
| 76 | public function interactUsingMetadata( |
| 77 | MetadataInterface $metadata, |
| 78 | InputInterface $input, |
| 79 | OutputInterface $output, |
| 80 | ?ApieContext $context = null |
| 81 | ): mixed { |
| 82 | if (!$input->isInteractive()) { |
| 83 | throw new \RuntimeException('Input is not interactive!'); |
| 84 | } |
| 85 | $context??= new ApieContext(); |
| 86 | foreach ($this->inputInteractors as $inputInteractor) { |
| 87 | if ($inputInteractor->supports($metadata)) { |
| 88 | return $inputInteractor->interactWith( |
| 89 | $metadata, |
| 90 | $this->getHelperSet(), |
| 91 | $input, |
| 92 | $output, |
| 93 | $context |
| 94 | ); |
| 95 | } |
| 96 | } |
| 97 | throw new \RuntimeException('I can not interact with metadata class: ' . get_debug_type($metadata)); |
| 98 | } |
| 99 | } |