Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
70.00% |
7 / 10 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
BooleanInteractor | |
70.00% |
7 / 10 |
|
50.00% |
1 / 2 |
5.68 | |
0.00% |
0 / 1 |
supports | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
interactWith | |
62.50% |
5 / 8 |
|
0.00% |
0 / 1 |
2.21 |
1 | <?php |
2 | namespace Apie\Console\Helpers; |
3 | |
4 | use Apie\Core\Context\ApieContext; |
5 | use Apie\Core\Enums\ScalarType; |
6 | use Apie\Core\Metadata\MetadataInterface; |
7 | use Apie\Core\Metadata\ScalarMetadata; |
8 | use Apie\Core\Metadata\ValueObjectMetadata; |
9 | use Symfony\Component\Console\Helper\HelperSet; |
10 | use Symfony\Component\Console\Helper\QuestionHelper; |
11 | use Symfony\Component\Console\Input\InputInterface; |
12 | use Symfony\Component\Console\Output\OutputInterface; |
13 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
14 | |
15 | final class BooleanInteractor implements InputInteractorInterface |
16 | { |
17 | public function supports(MetadataInterface $metadata): bool |
18 | { |
19 | return ($metadata instanceof ScalarMetadata || $metadata instanceof ValueObjectMetadata) |
20 | && $metadata->toScalarType() === ScalarType::BOOLEAN; |
21 | } |
22 | public function interactWith( |
23 | MetadataInterface $metadata, |
24 | HelperSet $helperSet, |
25 | InputInterface $input, |
26 | OutputInterface $output, |
27 | ApieContext $context |
28 | ): mixed { |
29 | $helper = $helperSet->get('question'); |
30 | assert($helper instanceof QuestionHelper); |
31 | $question = new ConfirmationQuestion('On? (yes/no): '); |
32 | if ($metadata instanceof ValueObjectMetadata) { |
33 | $question->setValidator(function ($input) use ($metadata) { |
34 | return $metadata->toClass()->getMethod('fromNative')->invoke(null, $input)->toNative(); |
35 | }); |
36 | } |
37 | return $helper->ask($input, $output, $question); |
38 | } |
39 | } |