Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
80.00% |
36 / 45 |
|
63.64% |
7 / 11 |
CRAP | |
0.00% |
0 / 1 |
GroupedDataLayer | |
80.00% |
36 / 45 |
|
63.64% |
7 / 11 |
18.05 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isSupported | |
75.00% |
6 / 8 |
|
0.00% |
0 / 1 |
4.25 | |||
getFilterColumns | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
getOrderByColumns | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
all | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
find | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
dispatch | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
persistNew | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
persistExisting | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
removeExisting | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
upsert | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | namespace Apie\Core\Datalayers; |
3 | |
4 | use Apie\Core\BoundedContext\BoundedContextId; |
5 | use Apie\Core\Datalayers\Events\EntityPersisted; |
6 | use Apie\Core\Datalayers\Grouped\DataLayerByBoundedContext; |
7 | use Apie\Core\Datalayers\Lists\EntityListInterface; |
8 | use Apie\Core\Entities\EntityInterface; |
9 | use Apie\Core\Identifiers\IdentifierInterface; |
10 | use Apie\Core\Lists\StringSet; |
11 | use Psr\EventDispatcher\EventDispatcherInterface; |
12 | use ReflectionClass; |
13 | |
14 | final class GroupedDataLayer implements ApieDatalayerWithFilters, ApieDatalayer, ApieDatalayerWithSupport |
15 | { |
16 | public function __construct( |
17 | private readonly DataLayerByBoundedContext $hashmap, |
18 | private readonly EventDispatcherInterface $dispatcher |
19 | ) { |
20 | } |
21 | |
22 | public function isSupported(EntityInterface|ReflectionClass|IdentifierInterface $instance, BoundedContextId $boundedContextId): bool |
23 | { |
24 | if ($instance instanceof EntityInterface) { |
25 | $instance = $instance->getId()->getReferenceFor(); |
26 | } elseif ($instance instanceof IdentifierInterface) { |
27 | $instance = $instance->getReferenceFor(); |
28 | } |
29 | $datalayer = $this->hashmap->pickDataLayerFor($instance, $boundedContextId); |
30 | if ($datalayer instanceof ApieDatalayerWithSupport) { |
31 | return $datalayer->isSupported($instance, $boundedContextId); |
32 | } |
33 | return true; |
34 | } |
35 | |
36 | public function getFilterColumns(ReflectionClass $class, BoundedContextId $boundedContextId): ?StringSet |
37 | { |
38 | $datalayer = $this->hashmap->pickDataLayerFor($class, $boundedContextId); |
39 | if ($datalayer instanceof ApieDatalayerWithFilters) { |
40 | return $datalayer->getFilterColumns($class, $boundedContextId); |
41 | } |
42 | return null; |
43 | } |
44 | |
45 | public function getOrderByColumns(ReflectionClass $class, BoundedContextId $boundedContextId): ?StringSet |
46 | { |
47 | $datalayer = $this->hashmap->pickDataLayerFor($class, $boundedContextId); |
48 | if ($datalayer instanceof ApieDatalayerWithFilters) { |
49 | return $datalayer->getOrderByColumns($class, $boundedContextId); |
50 | } |
51 | return null; |
52 | } |
53 | |
54 | public function all(ReflectionClass $class, ?BoundedContextId $boundedContextId = null): EntityListInterface |
55 | { |
56 | return $this->hashmap->pickDataLayerFor($class, $boundedContextId) |
57 | ->all($class, $boundedContextId); |
58 | } |
59 | |
60 | public function find(IdentifierInterface $identifier, ?BoundedContextId $boundedContextId = null): EntityInterface |
61 | { |
62 | return $this->hashmap->pickDataLayerFor($identifier::getReferenceFor(), $boundedContextId) |
63 | ->find($identifier, $boundedContextId); |
64 | } |
65 | |
66 | private function dispatch(EntityInterface $entity, ?BoundedContextId $boundedContextId = null): EntityInterface |
67 | { |
68 | $this->dispatcher->dispatch( |
69 | new EntityPersisted( |
70 | $entity, |
71 | $boundedContextId |
72 | ) |
73 | ); |
74 | return $entity; |
75 | } |
76 | |
77 | public function persistNew(EntityInterface $entity, ?BoundedContextId $boundedContextId = null): EntityInterface |
78 | { |
79 | return $this->dispatch( |
80 | $this->hashmap->pickDataLayerFor($entity->getId()::getReferenceFor(), $boundedContextId) |
81 | ->persistNew($entity, $boundedContextId), |
82 | $boundedContextId |
83 | ); |
84 | } |
85 | |
86 | public function persistExisting(EntityInterface $entity, ?BoundedContextId $boundedContextId = null): EntityInterface |
87 | { |
88 | return $this->dispatch( |
89 | $this->hashmap->pickDataLayerFor($entity->getId()::getReferenceFor(), $boundedContextId) |
90 | ->persistExisting($entity, $boundedContextId), |
91 | $boundedContextId |
92 | ); |
93 | } |
94 | |
95 | public function removeExisting(EntityInterface $entity, ?BoundedContextId $boundedContextId = null): void |
96 | { |
97 | $this->hashmap->pickDataLayerFor($entity->getId()::getReferenceFor(), $boundedContextId) |
98 | ->removeExisting($entity, $boundedContextId); |
99 | } |
100 | |
101 | public function upsert(EntityInterface $entity, ?BoundedContextId $boundedContextId): EntityInterface |
102 | { |
103 | return $this->dispatch( |
104 | $this->hashmap->pickDataLayerFor($entity->getId()::getReferenceFor(), $boundedContextId) |
105 | ->upsert($entity, $boundedContextId), |
106 | $boundedContextId |
107 | ); |
108 | } |
109 | } |