Skip to content

Commit cec411e

Browse files
committed
Update API of SubscriberMetadata to not hold a list of methods for given events.
Ref: #701
1 parent 0e0ef8a commit cec411e

4 files changed

Lines changed: 26 additions & 22 deletions

File tree

src/Metadata/Subscriber/AttributeSubscriberMetadataFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ public function metadata(string $subscriber): SubscriberMetadata
5858
throw DuplicateSubscribeMethod::duplicateEvent(
5959
$subscriber,
6060
$eventClass,
61-
$subscribeMethods[$eventClass][0]->name,
61+
$subscribeMethods[$eventClass]->name,
6262
$method->getName(),
6363
);
6464
}
6565

66-
$subscribeMethods[$eventClass][] = $this->subscribeMethod($method);
66+
$subscribeMethods[$eventClass] = $this->subscribeMethod($method);
6767
}
6868

6969
if ($method->getAttributes(OnFailed::class)) {

src/Metadata/Subscriber/SubscriberMetadata.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function __construct(
1313
public readonly string $id,
1414
public readonly string $group = Subscription::DEFAULT_GROUP,
1515
public readonly RunMode $runMode = RunMode::FromBeginning,
16-
/** @var array<class-string|"*", list<SubscribeMethodMetadata>> */
16+
/** @var array<class-string|"*", SubscribeMethodMetadata> */
1717
public readonly array $subscribeMethods = [],
1818
public readonly string|null $setupMethod = null,
1919
public readonly string|null $teardownMethod = null,

src/Subscription/Subscriber/MetadataSubscriberAccessor.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,15 @@ public function subscribeMethods(string $eventClass): array
115115
return $this->subscribeCache[$eventClass];
116116
}
117117

118-
$methods = array_merge(
119-
$this->metadata->subscribeMethods[$eventClass] ?? [],
120-
$this->metadata->subscribeMethods[Subscribe::ALL] ?? [],
121-
);
118+
$methods = [];
119+
120+
if (array_key_exists($eventClass, $this->metadata->subscribeMethods)) {
121+
$methods[] = $this->metadata->subscribeMethods[$eventClass];
122+
}
123+
124+
if (array_key_exists(Subscribe::ALL, $this->metadata->subscribeMethods)) {
125+
$methods[] = $this->metadata->subscribeMethods[Subscribe::ALL];
126+
}
122127

123128
$this->subscribeCache[$eventClass] = array_map(
124129
fn (SubscribeMethodMetadata $method): Closure => $this->createClosure($eventClass, $method),

tests/Unit/Metadata/Subscriber/AttributeSubscriberMetadataFactoryTest.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ public function drop(): void
113113

114114
self::assertEquals(
115115
[
116-
ProfileVisited::class => [
117-
new SubscribeMethodMetadata('handle', []),
118-
],
116+
ProfileVisited::class => new SubscribeMethodMetadata('handle', []),
119117
],
120118
$metadata->subscribeMethods,
121119
);
@@ -140,8 +138,8 @@ public function handle(): void
140138

141139
self::assertEquals(
142140
[
143-
ProfileVisited::class => [new SubscribeMethodMetadata('handle', [])],
144-
ProfileCreated::class => [new SubscribeMethodMetadata('handle', [])],
141+
ProfileVisited::class => new SubscribeMethodMetadata('handle', []),
142+
ProfileCreated::class => new SubscribeMethodMetadata('handle', []),
145143
],
146144
$metadata->subscribeMethods,
147145
);
@@ -162,7 +160,7 @@ public function handle(): void
162160

163161
self::assertEquals(
164162
[
165-
'*' => [new SubscribeMethodMetadata('handle', [])],
163+
'*' => new SubscribeMethodMetadata('handle', []),
166164
],
167165
$metadata->subscribeMethods,
168166
);
@@ -188,17 +186,18 @@ public function profileCreated(ProfileCreated $profileCreated, string $aggregate
188186

189187
self::assertEquals(
190188
[
191-
ProfileVisited::class => [
192-
new SubscribeMethodMetadata('profileVisited', [
193-
new ArgumentMetadata('message', Message::class),
194-
]),
195-
],
196-
ProfileCreated::class => [
197-
new SubscribeMethodMetadata('profileCreated', [
189+
ProfileVisited::class => new SubscribeMethodMetadata(
190+
'profileVisited',
191+
[new ArgumentMetadata('message', Message::class)],
192+
),
193+
194+
ProfileCreated::class => new SubscribeMethodMetadata(
195+
'profileCreated',
196+
[
198197
new ArgumentMetadata('profileCreated', ProfileCreated::class),
199198
new ArgumentMetadata('aggregateId', 'string'),
200-
]),
201-
],
199+
]
200+
),
202201
],
203202
$metadata->subscribeMethods,
204203
);

0 commit comments

Comments
 (0)