-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventBus.php
More file actions
56 lines (44 loc) · 1.13 KB
/
EventBus.php
File metadata and controls
56 lines (44 loc) · 1.13 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
<?php
namespace DevPledge\Integrations\Event;
/**
* Class EventBus
* @package DevPledge\Integrations\Event
*/
class EventBus {
protected $eventHandlerMap = [];
/**
* @param AbstractEvent $event
*/
public function handle( AbstractEvent $event ) {
$handlerClassArray = $this->getHandlers( get_class( $event ) );
if ( $handlerClassArray ) {
foreach ( $handlerClassArray as $handlerClass ) {
$handler = new $handlerClass();
call_user_func_array( $handler, array( $event ) );
}
}
}
/**
* @param $key
*
* @return AbstractEventHandler[] | null
*/
protected function getHandlers( $key ): ?array {
if ( isset( $this->eventHandlerMap[ $key ] ) ) {
return $this->eventHandlerMap[ $key ];
}
return null;
}
/**
* @param AbstractEventHandler $eventHandler
*
* @return $this
*/
public function setHandler( AbstractEventHandler $eventHandler ) {
if ( ! isset( $this->eventHandlerMap[ $eventHandler->getContainerKey() ] ) ) {
$this->eventHandlerMap[ $eventHandler->getContainerKey() ] = [];
}
$this->eventHandlerMap[ $eventHandler->getContainerKey() ][] = $eventHandler;
return $this;
}
}