-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleEventBus.php
More file actions
51 lines (45 loc) · 1.17 KB
/
SimpleEventBus.php
File metadata and controls
51 lines (45 loc) · 1.17 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
<?php
namespace SmoothPhp\EventBus;
use SmoothPhp\Contracts\Domain\DomainEventStream;
use SmoothPhp\Contracts\Domain\DomainMessage;
use SmoothPhp\Contracts\EventBus\EventBus;
use SmoothPhp\Contracts\EventBus\EventListener;
/**
* Class SimpleEventBus
* @package SmoothPhp\EventBus
* @author Simon Bennett <simon@bennett.im>
*/
final class SimpleEventBus implements EventBus
{
/**
* @var EventListener[]
*/
private $eventListeners = [];
/**
* @var DomainMessage[]
*/
private $queue = [];
/**
* @param $eventListener
* @return mixed
*/
public function subscribe(EventListener $eventListener)
{
$this->eventListeners[] = $eventListener;
}
/**
* @param DomainEventStream $domainEventStream
* @return void
*/
public function publish(DomainEventStream $domainEventStream)
{
foreach ($domainEventStream as $domainMessage) {
$this->queue[] = $domainMessage;
}
while ($domainMessage = array_shift($this->queue)) {
foreach ($this->eventListeners as $eventListener) {
$eventListener->handle($domainMessage);
}
}
}
}