Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.

Commit 86a7998

Browse files
bump up to superbalist/php-pubsub ^2.0 & add new dispatchBatch method to EventManager
1 parent 97fcb20 commit 86a7998

File tree

5 files changed

+90
-8
lines changed

5 files changed

+90
-8
lines changed

README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ $adapter = new \Superbalist\PubSub\Adapters\LocalPubSubAdapter();
3737
$translator = new \Superbalist\EventPubSub\Translators\SimpleEventMessageTranslator();
3838
$manager = new \Superbalist\EventPubSub\EventManager($adapter, $translator);
3939

40-
// publish an event
40+
// dispatch an event
4141
$event = new \Superbalist\EventPubSub\Events\SimpleEvent(
4242
'user.created',
4343
[
@@ -51,6 +51,27 @@ $event = new \Superbalist\EventPubSub\Events\SimpleEvent(
5151
);
5252
$manager->dispatch('events', $event);
5353

54+
// dispatch multiple events
55+
$events = [
56+
new \Superbalist\EventPubSub\Events\SimpleEvent(
57+
'user.created',
58+
[
59+
'user' => [
60+
// ...
61+
],
62+
]
63+
),
64+
new \Superbalist\EventPubSub\Events\SimpleEvent(
65+
'user.created',
66+
[
67+
'user' => [
68+
// ...
69+
],
70+
]
71+
),
72+
];
73+
$manager->dispatch('events', $events);
74+
5475
// listen for an event
5576
$manager->listen('events', 'user.created', function (\Superbalist\EventPubSub\EventInterface $event) {
5677
var_dump($event->getName());
@@ -73,7 +94,7 @@ $adapter = new \Superbalist\PubSub\Adapters\LocalPubSubAdapter();
7394
$translator = new \Superbalist\EventPubSub\Translators\TopicEventMessageTranslator();
7495
$manager = new \Superbalist\EventPubSub\EventManager($adapter, $translator);
7596

76-
// publish an event
97+
// dispatch an event
7798
$event = new \Superbalist\EventPubSub\Events\TopicEvent(
7899
'user',
79100
'created',
@@ -151,7 +172,7 @@ $validator = new \Superbalist\EventPubSub\Validators\JSONSchemaEventValidator($d
151172

152173
$manager = new \Superbalist\EventPubSub\EventManager($adapter, $translator, $validator);
153174

154-
// publish an event
175+
// dispatch an event
155176
$event = new \Superbalist\EventPubSub\Events\SchemaEvent(
156177
'http://schemas.my-website.org/events/user/created/1.0.json',
157178
[

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 3.0.0 - 2017-05-16
4+
5+
* Bump up to superbalist/php-pubsub ^2.0
6+
* Add new dispatchBatch method to EventManager for dispatching multiple events at once
7+
38
## 2.0.0 - 2017-02-01
49

510
* Add `setAttribute` method to `EventInterface`

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
],
1111
"require": {
1212
"php": ">=5.6.0",
13-
"superbalist/php-pubsub": "^1.0",
13+
"superbalist/php-pubsub": "^2.0",
1414
"composer/semver": "^1.4",
1515
"league/json-guard": "^0.5.1",
1616
"ramsey/uuid": "^3.5"

src/EventManager.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,10 @@ protected function getValuesFromAttributeInjectors()
139139
}
140140

141141
/**
142-
* Dispatch an event.
143-
*
144-
* @param string $channel
145142
* @param EventInterface $event
143+
* @return EventInterface
146144
*/
147-
public function dispatch($channel, EventInterface $event)
145+
protected function prepEventForDispatch(EventInterface $event)
148146
{
149147
// automagically inject attributes from injectors
150148
$attributes = $this->getValuesFromAttributeInjectors();
@@ -154,7 +152,32 @@ public function dispatch($channel, EventInterface $event)
154152
$e->setAttribute($k, $v);
155153
}
156154
}
155+
return $e;
156+
}
157157

158+
/**
159+
* Dispatch an event.
160+
*
161+
* @param string $channel
162+
* @param EventInterface $event
163+
*/
164+
public function dispatch($channel, EventInterface $event)
165+
{
166+
$e = $this->prepEventForDispatch($event);
158167
$this->adapter->publish($channel, $e->toMessage());
159168
}
169+
170+
/**
171+
* Dispatch multiple events.
172+
*
173+
* @param string $channel
174+
* @param array $events
175+
*/
176+
public function dispatchBatch($channel, array $events)
177+
{
178+
$messages = array_map(function (EventInterface $event) {
179+
return $event->toMessage();
180+
}, $events);
181+
$this->adapter->publishBatch($channel, $messages);
182+
}
160183
}

tests/EventManagerTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,37 @@ public function testDispatchWithInjectionsDoNotOverrideExistingAttributes()
155155
$event = new SimpleEvent('user.created', ['user' => ['id' => 1234], 'date' => 'my_date']);
156156
$manager->dispatch('channel', $event);
157157
}
158+
159+
public function testDispatchBatch()
160+
{
161+
$adapter = Mockery::mock(PubSubAdapterInterface::class);
162+
$adapter->shouldReceive('publishBatch')
163+
->withArgs([
164+
'channel',
165+
[
166+
[
167+
'event' => 'user.created',
168+
'user' => [
169+
'id' => 1234,
170+
],
171+
],
172+
[
173+
'event' => 'user.created',
174+
'user' => [
175+
'id' => 7812,
176+
],
177+
],
178+
]
179+
]);
180+
181+
$translator = Mockery::mock(MessageTranslatorInterface::class);
182+
183+
$manager = new EventManager($adapter, $translator);
184+
185+
$events = [
186+
new SimpleEvent('user.created', ['user' => ['id' => 1234]]),
187+
new SimpleEvent('user.created', ['user' => ['id' => 7812]]),
188+
];
189+
$manager->dispatchBatch('channel', $events);
190+
}
158191
}

0 commit comments

Comments
 (0)