EventDispatcher::dispatch()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 4
nc 4
nop 1
1
<?php
2
3
namespace BornFree\TacticianDomainEvent\EventDispatcher;
4
5
class EventDispatcher implements EventDispatcherInterface, ContainsListenersInterface
6
{
7
    /**
8
     * @var callable[][]
9
     */
10
    private $listeners = [];
11
12
    /**
13
     * @inheritdoc
14
     */
15
    public function dispatch($event)
16
    {
17
        $name = $event instanceof NamedEvent ? $event->getName() : get_class($event);
18
19
        foreach ($this->getListeners($name) as $listener) {
20
            $listener($event);
21
        }
22
    }
23
24
    /**
25
     * @inheritdoc
26
     */
27
    public function addListener($eventName, callable $listener)
28
    {
29
        $this->listeners[$eventName][] = $listener;
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function getListeners($eventName)
36
    {
37
        if (! $this->hasListeners($eventName)) {
38
            return [];
39
        }
40
41
        return $this->listeners[$eventName];
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function hasListeners($eventName)
48
    {
49
        return isset($this->listeners[$eventName]);
50
    }
51
}