-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMetadataSubscriberAccessor.php
More file actions
193 lines (155 loc) · 5.08 KB
/
Copy pathMetadataSubscriberAccessor.php
File metadata and controls
193 lines (155 loc) · 5.08 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
<?php
declare(strict_types=1);
namespace Patchlevel\EventSourcing\Subscription\Subscriber;
use Closure;
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Message\Message;
use Patchlevel\EventSourcing\Metadata\Subscriber\SubscribeMethodMetadata;
use Patchlevel\EventSourcing\Metadata\Subscriber\SubscriberMetadata;
use Patchlevel\EventSourcing\Subscription\RunMode;
use Patchlevel\EventSourcing\Subscription\Subscriber\ArgumentResolver\ArgumentResolver;
use Throwable;
use function array_key_exists;
use function array_keys;
use function array_map;
/** @template T of object */
final class MetadataSubscriberAccessor implements SubscriberAccessor, RealSubscriberAccessor
{
/** @var array<class-string, list<Closure(Message):void>> */
private array $subscribeCache = [];
/**
* @param T $subscriber
* @param list<ArgumentResolver> $argumentResolvers
*/
public function __construct(
private readonly object $subscriber,
private readonly SubscriberMetadata $metadata,
private readonly array $argumentResolvers,
) {
}
public function metadata(): SubscriberMetadata
{
return $this->metadata;
}
/** @return T */
public function subscriber(): object
{
return $this->subscriber;
}
/** @deprecated use `->metadata()->id` instead */
public function id(): string
{
return $this->metadata->id;
}
/** @deprecated use `->metadata()->group` instead */
public function group(): string
{
return $this->metadata->group;
}
/** @deprecated use `->metadata()->runMode` instead */
public function runMode(): RunMode
{
return $this->metadata->runMode;
}
public function setupMethod(): Closure|null
{
$method = $this->metadata->setupMethod;
if ($method === null) {
return null;
}
return $this->subscriber->$method(...);
}
public function teardownMethod(): Closure|null
{
$method = $this->metadata->teardownMethod;
if ($method === null) {
return null;
}
return $this->subscriber->$method(...);
}
/** @return Closure(Message, Throwable):void|null */
public function failedMethod(): Closure|null
{
$method = $this->metadata->failedMethod;
if ($method === null) {
return null;
}
return $this->subscriber->$method(...);
}
/** @return list<class-string|'*'> */
public function events(): array
{
return array_keys($this->metadata->subscribeMethods);
}
/**
* @param class-string $eventClass
*
* @return list<Closure(Message):void>
*/
public function subscribeMethods(string $eventClass): array
{
if (array_key_exists($eventClass, $this->subscribeCache)) {
return $this->subscribeCache[$eventClass];
}
$methods = [];
if (array_key_exists($eventClass, $this->metadata->subscribeMethods)) {
$methods[] = $this->metadata->subscribeMethods[$eventClass];
}
if (array_key_exists(Subscribe::ALL, $this->metadata->subscribeMethods)) {
$methods[] = $this->metadata->subscribeMethods[Subscribe::ALL];
}
$this->subscribeCache[$eventClass] = array_map(
fn (SubscribeMethodMetadata $method): Closure => $this->createClosure($eventClass, $method),
$methods,
);
return $this->subscribeCache[$eventClass];
}
/**
* @param class-string $eventClass
*
* @return Closure(Message):void
*/
private function createClosure(string $eventClass, SubscribeMethodMetadata $method): Closure
{
$resolvers = $this->resolvers($eventClass, $method);
$methodName = $method->name;
return function (Message $message) use ($methodName, $resolvers): void {
$arguments = [];
foreach ($resolvers as $resolver) {
$arguments[] = $resolver($message);
}
$this->subscriber->$methodName(...$arguments);
};
}
/**
* @param class-string $eventClass
*
* @return list<Closure(Message):mixed>
*/
private function resolvers(string $eventClass, SubscribeMethodMetadata $method): array
{
$resolvers = [];
foreach ($method->arguments as $argument) {
foreach ($this->argumentResolvers as $resolver) {
if (!$resolver->support($argument, $eventClass)) {
continue;
}
$resolvers[] = static function (Message $message) use ($resolver, $argument): mixed {
return $resolver->resolve($argument, $message);
};
continue 2;
}
throw new NoSuitableResolver($this->subscriber::class, $method->name, $argument->name);
}
return $resolvers;
}
/**
* @deprecated use `->metadata()` instead
*
* @return T
*/
public function realSubscriber(): object
{
return $this->subscriber;
}
}