Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
97.78% |
44 / 45 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
PolymorphicObjectInteractor | |
97.78% |
44 / 45 |
|
50.00% |
1 / 2 |
5 | |
0.00% |
0 / 1 |
supports | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 | |||
interactWith | |
100.00% |
41 / 41 |
|
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\Context\MetadataFieldHashmap; |
7 | use Apie\Core\Metadata\CompositeMetadata; |
8 | use Apie\Core\Metadata\Fields\DiscriminatorColumn; |
9 | use Apie\Core\Metadata\Fields\FieldInterface; |
10 | use Apie\Core\Metadata\MetadataFactory; |
11 | use Apie\Core\Metadata\MetadataInterface; |
12 | use Apie\Core\Utils\ConverterUtils; |
13 | use Apie\Core\Utils\EntityUtils; |
14 | use Symfony\Component\Console\Helper\HelperSet; |
15 | use Symfony\Component\Console\Helper\QuestionHelper; |
16 | use Symfony\Component\Console\Input\InputInterface; |
17 | use Symfony\Component\Console\Output\OutputInterface; |
18 | use Symfony\Component\Console\Question\ChoiceQuestion; |
19 | |
20 | class PolymorphicObjectInteractor extends DefaultObjectInteractor implements InputInteractorInterface |
21 | { |
22 | public function supports(MetadataInterface $metadata): bool |
23 | { |
24 | if ($metadata instanceof CompositeMetadata) { |
25 | $class = $metadata->toClass(); |
26 | return $class !== null && EntityUtils::isPolymorphicEntity($class); |
27 | } |
28 | |
29 | return false; |
30 | } |
31 | |
32 | public function interactWith( |
33 | MetadataInterface $metadata, |
34 | HelperSet $helperSet, |
35 | InputInterface $input, |
36 | OutputInterface $output, |
37 | ApieContext $context |
38 | ): mixed { |
39 | $apieInputHelper = $helperSet->get('apie'); |
40 | assert($apieInputHelper instanceof ApieInputHelper); |
41 | assert($metadata instanceof CompositeMetadata); |
42 | $class = $metadata->toClass(); |
43 | assert($class !== null); |
44 | $helper = $helperSet->get('question'); |
45 | assert($helper instanceof QuestionHelper); |
46 | |
47 | $list = EntityUtils::getDiscriminatorClasses($class)->toStringArray(); |
48 | $question = new ChoiceQuestion('Pick a value: ', array_combine($list, $list)); |
49 | $result = ConverterUtils::toReflectionClass($helper->ask($input, $output, $question)); |
50 | $output->writeln(''); |
51 | |
52 | $childMetadata = MetadataFactory::getCreationMetadata( |
53 | $result, |
54 | $context |
55 | ); |
56 | assert($childMetadata instanceof CompositeMetadata); |
57 | $columns = []; |
58 | $filteredMap = array_filter( |
59 | $childMetadata->getHashmap()->toArray(), |
60 | function (FieldInterface $field, string $propertyName) use (&$columns, $result) { |
61 | if ($field instanceof DiscriminatorColumn) { |
62 | $columns[$propertyName] = $field->getValueForClass($result); |
63 | return false; |
64 | } |
65 | |
66 | return true; |
67 | }, |
68 | ARRAY_FILTER_USE_BOTH |
69 | ); |
70 | $childMetadata = new CompositeMetadata( |
71 | new MetadataFieldHashmap($filteredMap), |
72 | $childMetadata->toClass() |
73 | ); |
74 | |
75 | return array_merge( |
76 | $columns, |
77 | parent::interactWith( |
78 | $childMetadata, |
79 | $helperSet, |
80 | $input, |
81 | $output, |
82 | $context |
83 | ) |
84 | ); |
85 | } |
86 | } |