Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
13.79% |
4 / 29 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| RelationNormalizer | |
13.79% |
4 / 29 |
|
0.00% |
0 / 3 |
74.07 | |
0.00% |
0 / 1 |
| findBoundedContext | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| supportsNormalization | |
36.36% |
4 / 11 |
|
0.00% |
0 / 1 |
8.12 | |||
| normalize | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | namespace Apie\Serializer\Normalizers; |
| 3 | |
| 4 | use Apie\Core\BoundedContext\BoundedContext; |
| 5 | use Apie\Core\BoundedContext\BoundedContextHashmap; |
| 6 | use Apie\Core\BoundedContext\BoundedContextId; |
| 7 | use Apie\Core\Datalayers\ApieDatalayer; |
| 8 | use Apie\Core\Exceptions\EntityNotFoundException; |
| 9 | use Apie\Core\Identifiers\IdentifierInterface; |
| 10 | use Apie\Core\Lists\ItemHashmap; |
| 11 | use Apie\Serializer\Context\ApieSerializerContext; |
| 12 | use Apie\Serializer\Interfaces\NormalizerInterface; |
| 13 | use Apie\Serializer\Relations\EmbedRelationInterface; |
| 14 | use ReflectionClass; |
| 15 | |
| 16 | class RelationNormalizer implements NormalizerInterface |
| 17 | { |
| 18 | /** |
| 19 | * @param ReflectionClass<object> $resourceClass |
| 20 | */ |
| 21 | private function findBoundedContext( |
| 22 | ApieSerializerContext $apieSerializerContext, |
| 23 | ReflectionClass $resourceClass |
| 24 | ): ?BoundedContextId { |
| 25 | $hashmap = $apieSerializerContext->getContext()->getContext(BoundedContextHashmap::class, false); |
| 26 | if ($hashmap instanceof BoundedContextHashmap) { |
| 27 | return $hashmap->getBoundedContextFromClassName( |
| 28 | $resourceClass, |
| 29 | $apieSerializerContext->getContext()->getContext(BoundedContextId::class, false) |
| 30 | )?->getId(); |
| 31 | } |
| 32 | $boundedContext = $apieSerializerContext->getContext()->getContext(BoundedContext::class, false); |
| 33 | if ($boundedContext instanceof BoundedContext && $boundedContext->contains($resourceClass)) { |
| 34 | return $boundedContext->getId(); |
| 35 | } |
| 36 | |
| 37 | return null; |
| 38 | |
| 39 | } |
| 40 | |
| 41 | public function supportsNormalization(mixed $object, ApieSerializerContext $apieSerializerContext): bool |
| 42 | { |
| 43 | if (!$apieSerializerContext->getContext()->hasContext(ApieDatalayer::class)) { |
| 44 | return false; |
| 45 | } |
| 46 | if (!$apieSerializerContext->getContext()->getContext(EmbedRelationInterface::class, false)?->hasEmbeddedRelation()) { |
| 47 | return false; |
| 48 | } |
| 49 | if ($object instanceof IdentifierInterface) { |
| 50 | $resourceClass = $object::getReferenceFor(); |
| 51 | return null !== $this->findBoundedContext( |
| 52 | $apieSerializerContext, |
| 53 | $resourceClass |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | return false; |
| 58 | } |
| 59 | public function normalize(mixed $object, ApieSerializerContext $apieSerializerContext): ?ItemHashmap |
| 60 | { |
| 61 | assert($object instanceof IdentifierInterface); |
| 62 | $contextId = $this->findBoundedContext($apieSerializerContext, $object::getReferenceFor()); |
| 63 | $dataLayer = $apieSerializerContext->getContext()->getContext(ApieDatalayer::class); |
| 64 | assert($dataLayer instanceof ApieDatalayer); |
| 65 | try { |
| 66 | $relation = $dataLayer->find($object, $contextId); |
| 67 | } catch (EntityNotFoundException) { |
| 68 | return null; |
| 69 | } |
| 70 | return $apieSerializerContext->normalizeAgain($relation, true); |
| 71 | } |
| 72 | } |