Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.33% |
28 / 30 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| EntityIdentifierOptionProvider | |
93.33% |
28 / 30 |
|
60.00% |
3 / 5 |
13.05 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| supportsField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getBoundedContext | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
5.03 | |||
| createDropdownList | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
3 | |||
| determineDisplayValue | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
| 1 | <?php |
| 2 | namespace Apie\CmsApiDropdownOption\DropdownOptionProvider; |
| 3 | |
| 4 | use Apie\CmsApiDropdownOption\Dtos\DropdownOption; |
| 5 | use Apie\CmsApiDropdownOption\Lists\DropdownOptionList; |
| 6 | use Apie\Core\BoundedContext\BoundedContext; |
| 7 | use Apie\Core\BoundedContext\BoundedContextHashmap; |
| 8 | use Apie\Core\BoundedContext\BoundedContextId; |
| 9 | use Apie\Core\Context\ApieContext; |
| 10 | use Apie\Core\ContextConstants; |
| 11 | use Apie\Core\Datalayers\ApieDatalayer; |
| 12 | use Apie\Core\Datalayers\Search\QuerySearch; |
| 13 | use Apie\Core\Entities\EntityInterface; |
| 14 | use Apie\Core\Exceptions\InvalidTypeException; |
| 15 | use Apie\Core\Identifiers\IdentifierInterface; |
| 16 | use Apie\Core\Metadata\Fields\FieldInterface; |
| 17 | use Apie\Core\ValueObjects\Utils; |
| 18 | use ReflectionClass; |
| 19 | use ReflectionNamedType; |
| 20 | |
| 21 | final class EntityIdentifierOptionProvider extends BaseDropdownOptionProvider |
| 22 | { |
| 23 | public function __construct( |
| 24 | private readonly BoundedContextHashmap $boundedContextHashmap, |
| 25 | private readonly ApieDatalayer $apieDatalayer |
| 26 | ) { |
| 27 | } |
| 28 | |
| 29 | protected function supportsField(FieldInterface $fieldMetadata, ApieContext $apieContext): bool |
| 30 | { |
| 31 | return null !== $this->getBoundedContext($fieldMetadata, $apieContext); |
| 32 | } |
| 33 | |
| 34 | private function getBoundedContext(FieldInterface $fieldMetadata, ApieContext $apieContext): ?BoundedContext |
| 35 | { |
| 36 | $typehint = $fieldMetadata->getTypehint(); |
| 37 | if (!$typehint instanceof ReflectionNamedType || $typehint->isBuiltin()) { |
| 38 | return null; |
| 39 | } |
| 40 | $class = new ReflectionClass($typehint->getName()); |
| 41 | if (!in_array(IdentifierInterface::class, $class->getInterfaceNames())) { |
| 42 | return null; |
| 43 | } |
| 44 | $boundedContextId = $apieContext->hasContext(ContextConstants::BOUNDED_CONTEXT_ID) |
| 45 | ? new BoundedContextId($apieContext->getContext(ContextConstants::BOUNDED_CONTEXT_ID)) |
| 46 | : null; |
| 47 | return $this->boundedContextHashmap->getBoundedContextFromClassName($class, $boundedContextId); |
| 48 | } |
| 49 | |
| 50 | protected function createDropdownList( |
| 51 | string $property, |
| 52 | FieldInterface $fieldMetadata, |
| 53 | string $searchTerm, |
| 54 | ApieContext $apieContext |
| 55 | ): DropdownOptionList { |
| 56 | $boundedContext = $this->getBoundedContext($fieldMetadata, $apieContext); |
| 57 | assert($boundedContext instanceof BoundedContext); |
| 58 | $typehint = $fieldMetadata->getTypehint(); |
| 59 | assert($typehint instanceof ReflectionNamedType); |
| 60 | $class = new ReflectionClass($typehint->getName()); |
| 61 | if (in_array(IdentifierInterface::class, $class->getInterfaceNames())) { |
| 62 | $class = $class->getMethod('getReferenceFor')->invoke(null); |
| 63 | } |
| 64 | $result = $this->apieDatalayer->all($class, $boundedContext->getId()) |
| 65 | ->toPaginatedResult(new QuerySearch(0, textSearch: $searchTerm, apieContext: $apieContext)); |
| 66 | $list = []; |
| 67 | foreach ($result as $entity) { |
| 68 | $list[] = new DropdownOption($entity->getId(), $this->determineDisplayValue($entity)); |
| 69 | } |
| 70 | |
| 71 | return new DropdownOptionList($list); |
| 72 | } |
| 73 | |
| 74 | private function determineDisplayValue(EntityInterface $entity): string |
| 75 | { |
| 76 | $methods = ['getName', 'getDescription', '__toString', 'getId']; |
| 77 | foreach ($methods as $method) { |
| 78 | if (is_callable([$entity, $method])) { |
| 79 | return Utils::toString($entity->$method()); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | throw new InvalidTypeException($entity, 'EntityInterface'); |
| 84 | } |
| 85 | } |