Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
91.80% |
112 / 122 |
|
75.00% |
9 / 12 |
CRAP | |
0.00% |
0 / 1 |
| SessionDatalayer | |
91.80% |
112 / 122 |
|
75.00% |
9 / 12 |
29.46 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| restore | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| store | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| refresh | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| all | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
2 | |||
| persistNew | |
96.88% |
31 / 32 |
|
0.00% |
0 / 1 |
7 | |||
| persistExisting | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
3 | |||
| find | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| getMetadata | |
46.67% |
7 / 15 |
|
0.00% |
0 / 1 |
2.61 | |||
| removeExisting | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
3.00 | |||
| upsert | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| storeMetadata | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Datalayers\InMemory; |
| 3 | |
| 4 | use Apie\Core\ApieLib; |
| 5 | use Apie\Core\Attributes\ClassStoreOptions; |
| 6 | use Apie\Core\BoundedContext\BoundedContextId; |
| 7 | use Apie\Core\Datalayers\ApieDatalayerWithFilters; |
| 8 | use Apie\Core\Datalayers\Concerns\FiltersOnAllFields; |
| 9 | use Apie\Core\Datalayers\DataLayerMetadata; |
| 10 | use Apie\Core\Datalayers\Lists\EntityListInterface; |
| 11 | use Apie\Core\Datalayers\Lists\InMemoryEntityList; |
| 12 | use Apie\Core\Datalayers\Search\LazyLoadedListFilterer; |
| 13 | use Apie\Core\Entities\EntityInterface; |
| 14 | use Apie\Core\Enums\SortingOrder; |
| 15 | use Apie\Core\Exceptions\EntityAlreadyPersisted; |
| 16 | use Apie\Core\Exceptions\EntityNotFoundException; |
| 17 | use Apie\Core\Exceptions\UnknownExistingEntityError; |
| 18 | use Apie\Core\Identifiers\AutoIncrementInteger; |
| 19 | use Apie\Core\Identifiers\IdentifierInterface; |
| 20 | use Apie\Core\Session\SessionFactoryInterface; |
| 21 | use Faker\Factory; |
| 22 | use Faker\Generator; |
| 23 | use ReflectionClass; |
| 24 | use ReflectionProperty; |
| 25 | use Symfony\Component\HttpFoundation\Session\SessionInterface; |
| 26 | |
| 27 | class SessionDatalayer implements ApieDatalayerWithFilters |
| 28 | { |
| 29 | use FiltersOnAllFields; |
| 30 | |
| 31 | /** |
| 32 | * @var array<string, array<string, array<int, EntityInterface>>> |
| 33 | */ |
| 34 | private array $stored = []; |
| 35 | |
| 36 | /** |
| 37 | * @var array<string, array<class-string<EntityInterface>, EntityListInterface<EntityInterface>>> |
| 38 | */ |
| 39 | private array $alreadyLoadedLists = []; |
| 40 | |
| 41 | private Generator $generator; |
| 42 | |
| 43 | /** @var array<string, array<class-string<EntityInterface>, array<string, DataLayerMetadata>>> */ |
| 44 | private array $metadata = []; |
| 45 | |
| 46 | private readonly SessionInterface $session; |
| 47 | |
| 48 | public function __construct( |
| 49 | SessionFactoryInterface $sessionFactory, |
| 50 | private readonly LazyLoadedListFilterer $filterer, |
| 51 | private readonly string $sessionKey = 'datalayer' |
| 52 | ) { |
| 53 | $this->session = $sessionFactory->create(); |
| 54 | $this->generator = Factory::create(); |
| 55 | $this->stored = $this->restore(); |
| 56 | $this->metadata = $this->session->get($this->sessionKey . '_metadata', []); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @return array<string, array<string, array<int, EntityInterface>>> |
| 61 | */ |
| 62 | protected function restore(): array |
| 63 | { |
| 64 | return $this->session->get($this->sessionKey, []); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param array<string, array<string, array<int, EntityInterface>>> $list |
| 69 | */ |
| 70 | protected function store(array $list): void |
| 71 | { |
| 72 | $this->session->set($this->sessionKey, $list); |
| 73 | } |
| 74 | |
| 75 | private function refresh(): void |
| 76 | { |
| 77 | $stored = $this->restore(); |
| 78 | if ($stored !== $this->stored) { |
| 79 | $this->stored = $stored; |
| 80 | $this->alreadyLoadedLists = []; |
| 81 | } |
| 82 | $metadata = $this->session->get($this->sessionKey . '_metadata', []); |
| 83 | if ($metadata !== $this->metadata) { |
| 84 | $this->metadata = $metadata; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | public function all(ReflectionClass $class, ?BoundedContextId $boundedContextId = null): EntityListInterface |
| 89 | { |
| 90 | $this->refresh(); |
| 91 | $boundedContextId ??= new BoundedContextId('unknown'); |
| 92 | $boundedContextIdString = $boundedContextId->toNative(); |
| 93 | $className = $class->name; |
| 94 | $this->stored[$boundedContextIdString][$className] ??= []; |
| 95 | if (!isset($this->alreadyLoadedLists[$boundedContextIdString][$className])) { |
| 96 | $this->alreadyLoadedLists[$boundedContextIdString][$className] = new InMemoryEntityList( |
| 97 | $class, |
| 98 | $boundedContextId, |
| 99 | $this->filterer, |
| 100 | $this->stored[$boundedContextIdString][$className] |
| 101 | ); |
| 102 | } |
| 103 | return $this->alreadyLoadedLists[$boundedContextIdString][$className]; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @template T of EntityInterface |
| 108 | * @param T $entity |
| 109 | * @return T |
| 110 | */ |
| 111 | public function persistNew(EntityInterface $entity, ?BoundedContextId $boundedContextId = null): EntityInterface |
| 112 | { |
| 113 | $this->refresh(); |
| 114 | $boundedContextId ??= new BoundedContextId('unknown'); |
| 115 | $boundedContextIdString = $boundedContextId->toNative(); |
| 116 | $id = $entity->getId(); |
| 117 | if ($id instanceof AutoIncrementInteger) { |
| 118 | $id = $id::createRandom($this->generator); |
| 119 | $reflProperty = new ReflectionProperty($entity, 'id'); |
| 120 | $reflProperty->setValue($entity, $id); |
| 121 | } |
| 122 | $reference = $id::getReferenceFor(); |
| 123 | $className = $reference->name; |
| 124 | $id = $entity->getId()->toNative(); |
| 125 | foreach ($this->stored[$boundedContextIdString][$className] ?? [] as $entityInList) { |
| 126 | if ($entityInList->getId()->toNative() === $id) { |
| 127 | throw new EntityAlreadyPersisted($entity); |
| 128 | } |
| 129 | } |
| 130 | $sortOrder = SortingOrder::Descending; |
| 131 | foreach ($reference->getAttributes(ClassStoreOptions::class) as $attribute) { |
| 132 | if ($attribute->newInstance()->defaultSortingOrder === SortingOrder::Ascending) { |
| 133 | $sortOrder = SortingOrder::Ascending; |
| 134 | } |
| 135 | } |
| 136 | $this->stored[$boundedContextIdString][$className] ??= []; |
| 137 | if ($sortOrder === SortingOrder::Ascending) { |
| 138 | $this->stored[$boundedContextIdString][$className][] = $entity; |
| 139 | } else { |
| 140 | array_unshift($this->stored[$boundedContextIdString][$className], $entity); |
| 141 | } |
| 142 | $this->store($this->stored); |
| 143 | $now = ApieLib::getPsrClock()->now(); |
| 144 | $this->metadata[$boundedContextIdString][$className][(string) $id] = new DataLayerMetadata( |
| 145 | $now, |
| 146 | $now, |
| 147 | ApieLib::VERSION, |
| 148 | ApieLib::VERSION |
| 149 | ); |
| 150 | $this->storeMetadata(); |
| 151 | return $entity; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @template T of EntityInterface |
| 156 | * @param T $entity |
| 157 | * @return T |
| 158 | */ |
| 159 | public function persistExisting(EntityInterface $entity, ?BoundedContextId $boundedContextId = null): EntityInterface |
| 160 | { |
| 161 | $this->refresh(); |
| 162 | $boundedContextId ??= new BoundedContextId('unknown'); |
| 163 | $boundedContextIdString = $boundedContextId->toNative(); |
| 164 | $id = $entity->getId()->toNative(); |
| 165 | $className = $entity->getId()::getReferenceFor()->name; |
| 166 | foreach ($this->stored[$boundedContextIdString][$className] ?? [] as $key => $entityInList) { |
| 167 | if ($entityInList->getId()->toNative() === $id) { |
| 168 | $this->stored[$boundedContextIdString][$className][$key] = $entity; |
| 169 | $this->store($this->stored); |
| 170 | $metadata = $this->metadata[$boundedContextIdString][$className][(string) $id] ?? new DataLayerMetadata( |
| 171 | ApieLib::getPsrClock()->now(), |
| 172 | ApieLib::getPsrClock()->now(), |
| 173 | ApieLib::VERSION, |
| 174 | ApieLib::VERSION |
| 175 | ); |
| 176 | $this->metadata[$boundedContextIdString][$className][(string) $id] = $metadata->withUpdatedMetadata( |
| 177 | ApieLib::getPsrClock()->now(), |
| 178 | ApieLib::VERSION |
| 179 | ); |
| 180 | $this->storeMetadata(); |
| 181 | return $entity; |
| 182 | } |
| 183 | } |
| 184 | throw new UnknownExistingEntityError($entity); |
| 185 | } |
| 186 | |
| 187 | public function find(IdentifierInterface $identifier, ?BoundedContextId $boundedContextId = null): EntityInterface |
| 188 | { |
| 189 | $this->refresh(); |
| 190 | $boundedContextId ??= new BoundedContextId('unknown'); |
| 191 | $boundedContextIdString = $boundedContextId->toNative(); |
| 192 | $className = $identifier::getReferenceFor()->name; |
| 193 | $id = $identifier->toNative(); |
| 194 | foreach ($this->stored[$boundedContextIdString][$className] ?? [] as $entityInList) { |
| 195 | if ($entityInList->getId()->toNative() === $id) { |
| 196 | return $entityInList; |
| 197 | } |
| 198 | } |
| 199 | throw new EntityNotFoundException($identifier); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @template T of EntityInterface |
| 204 | * @param IdentifierInterface<T> $identifier |
| 205 | */ |
| 206 | public function getMetadata( |
| 207 | IdentifierInterface $identifier, |
| 208 | ?BoundedContextId $boundedContextId = null |
| 209 | ): DataLayerMetadata { |
| 210 | $boundedContextId ??= new BoundedContextId('unknown'); |
| 211 | $this->find($identifier, $boundedContextId); |
| 212 | $contextId = $boundedContextId->toNative(); |
| 213 | $className = $identifier::getReferenceFor()->name; |
| 214 | $id = (string) $identifier->toNative(); |
| 215 | if (!isset($this->metadata[$contextId][$className][$id])) { |
| 216 | $now = ApieLib::getPsrClock()->now(); |
| 217 | $this->metadata[$contextId][$className][$id] = new DataLayerMetadata( |
| 218 | $now, |
| 219 | $now, |
| 220 | ApieLib::VERSION, |
| 221 | ApieLib::VERSION |
| 222 | ); |
| 223 | $this->storeMetadata(); |
| 224 | } |
| 225 | |
| 226 | return $this->metadata[$contextId][$className][$id]; |
| 227 | } |
| 228 | |
| 229 | public function removeExisting(EntityInterface $entity, ?BoundedContextId $boundedContextId = null): void |
| 230 | { |
| 231 | $this->refresh(); |
| 232 | $boundedContextId ??= new BoundedContextId('unknown'); |
| 233 | $boundedContextIdString = $boundedContextId->toNative(); |
| 234 | $identifier = $entity->getId(); |
| 235 | $className = $identifier::getReferenceFor()->name; |
| 236 | $id = $identifier->toNative(); |
| 237 | $newList = []; |
| 238 | foreach ($this->stored[$boundedContextIdString][$className] ?? [] as $entityInList) { |
| 239 | if ($entityInList->getId()->toNative() !== $id) { |
| 240 | $newList[] = $entityInList; |
| 241 | } |
| 242 | } |
| 243 | $this->stored[$boundedContextIdString][$className] = $newList; |
| 244 | unset($this->metadata[$boundedContextIdString][$className][(string) $id]); |
| 245 | $this->store($this->stored); |
| 246 | $this->storeMetadata(); |
| 247 | } |
| 248 | |
| 249 | public function upsert(EntityInterface $entity, ?BoundedContextId $boundedContextId): EntityInterface |
| 250 | { |
| 251 | try { |
| 252 | return $this->persistExisting($entity, $boundedContextId); |
| 253 | } catch (UnknownExistingEntityError) { |
| 254 | return $this->persistNew($entity, $boundedContextId); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | private function storeMetadata(): void |
| 259 | { |
| 260 | $this->session->set($this->sessionKey . '_metadata', $this->metadata); |
| 261 | } |
| 262 | } |