Completed
Push — master ( f3605b...a98af8 )
by Daniel
02:17
created

SimpleEmitter::emit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 9.6666
cc 2
eloc 6
nc 2
nop 1
crap 2
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[][] $listeners
23
     */
24 8
    public function __construct(EventNameExtractor $eventNameExtractor, array $listeners = [])
25
    {
26 8
        $this->eventNameExtractor = $eventNameExtractor;
27 8
        foreach ($listeners as $eventName => $eventListeners) {
28 1
            foreach ($eventListeners as $listener) {
29 1
                $this->addListener($eventName, $listener);
30 1
            }
31 8
        }
32 8
    }
33
34
    /**
35
     * @param object $event
36
     * @return void
37
     */
38 7
    public function emit($event)
39
    {
40 7
        $eventName = $this->extractEventName($event);
41 7
        $eventListeners = $this->getEventListeners($eventName);
42 7
        $eventMeta = new Meta($event, $eventName, $eventName, $this);
43 7
        foreach ($eventListeners as $listener) {
44 7
            $listener->handle($event, $eventMeta);
45 7
        }
46 7
    }
47
48
    /**
49
     * @param string $eventName
50
     * @param Listener $listener
51
     * @return Subscription
52
     */
53 8
    public function addListener($eventName, Listener $listener)
54
    {
55 8
        if (!isset($this->listeners[$eventName])) {
56 8
            $this->listeners[$eventName] = [];
57 8
        }
58 8
        $this->listeners[$eventName][] = $listener;
59 8
        return new Subscription($eventName, $listener, $this);
60
    }
61
62
    /**
63
     * @param string $eventName
64
     * @param Listener $listener
65
     * @return bool
66
     */
67 4
    public function removeListener($eventName, Listener $listener)
68
    {
69 4
        $eventListeners = $this->getEventListeners($eventName);
70 4
        foreach ($eventListeners as $key => $eventListener) {
71 4
            if ($listener === $eventListener) {
72 4
                unset($this->listeners[$eventName][$key]);
73 4
                return true;
74
            }
75 2
        }
76 2
        return false;
77
    }
78
79
    /**
80
     * @param $event
81
     * @return string
82
     */
83 7
    private function extractEventName($event)
84
    {
85 7
        return $this->eventNameExtractor->extract($event);
86
    }
87
88
    /**
89
     * @param string $eventName
90
     * @return Listener[]
91
     */
92 8
    private function getEventListeners($eventName)
93
    {
94 8
        $eventListeners = array_key_exists($eventName, $this->listeners) ? $this->listeners[$eventName] : [];
95 8
        return $eventListeners;
96
    }
97
}
98