-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathAttributeAggregateRootMetadataFactory.php
More file actions
336 lines (261 loc) · 10.5 KB
/
AttributeAggregateRootMetadataFactory.php
File metadata and controls
336 lines (261 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
declare(strict_types=1);
namespace Patchlevel\EventSourcing\Metadata\AggregateRoot;
use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
use Patchlevel\EventSourcing\Attribute\Aggregate;
use Patchlevel\EventSourcing\Attribute\Apply;
use Patchlevel\EventSourcing\Attribute\AutoInitialize;
use Patchlevel\EventSourcing\Attribute\ChildAggregate;
use Patchlevel\EventSourcing\Attribute\Id;
use Patchlevel\EventSourcing\Attribute\SharedApplyContext;
use Patchlevel\EventSourcing\Attribute\Snapshot as AttributeSnapshot;
use Patchlevel\EventSourcing\Attribute\Stream;
use Patchlevel\EventSourcing\Attribute\SuppressMissingApply;
use ReflectionClass;
use ReflectionIntersectionType;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionUnionType;
use RuntimeException;
use function array_key_exists;
use function array_map;
use function array_merge;
use function class_exists;
use function is_a;
final class AttributeAggregateRootMetadataFactory implements AggregateRootMetadataFactory
{
/** @var array<class-string<AggregateRoot>, AggregateRootMetadata> */
private array $aggregateMetadata = [];
/**
* @param class-string<T> $aggregate
*
* @return AggregateRootMetadata<T>
*
* @template T of AggregateRoot
*/
public function metadata(string $aggregate): AggregateRootMetadata
{
if (array_key_exists($aggregate, $this->aggregateMetadata)) {
return $this->aggregateMetadata[$aggregate];
}
$reflectionClass = new ReflectionClass($aggregate);
$aggregateName = $this->findAggregateName($reflectionClass);
$idProperty = $this->findIdProperty($reflectionClass);
$childAggregates = $this->findChildAggregates($reflectionClass);
[$suppressEvents, $suppressAll] = $this->findSuppressMissingApply($reflectionClass);
$applyMethods = $this->findApplyMethods($reflectionClass, $aggregate, $childAggregates);
$snapshot = $this->findSnapshot($reflectionClass);
$autoInitializeMethod = $this->findAutoInitializeMethod($reflectionClass);
$metadata = new AggregateRootMetadata(
$aggregate,
$aggregateName,
$idProperty,
$applyMethods,
$suppressEvents,
$suppressAll,
$snapshot,
array_map(static fn (array $list) => $list[0], $childAggregates),
$this->findStreamName($reflectionClass),
$autoInitializeMethod,
);
$this->aggregateMetadata[$aggregate] = $metadata;
return $metadata;
}
/** @return array{array<class-string, true>, bool} */
private function findSuppressMissingApply(ReflectionClass $reflector): array
{
$suppressEvents = [];
$attributes = $reflector->getAttributes(SuppressMissingApply::class);
if ($attributes !== []) {
$instance = $attributes[0]->newInstance();
if ($instance->suppressAll) {
return [[], true];
}
foreach ($instance->suppressEvents as $event) {
$suppressEvents[$event] = true;
}
}
$attributes = $reflector->getAttributes(SharedApplyContext::class);
if ($attributes !== []) {
$instance = $attributes[0]->newInstance();
foreach ($instance->aggregates as $aggregateClass) {
$reflectionClass = new ReflectionClass($aggregateClass);
$applyMethods = $this->findApplyMethods(
$reflectionClass,
$aggregateClass,
$this->findChildAggregates($reflectionClass),
);
foreach ($applyMethods as $eventClass => $method) {
$suppressEvents[$eventClass] = true;
}
}
}
return [$suppressEvents, false];
}
private function findAggregateName(ReflectionClass $reflector): string
{
$attributeReflectionList = $reflector->getAttributes(Aggregate::class);
if (!$attributeReflectionList) {
throw new ClassIsNotAnAggregate($reflector->getName());
}
$aggregateAttribute = $attributeReflectionList[0]->newInstance();
return $aggregateAttribute->name;
}
private function findIdProperty(ReflectionClass $reflector): string
{
$properties = $reflector->getProperties();
foreach ($properties as $property) {
$attributes = $property->getAttributes(Id::class);
if ($attributes === []) {
continue;
}
return $property->getName();
}
throw new AggregateRootIdNotFound($reflector->getName());
}
private function findSnapshot(ReflectionClass $reflector): Snapshot|null
{
$attributeReflectionList = $reflector->getAttributes(AttributeSnapshot::class);
if (!$attributeReflectionList) {
return null;
}
$attribute = $attributeReflectionList[0]->newInstance();
return new Snapshot(
$attribute->name,
$attribute->batch,
$attribute->version,
);
}
private function findStreamName(ReflectionClass $reflector): string|null
{
$attributes = $reflector->getAttributes(Stream::class);
if ($attributes === []) {
return null;
}
$streamName = $attributes[0]->newInstance()->name;
if (class_exists($streamName) && is_a($streamName, AggregateRoot::class, true)) {
return $this->metadata($streamName)->streamName;
}
return $attributes[0]->newInstance()->name;
}
private function findAutoInitializeMethod(ReflectionClass $reflector): string|null
{
foreach ($reflector->getMethods() as $method) {
$attributes = $method->getAttributes(AutoInitialize::class);
if ($attributes !== []) {
return $method->getName();
}
}
return null;
}
/** @return list<array{string, ReflectionClass}> */
private function findChildAggregates(ReflectionClass $reflector): array
{
$properties = $reflector->getProperties();
$childAggregates = [];
foreach ($properties as $property) {
$attributes = $property->getAttributes(ChildAggregate::class);
if ($attributes === []) {
continue;
}
$reflectionType = $property->getType();
if (!$reflectionType instanceof ReflectionNamedType) {
throw new RuntimeException('no intersection / union supported');
}
if (!is_a($reflectionType->getName(), \Patchlevel\EventSourcing\Aggregate\ChildAggregate::class, true)) {
throw new RuntimeException('no child');
}
$childAggregates[] = [$property->getName(), new ReflectionClass($reflectionType->getName())];
}
return $childAggregates;
}
/**
* @param class-string<AggregateRoot> $aggregate
* @param list<array{string, ReflectionClass}> $childAggregates
*
* @return array<class-string, string>
*/
private function findApplyMethods(ReflectionClass $reflector, string $aggregate, array $childAggregates): array
{
$applyMethods = [];
/** @var list<array{string, ReflectionMethod}> $methodList */
$methodList = [];
foreach ($reflector->getMethods() as $method) {
$methodList[] = [$method->getName(), $method];
}
foreach ($childAggregates as [$propertyName, $childReflector]) {
foreach ($childReflector->getMethods() as $method) {
$methodList[] = [$propertyName . '.' . $method->getName(), $method];
}
}
// process apply methods
foreach ($methodList as [$path, $method]) {
$attributes = $method->getAttributes(Apply::class);
if ($attributes === []) {
continue;
}
$eventClasses = [];
$hasOneEmptyApply = false;
$hasOneNonEmptyApply = false;
foreach ($attributes as $attribute) {
$applyAttribute = $attribute->newInstance();
$eventClass = $applyAttribute->eventClass;
if ($eventClass !== null) {
$hasOneNonEmptyApply = true;
$eventClasses[] = $eventClass;
continue;
}
if ($hasOneEmptyApply) {
throw new DuplicateEmptyApplyAttribute($path);
}
$hasOneEmptyApply = true;
$eventClasses = array_merge($eventClasses, $this->getEventClassesByPropertyTypes($method));
}
if ($hasOneEmptyApply && $hasOneNonEmptyApply) {
throw new MixedApplyAttributeUsage($path);
}
foreach ($eventClasses as $eventClass) {
if (!class_exists($eventClass)) {
throw new ArgumentTypeIsNotAClass($path, $eventClass);
}
if (array_key_exists($eventClass, $applyMethods)) {
throw new DuplicateApplyMethod(
$aggregate,
$eventClass,
$applyMethods[$eventClass],
$path,
);
}
$applyMethods[$eventClass] = $path;
}
}
return $applyMethods;
}
/** @return array<string> */
private function getEventClassesByPropertyTypes(ReflectionMethod $method): array
{
$propertyType = $method->getParameters()[0]->getType();
$methodName = $method->getName();
if ($propertyType === null) {
throw new ArgumentTypeIsMissing($methodName);
}
if ($propertyType instanceof ReflectionIntersectionType) {
throw new ArgumentTypeIsMissing($methodName);
}
if ($propertyType instanceof ReflectionNamedType) {
return [$propertyType->getName()];
}
if ($propertyType instanceof ReflectionUnionType) {
return array_map(
static function (ReflectionNamedType|ReflectionIntersectionType $reflectionType) use ($methodName): string {
if ($reflectionType instanceof ReflectionIntersectionType) {
throw new ArgumentTypeIsMissing($methodName);
}
return $reflectionType->getName();
},
$propertyType->getTypes(),
);
}
return [];
}
}