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