Completed
Push — master ( 4d2a3e...f3605b )
by Daniel
02:16
created

SimpleEmitter::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 6.5971

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 5
cts 11
cp 0.4545
rs 9.2
cc 4
eloc 8
nc 3
nop 2
crap 6.5971
1
<?php
2
/**
3
 * @package event
4
 */
5
6
7
namespace Mleko\Event;
8
9
10
class SimpleEmitter implements MutableEmitter
11
{
12
13
    /** @var array */
14
    private $listeners = [];
15
16
    /** @var EventNameExtractor */
17
    private $eventNameExtractor;
18
19
    /**
20
     * SimpleEventBus constructor.
21
     * @param EventNameExtractor $eventNameExtractor
22
     * @param Listener[]|Listener[][] $listeners
23
     */
24 7
    public function __construct(EventNameExtractor $eventNameExtractor, array $listeners = [])
25
    {
26 7
        $this->eventNameExtractor = $eventNameExtractor;
27 7
        foreach ($listeners as $eventName => $listenerDef) {
28
            if (is_array($listenerDef)) {
29
                foreach ($listenerDef as $listener) {
30
                    $this->addListener($eventName, $listener);
31
                }
32
            } else {
33
                $this->addListener($eventName, $listenerDef);
34
            }
35 7
        }
36 7
    }
37
38
    /**
39
     * @param object $event
40
     * @return void
41
     */
42 7
    public function emit($event)
43
    {
44 7
        $eventName = $this->extractEventName($event);
45 7
        $eventListeners = $this->getEventListeners($eventName);
46 7
        $eventMeta = new Meta($event, $eventName, $eventName, $this);
47 7
        foreach ($eventListeners as $listener) {
48 7
            $listener->handle($event, $eventMeta);
49 7
        }
50 7
    }
51
52
    /**
53
     * @param string $eventName
54
     * @param Listener $listener
55
     * @return Subscription
56
     */
57 7
    public function addListener($eventName, Listener $listener)
58
    {
59 7
        if (!isset($this->listeners[$eventName])) {
60 7
            $this->listeners[$eventName] = [];
61 7
        }
62 7
        $this->listeners[$eventName][] = $listener;
63 7
        return new Subscription($eventName, $listener, $this);
64
    }
65
66
    /**
67
     * @param string $eventName
68
     * @param Listener $listener
69
     * @return bool
70
     */
71 3
    public function removeListener($eventName, Listener $listener)
72
    {
73 3
        $eventListeners = $this->getEventListeners($eventName);
74 3
        foreach ($eventListeners as $key => $eventListener) {
75 3
            if ($listener === $eventListener) {
76 3
                unset($this->listeners[$eventName][$key]);
77 3
                return true;
78
            }
79 2
        }
80 2
        return false;
81
    }
82
83
    /**
84
     * @param $event
85
     * @return string
86
     */
87 7
    private function extractEventName($event)
88
    {
89 7
        return $this->eventNameExtractor->extract($event);
90
    }
91
92
    /**
93
     * @param string $eventName
94
     * @return Listener[]
95
     */
96 7
    private function getEventListeners($eventName)
97
    {
98 7
        $eventListeners = array_key_exists($eventName, $this->listeners) ? $this->listeners[$eventName] : [];
99 7
        return $eventListeners;
100
    }
101
}
102