Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
84.62% |
22 / 26 |
|
66.67% |
6 / 9 |
CRAP | |
0.00% |
0 / 1 |
| RequestAwareInMemoryDatalayer | |
84.62% |
22 / 26 |
|
66.67% |
6 / 9 |
15.82 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isSupported | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
4.13 | |||
| all | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| find | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| persistNew | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| persistExisting | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getRepository | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
4.03 | |||
| removeExisting | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| upsert | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Common\Wrappers; |
| 3 | |
| 4 | use Apie\Common\Interfaces\BoundedContextSelection; |
| 5 | use Apie\Core\BoundedContext\BoundedContextId; |
| 6 | use Apie\Core\Datalayers\ApieDatalayerWithFilters; |
| 7 | use Apie\Core\Datalayers\ApieDatalayerWithSupport; |
| 8 | use Apie\Core\Datalayers\Concerns\FiltersOnAllFields; |
| 9 | use Apie\Core\Datalayers\InMemory\InMemoryDatalayer; |
| 10 | use Apie\Core\Datalayers\Lists\EntityListInterface; |
| 11 | use Apie\Core\Datalayers\Search\LazyLoadedListFilterer; |
| 12 | use Apie\Core\Entities\EntityInterface; |
| 13 | use Apie\Core\Identifiers\IdentifierInterface; |
| 14 | use ReflectionClass; |
| 15 | |
| 16 | final class RequestAwareInMemoryDatalayer implements ApieDatalayerWithFilters, ApieDatalayerWithSupport |
| 17 | { |
| 18 | use FiltersOnAllFields; |
| 19 | |
| 20 | /** |
| 21 | * @var array<string, InMemoryDatalayer> |
| 22 | */ |
| 23 | private array $createdRepositories = []; |
| 24 | |
| 25 | public function __construct( |
| 26 | private readonly BoundedContextSelection $boundedContextSelected, |
| 27 | private readonly LazyLoadedListFilterer $filterer |
| 28 | ) { |
| 29 | } |
| 30 | |
| 31 | public function isSupported( |
| 32 | EntityInterface|ReflectionClass|IdentifierInterface $instance, |
| 33 | BoundedContextId $boundedContextId |
| 34 | ): bool { |
| 35 | $className = ($instance instanceof ReflectionClass) ? $instance->name : get_class($instance); |
| 36 | if ($instance instanceof IdentifierInterface) { |
| 37 | $className = $instance->getReferenceFor()->name; |
| 38 | } |
| 39 | $boundedContext = $this->boundedContextSelected->getBoundedContextFromClassName($className); |
| 40 | return $boundedContext ? $boundedContext->getId()->toNative() === $boundedContextId->toNative() : false; |
| 41 | } |
| 42 | |
| 43 | public function all(ReflectionClass $class, ?BoundedContextId $boundedContextId = null): EntityListInterface |
| 44 | { |
| 45 | return $this->getRepository($class, $boundedContextId) |
| 46 | ->all($class, $boundedContextId); |
| 47 | } |
| 48 | |
| 49 | public function find(IdentifierInterface $identifier, ?BoundedContextId $boundedContextId = null): EntityInterface |
| 50 | { |
| 51 | return $this->getRepository($identifier::getReferenceFor(), $boundedContextId) |
| 52 | ->find($identifier, $boundedContextId); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @template T of EntityInterface |
| 57 | * @param T $entity |
| 58 | * @return T |
| 59 | */ |
| 60 | public function persistNew(EntityInterface $entity, ?BoundedContextId $boundedContextId = null): EntityInterface |
| 61 | { |
| 62 | return $this->getRepository($entity->getId()::getReferenceFor(), $boundedContextId) |
| 63 | ->persistNew($entity, $boundedContextId); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @template T of EntityInterface |
| 68 | * @param T $entity |
| 69 | * @return T |
| 70 | */ |
| 71 | public function persistExisting(EntityInterface $entity, ?BoundedContextId $boundedContextId = null): EntityInterface |
| 72 | { |
| 73 | return $this->getRepository($entity->getId()::getReferenceFor(), $boundedContextId) |
| 74 | ->persistExisting($entity, $boundedContextId); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @param ReflectionClass<object> $class |
| 79 | */ |
| 80 | private function getRepository(ReflectionClass $class, ?BoundedContextId $boundedContextId = null): InMemoryDatalayer |
| 81 | { |
| 82 | if ($boundedContextId === null) { |
| 83 | $boundedContextId = $this->boundedContextSelected->getBoundedContextFromRequest()?->getId(); |
| 84 | if (!$boundedContextId) { |
| 85 | $boundedContextId = $this->boundedContextSelected->getBoundedContextFromClassName($class->name)?->getId(); |
| 86 | } |
| 87 | } |
| 88 | $boundedContextId ??= new BoundedContextId('unknown'); |
| 89 | if (!isset($this->createdRepositories[$boundedContextId->toNative()])) { |
| 90 | $this->createdRepositories[$boundedContextId->toNative()] = new InMemoryDatalayer($boundedContextId, $this->filterer); |
| 91 | } |
| 92 | |
| 93 | return $this->createdRepositories[$boundedContextId->toNative()]; |
| 94 | } |
| 95 | |
| 96 | public function removeExisting(EntityInterface $entity, ?BoundedContextId $boundedContextId = null): void |
| 97 | { |
| 98 | $this->getRepository($entity->getId()::getReferenceFor(), $boundedContextId) |
| 99 | ->removeExisting($entity, $boundedContextId); |
| 100 | } |
| 101 | |
| 102 | public function upsert(EntityInterface $entity, ?BoundedContextId $boundedContextId): EntityInterface |
| 103 | { |
| 104 | return $this->getRepository($entity->getId()::getReferenceFor(), $boundedContextId) |
| 105 | ->upsert($entity, $boundedContextId); |
| 106 | } |
| 107 | } |