Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
91.49% |
43 / 47 |
|
71.43% |
5 / 7 |
CRAP | |
0.00% |
0 / 1 |
| InMemoryDatalayer | |
91.49% |
43 / 47 |
|
71.43% |
5 / 7 |
18.20 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| all | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| persistNew | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
| persistExisting | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| find | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| removeExisting | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
3.02 | |||
| upsert | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Datalayers\InMemory; |
| 3 | |
| 4 | use Apie\Core\BoundedContext\BoundedContextId; |
| 5 | use Apie\Core\Datalayers\ApieDatalayer; |
| 6 | use Apie\Core\Datalayers\Lists\EntityListInterface; |
| 7 | use Apie\Core\Datalayers\Lists\InMemoryEntityList; |
| 8 | use Apie\Core\Datalayers\Search\LazyLoadedListFilterer; |
| 9 | use Apie\Core\Entities\EntityInterface; |
| 10 | use Apie\Core\Exceptions\EntityAlreadyPersisted; |
| 11 | use Apie\Core\Exceptions\EntityNotFoundException; |
| 12 | use Apie\Core\Exceptions\UnknownExistingEntityError; |
| 13 | use Apie\Core\Identifiers\AutoIncrementInteger; |
| 14 | use Apie\Core\Identifiers\IdentifierInterface; |
| 15 | use Faker\Factory; |
| 16 | use Faker\Generator; |
| 17 | use ReflectionClass; |
| 18 | use ReflectionProperty; |
| 19 | |
| 20 | class InMemoryDatalayer implements ApieDatalayer |
| 21 | { |
| 22 | /** |
| 23 | * @var array<string, array<int, EntityInterface>> |
| 24 | */ |
| 25 | private array $stored = []; |
| 26 | |
| 27 | /** |
| 28 | * @var array<class-string<EntityInterface>, EntityListInterface<EntityInterface>> |
| 29 | */ |
| 30 | private array $alreadyLoadedLists = []; |
| 31 | |
| 32 | private Generator $generator; |
| 33 | |
| 34 | public function __construct(private BoundedContextId $boundedContextId, private LazyLoadedListFilterer $filterer) |
| 35 | { |
| 36 | $this->generator = Factory::create(); |
| 37 | } |
| 38 | |
| 39 | public function all(ReflectionClass $class, ?BoundedContextId $boundedContextId = null): EntityListInterface |
| 40 | { |
| 41 | $className = $class->name; |
| 42 | $this->stored[$className] ??= []; |
| 43 | if (!isset($this->alreadyLoadedLists[$className])) { |
| 44 | $this->alreadyLoadedLists[$className] = new InMemoryEntityList( |
| 45 | $class, |
| 46 | $this->boundedContextId, |
| 47 | $this->filterer, |
| 48 | $this->stored[$className] |
| 49 | ); |
| 50 | } |
| 51 | return $this->alreadyLoadedLists[$className]; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @template T of EntityInterface |
| 56 | * @param T $entity |
| 57 | * @return T |
| 58 | */ |
| 59 | public function persistNew(EntityInterface $entity, ?BoundedContextId $boundedContextId = null): EntityInterface |
| 60 | { |
| 61 | $id = $entity->getId(); |
| 62 | if ($id instanceof AutoIncrementInteger) { |
| 63 | $id = $id::createRandom($this->generator); |
| 64 | $reflProperty = new ReflectionProperty($entity, 'id'); |
| 65 | $reflProperty->setValue($entity, $id); |
| 66 | } |
| 67 | $className = $id::getReferenceFor()->name; |
| 68 | $id = $entity->getId()->toNative(); |
| 69 | foreach ($this->stored[$className] ?? [] as $entityInList) { |
| 70 | if ($entityInList->getId()->toNative() === $id) { |
| 71 | throw new EntityAlreadyPersisted($entity); |
| 72 | } |
| 73 | } |
| 74 | $this->stored[$className][] = $entity; |
| 75 | return $entity; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @template T of EntityInterface |
| 80 | * @param T $entity |
| 81 | * @return T |
| 82 | */ |
| 83 | public function persistExisting(EntityInterface $entity, ?BoundedContextId $boundedContextId = null): EntityInterface |
| 84 | { |
| 85 | $id = $entity->getId()->toNative(); |
| 86 | $className = $entity->getId()::getReferenceFor()->name; |
| 87 | foreach ($this->stored[$className] ?? [] as $key => $entityInList) { |
| 88 | if ($entityInList->getId()->toNative() === $id) { |
| 89 | $this->stored[$className][$key] = $entity; |
| 90 | return $entity; |
| 91 | } |
| 92 | } |
| 93 | throw new UnknownExistingEntityError($entity); |
| 94 | } |
| 95 | |
| 96 | public function find(IdentifierInterface $identifier, ?BoundedContextId $boundedContextId = null): EntityInterface |
| 97 | { |
| 98 | $className = $identifier::getReferenceFor()->name; |
| 99 | $id = $identifier->toNative(); |
| 100 | foreach ($this->stored[$className] ?? [] as $entityInList) { |
| 101 | if ($entityInList->getId()->toNative() === $id) { |
| 102 | return $entityInList; |
| 103 | } |
| 104 | } |
| 105 | throw new EntityNotFoundException($identifier); |
| 106 | } |
| 107 | |
| 108 | public function removeExisting(EntityInterface $entity, ?BoundedContextId $boundedContextId = null): void |
| 109 | { |
| 110 | $identifier = $entity->getId(); |
| 111 | $className = $identifier::getReferenceFor()->name; |
| 112 | $id = $identifier->toNative(); |
| 113 | $newList = []; |
| 114 | foreach ($this->stored[$className] ?? [] as $entityInList) { |
| 115 | if ($entityInList->getId()->toNative() !== $id) { |
| 116 | $newList[] = $entityInList; |
| 117 | } |
| 118 | } |
| 119 | $this->stored[$className] = $newList; |
| 120 | } |
| 121 | |
| 122 | public function upsert(EntityInterface $entity, ?BoundedContextId $boundedContextId): EntityInterface |
| 123 | { |
| 124 | try { |
| 125 | return $this->persistExisting($entity, $boundedContextId); |
| 126 | } catch (UnknownExistingEntityError) { |
| 127 | return $this->persistNew($entity, $boundedContextId); |
| 128 | } |
| 129 | } |
| 130 | } |