Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.00% covered (warning)
70.00%
7 / 10
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
BooleanInteractor
70.00% covered (warning)
70.00%
7 / 10
50.00% covered (danger)
50.00%
1 / 2
5.68
0.00% covered (danger)
0.00%
0 / 1
 supports
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
 interactWith
62.50% covered (warning)
62.50%
5 / 8
0.00% covered (danger)
0.00%
0 / 1
2.21
1<?php
2namespace Apie\Console\Helpers;
3
4use Apie\Core\Context\ApieContext;
5use Apie\Core\Enums\ScalarType;
6use Apie\Core\Metadata\MetadataInterface;
7use Apie\Core\Metadata\ScalarMetadata;
8use Apie\Core\Metadata\ValueObjectMetadata;
9use Symfony\Component\Console\Helper\HelperSet;
10use Symfony\Component\Console\Helper\QuestionHelper;
11use Symfony\Component\Console\Input\InputInterface;
12use Symfony\Component\Console\Output\OutputInterface;
13use Symfony\Component\Console\Question\ConfirmationQuestion;
14
15final 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}