1 | <?php |
||
17 | class EventMultiHandler implements EventHandlerInterface |
||
18 | { |
||
19 | /** |
||
20 | * List of handlers |
||
21 | * |
||
22 | * @var EventHandlerInterface[] |
||
23 | */ |
||
24 | private $handlers = []; |
||
25 | |||
26 | /** |
||
27 | * EventMultiHandler constructor. |
||
28 | * |
||
29 | * @param EventHandlerInterface[] $handlers List of handlers |
||
30 | */ |
||
31 | 4 | public function __construct(array $handlers = []) |
|
32 | { |
||
33 | 4 | $this->handlers = []; |
|
34 | 4 | foreach ($handlers as $handler) { |
|
35 | 1 | $this->addHandler($handler); |
|
36 | 4 | } |
|
37 | 4 | } |
|
38 | |||
39 | /** {@inheritdoc} */ |
||
40 | 4 | public function invokeEvent(Event $event) |
|
41 | { |
||
42 | 4 | foreach ($this->handlers as $handler) { |
|
43 | 3 | $handler->invokeEvent($event); |
|
44 | 4 | } |
|
45 | 4 | } |
|
46 | |||
47 | /** |
||
48 | * Add handler to list |
||
49 | * |
||
50 | * @param EventHandlerInterface $handler Handler to add |
||
51 | * |
||
52 | * @return void |
||
53 | */ |
||
54 | 4 | public function addHandler(EventHandlerInterface $handler) |
|
55 | { |
||
56 | 4 | $key = $this->getHandlerKey($handler); |
|
57 | 4 | $this->handlers[$key] = $handler; |
|
58 | 4 | } |
|
59 | |||
60 | /** |
||
61 | * Remove handler from list |
||
62 | * |
||
63 | * @param EventHandlerInterface $handler Handler to remove |
||
64 | * |
||
65 | * @return void |
||
66 | */ |
||
67 | 1 | public function removeHandler(EventHandlerInterface $handler) |
|
68 | { |
||
69 | 1 | $key = $this->getHandlerKey($handler); |
|
70 | 1 | if (isset($this->handlers[$key])) { |
|
71 | 1 | unset($this->handlers[$key]); |
|
72 | 1 | } |
|
73 | 1 | } |
|
74 | |||
75 | /** |
||
76 | * Return key for given handler |
||
77 | * |
||
78 | * @param EventHandlerInterface $handler Handler to get key for |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | 4 | private function getHandlerKey(EventHandlerInterface $handler) |
|
86 | } |
||
87 |