EventDispatcher   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 47
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatch() 0 8 3
A addListener() 0 4 1
A getListeners() 0 8 2
A hasListeners() 0 4 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
}