forked from patchlevel/event-sourcing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttributeSubscriberMetadataFactory.php
More file actions
176 lines (139 loc) · 5.27 KB
/
Copy pathAttributeSubscriberMetadataFactory.php
File metadata and controls
176 lines (139 loc) · 5.27 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
<?php
declare(strict_types=1);
namespace Patchlevel\EventSourcing\Metadata\Subscriber;
use Patchlevel\EventSourcing\Attribute\OnFailed;
use Patchlevel\EventSourcing\Attribute\RetryStrategy;
use Patchlevel\EventSourcing\Attribute\Setup;
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Attribute\Subscriber;
use Patchlevel\EventSourcing\Attribute\Teardown;
use ReflectionAttribute;
use ReflectionClass;
use ReflectionMethod;
use ReflectionNamedType;
use function array_key_exists;
use function count;
final class AttributeSubscriberMetadataFactory implements SubscriberMetadataFactory
{
/** @var array<class-string, SubscriberMetadata> */
private array $subscriberMetadata = [];
/** @param class-string $subscriber */
public function metadata(string $subscriber): SubscriberMetadata
{
if (array_key_exists($subscriber, $this->subscriberMetadata)) {
return $this->subscriberMetadata[$subscriber];
}
$reflector = new ReflectionClass($subscriber);
$attributes = $reflector->getAttributes(Subscriber::class, ReflectionAttribute::IS_INSTANCEOF);
if ($attributes === []) {
throw new ClassIsNotASubscriber($subscriber);
}
$subscriberInfo = $attributes[0]->newInstance();
$methods = $reflector->getMethods();
$subscribeMethods = [];
$setupMethod = null;
$teardownMethod = null;
$failedMethod = null;
foreach ($methods as $method) {
$attributes = $method->getAttributes(Subscribe::class);
foreach ($attributes as $attribute) {
$instance = $attribute->newInstance();
$eventClass = $instance->eventClass;
if (array_key_exists($eventClass, $subscribeMethods)) {
throw DuplicateSubscribeMethod::duplicateEvent(
$subscriber,
$eventClass,
$subscribeMethods[$eventClass][0]->name,
$method->getName(),
);
}
$subscribeMethods[$eventClass][] = $this->subscribeMethod($method);
}
if ($method->getAttributes(OnFailed::class)) {
if ($failedMethod !== null) {
throw new DuplicateFailedMethod(
$subscriber,
$failedMethod,
$method->getName(),
);
}
$failedMethod = $method->getName();
}
if ($method->getAttributes(Setup::class)) {
if ($setupMethod !== null) {
throw new DuplicateSetupMethod(
$subscriber,
$setupMethod,
$method->getName(),
);
}
$setupMethod = $method->getName();
}
if (!$method->getAttributes(Teardown::class)) {
continue;
}
if ($teardownMethod !== null) {
throw new DuplicateTeardownMethod(
$subscriber,
$teardownMethod,
$method->getName(),
);
}
$teardownMethod = $method->getName();
}
if (array_key_exists(Subscribe::ALL, $subscribeMethods) && count($subscribeMethods) > 1) {
throw DuplicateSubscribeMethod::mixedWithAll($subscriber);
}
$metadata = new SubscriberMetadata(
$subscriberInfo->id,
$subscriberInfo->group,
$subscriberInfo->runMode,
$subscribeMethods,
$setupMethod,
$teardownMethod,
$failedMethod,
$this->retryStrategy($reflector),
);
$this->subscriberMetadata[$subscriber] = $metadata;
return $metadata;
}
private function subscribeMethod(ReflectionMethod $method): SubscribeMethodMetadata
{
$arguments = [];
foreach ($method->getParameters() as $parameter) {
$type = $parameter->getType();
if ($type === null) {
throw ArgumentTypeNotSupported::missingType(
$method->getDeclaringClass()->getName(),
$method->getName(),
$parameter->getName(),
);
}
if (!$type instanceof ReflectionNamedType) {
throw ArgumentTypeNotSupported::onlyNamedTypeSupported(
$method->getDeclaringClass()->getName(),
$method->getName(),
$parameter->getName(),
);
}
$arguments[] = new ArgumentMetadata(
$parameter->getName(),
$type->getName(),
$parameter->allowsNull(),
);
}
return new SubscribeMethodMetadata(
$method->getName(),
$arguments,
);
}
private function retryStrategy(ReflectionClass $reflector): string|null
{
$attributes = $reflector->getAttributes(RetryStrategy::class);
if ($attributes === []) {
return null;
}
$instance = $attributes[0]->newInstance();
return $instance->name;
}
}