Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.43% covered (success)
91.43%
32 / 35
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApieInputHelper
91.43% covered (success)
91.43%
32 / 35
60.00% covered (warning)
60.00%
3 / 5
9.05
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 interactUsingTypehint
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
 interactUsingMetadata
84.62% covered (warning)
84.62%
11 / 13
0.00% covered (danger)
0.00%
0 / 1
4.06
1<?php
2namespace Apie\Console;
3
4use Apie\Console\Helpers\BooleanInteractor;
5use Apie\Console\Helpers\DefaultObjectInteractor;
6use Apie\Console\Helpers\EnumInteractor;
7use Apie\Console\Helpers\InputInteractorInterface;
8use Apie\Console\Helpers\ItemHashmapInteractor;
9use Apie\Console\Helpers\ItemListInteractor;
10use Apie\Console\Helpers\NullInteractor;
11use Apie\Console\Helpers\NumberInteractor;
12use Apie\Console\Helpers\PolymorphicObjectInteractor;
13use Apie\Console\Helpers\StringInteractor;
14use Apie\Console\Helpers\UnionInteractor;
15use Apie\Console\Helpers\UploadedFileInteractor;
16use Apie\Console\Helpers\VerifyOtpInteractor;
17use Apie\Core\Context\ApieContext;
18use Apie\Core\Metadata\MetadataFactory;
19use Apie\Core\Metadata\MetadataInterface;
20use ReflectionType;
21use Symfony\Component\Console\Helper\Helper;
22use Symfony\Component\Console\Input\InputInterface;
23use Symfony\Component\Console\Output\OutputInterface;
24
25final 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}