Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.00% |
34 / 40 |
|
14.29% |
1 / 7 |
CRAP | |
0.00% |
0 / 1 |
| EntityReference | |
85.00% |
34 / 40 |
|
14.29% |
1 / 7 |
17.98 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createFromContext | |
96.00% |
24 / 25 |
|
0.00% |
0 / 1 |
9 | |||
| getSeparator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getBoundedContextId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getEntityClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| resolve | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
3.01 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\ValueObjects; |
| 3 | |
| 4 | use Apie\Core\BoundedContext\BoundedContextHashmap; |
| 5 | use Apie\Core\BoundedContext\BoundedContextId; |
| 6 | use Apie\Core\Context\ApieContext; |
| 7 | use Apie\Core\ContextConstants; |
| 8 | use Apie\Core\Datalayers\ApieDatalayer; |
| 9 | use Apie\Core\Entities\EntityInterface; |
| 10 | use Apie\Core\IdentifierUtils; |
| 11 | |
| 12 | class EntityReference extends SnowflakeIdentifier |
| 13 | { |
| 14 | final public function __construct( |
| 15 | protected BoundedContextId $boundedContextId, |
| 16 | protected NonEmptyString $entityClass, |
| 17 | protected NonEmptyString $id |
| 18 | ) { |
| 19 | } |
| 20 | |
| 21 | public static function createFromContext(ApieContext $context): ?static |
| 22 | { |
| 23 | // in POST request this is the case if you make a new entity |
| 24 | $createdResource = $context->getContext(ContextConstants::RESOURCE, false); |
| 25 | if (!$context->hasContext(ContextConstants::RESOURCE_ID) && $createdResource instanceof EntityInterface) { |
| 26 | $context = $context->withContext( |
| 27 | ContextConstants::RESOURCE_ID, |
| 28 | $createdResource->getId()->toNative() |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | if (!$context->hasContext(ContextConstants::BOUNDED_CONTEXT_ID) |
| 33 | || !$context->hasContext(ContextConstants::RESOURCE_NAME) |
| 34 | || !$context->hasContext(ContextConstants::RESOURCE_ID) |
| 35 | ) { |
| 36 | return null; |
| 37 | } |
| 38 | $boundedContextId = new BoundedContextId($context->getContext(ContextConstants::BOUNDED_CONTEXT_ID)); |
| 39 | $resourceName = $context->getContext(ContextConstants::RESOURCE_NAME); |
| 40 | |
| 41 | $hashmap = $context->getContext(BoundedContextHashmap::class); |
| 42 | $boundedContext = $hashmap[$boundedContextId->toNative()] ?? null; |
| 43 | foreach ($boundedContext->resources ?? [] as $resource) { |
| 44 | if ($resource->getShortName() === $resourceName || $resource->name === $resourceName) { |
| 45 | $id = IdentifierUtils::entityClassToIdentifier($resource) |
| 46 | ->getMethod('fromNative') |
| 47 | ->invoke(null, $context->getContext(ContextConstants::RESOURCE_ID)); |
| 48 | return new static( |
| 49 | $boundedContextId, |
| 50 | NonEmptyString::fromNative($resource->getShortName()), |
| 51 | NonEmptyString::fromNative($id->toNative()) |
| 52 | ); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | protected static function getSeparator(): string |
| 60 | { |
| 61 | return '/'; |
| 62 | } |
| 63 | |
| 64 | public function getBoundedContextId(): BoundedContextId |
| 65 | { |
| 66 | return $this->boundedContextId; |
| 67 | } |
| 68 | |
| 69 | public function getEntityClass(): NonEmptyString |
| 70 | { |
| 71 | return $this->entityClass; |
| 72 | } |
| 73 | |
| 74 | public function getId(): NonEmptyString |
| 75 | { |
| 76 | return $this->id; |
| 77 | } |
| 78 | |
| 79 | public function resolve(ApieContext $apieContext): ?object |
| 80 | { |
| 81 | $dataLayer = $apieContext->getContext(ApieDatalayer::class, false); |
| 82 | $hashmap = $apieContext->getContext(BoundedContextHashmap::class); |
| 83 | $boundedContext = $hashmap[$this->boundedContextId->toNative()] ?? null; |
| 84 | foreach ($boundedContext->resources as $resource) { |
| 85 | if ($resource->getShortName() === $this->entityClass->toNative()) { |
| 86 | $id = IdentifierUtils::entityClassToIdentifier($resource) |
| 87 | ->getMethod('fromNative') |
| 88 | ->invoke(null, $this->id->toNative()); |
| 89 | return $dataLayer->find($id, $this->boundedContextId); |
| 90 | } |
| 91 | } |
| 92 | return null; |
| 93 | } |
| 94 | } |