Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
82.35% |
28 / 34 |
|
14.29% |
1 / 7 |
CRAP | |
0.00% |
0 / 1 |
| EntityReference | |
82.35% |
28 / 34 |
|
14.29% |
1 / 7 |
16.24 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createFromContext | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
7.01 | |||
| 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\IdentifierUtils; |
| 10 | |
| 11 | class EntityReference extends SnowflakeIdentifier |
| 12 | { |
| 13 | public function __construct( |
| 14 | private BoundedContextId $boundedContextId, |
| 15 | private NonEmptyString $entityClass, |
| 16 | private NonEmptyString $id |
| 17 | ) { |
| 18 | } |
| 19 | |
| 20 | public static function createFromContext(ApieContext $context): ?self |
| 21 | { |
| 22 | if (!$context->hasContext(ContextConstants::BOUNDED_CONTEXT_ID) |
| 23 | || !$context->hasContext(ContextConstants::RESOURCE_NAME) |
| 24 | || !$context->hasContext(ContextConstants::RESOURCE_ID) |
| 25 | ) { |
| 26 | return null; |
| 27 | } |
| 28 | $boundedContextId = new BoundedContextId($context->getContext(ContextConstants::BOUNDED_CONTEXT_ID)); |
| 29 | $resourceName = $context->getContext(ContextConstants::RESOURCE_NAME); |
| 30 | |
| 31 | $hashmap = $context->getContext(BoundedContextHashmap::class); |
| 32 | $boundedContext = $hashmap[$boundedContextId->toNative()] ?? null; |
| 33 | foreach ($boundedContext->resources ?? [] as $resource) { |
| 34 | if ($resource->getShortName() === $resourceName || $resource->name === $resourceName) { |
| 35 | $id = IdentifierUtils::entityClassToIdentifier($resource) |
| 36 | ->getMethod('fromNative') |
| 37 | ->invoke(null, $context->getContext(ContextConstants::RESOURCE_ID)); |
| 38 | return new self( |
| 39 | $boundedContextId, |
| 40 | NonEmptyString::fromNative($resource->getShortName()), |
| 41 | NonEmptyString::fromNative($id->toNative()) |
| 42 | ); |
| 43 | } |
| 44 | } |
| 45 | return null; |
| 46 | } |
| 47 | |
| 48 | protected static function getSeparator(): string |
| 49 | { |
| 50 | return '/'; |
| 51 | } |
| 52 | |
| 53 | public function getBoundedContextId(): BoundedContextId |
| 54 | { |
| 55 | return $this->boundedContextId; |
| 56 | } |
| 57 | |
| 58 | public function getEntityClass(): NonEmptyString |
| 59 | { |
| 60 | return $this->entityClass; |
| 61 | } |
| 62 | |
| 63 | public function getId(): NonEmptyString |
| 64 | { |
| 65 | return $this->id; |
| 66 | } |
| 67 | |
| 68 | public function resolve(ApieContext $apieContext): ?object |
| 69 | { |
| 70 | $dataLayer = $apieContext->getContext(ApieDatalayer::class, false); |
| 71 | $hashmap = $apieContext->getContext(BoundedContextHashmap::class); |
| 72 | $boundedContext = $hashmap[$this->boundedContextId->toNative()] ?? null; |
| 73 | foreach ($boundedContext->resources as $resource) { |
| 74 | if ($resource->getShortName() === $this->entityClass->toNative()) { |
| 75 | $id = IdentifierUtils::entityClassToIdentifier($resource) |
| 76 | ->getMethod('fromNative') |
| 77 | ->invoke(null, $this->id->toNative()); |
| 78 | return $dataLayer->find($id, $this->boundedContextId); |
| 79 | } |
| 80 | } |
| 81 | return null; |
| 82 | } |
| 83 | } |