EventManager::removeEventSubscriber()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Doctrine\Common;
4
5
use function spl_object_hash;
6
7
/**
8
 * The EventManager is the central point of Doctrine's event listener system.
9
 * Listeners are registered on the manager and events are dispatched through the
10
 * manager.
11
 */
12
class EventManager
13
{
14
    /**
15
     * Map of registered listeners.
16
     * <event> => <listeners>
17
     *
18
     * @var object[][]
19
     */
20
    private $_listeners = [];
21
22
    /**
23
     * Dispatches an event to all registered listeners.
24
     *
25
     * @param string         $eventName The name of the event to dispatch. The name of the event is
26
     *                                  the name of the method that is invoked on listeners.
27
     * @param EventArgs|null $eventArgs The event arguments to pass to the event handlers/listeners.
28
     *                                  If not supplied, the single empty EventArgs instance is used.
29
     *
30
     * @return void
31
     */
32 2
    public function dispatchEvent($eventName, ?EventArgs $eventArgs = null)
33
    {
34 2
        if (! isset($this->_listeners[$eventName])) {
35 1
            return;
36
        }
37
38 1
        $eventArgs = $eventArgs ?? EventArgs::getEmptyInstance();
39
40 1
        foreach ($this->_listeners[$eventName] as $listener) {
41 1
            $listener->$eventName($eventArgs);
42
        }
43 1
    }
44
45
    /**
46
     * Gets the listeners of a specific event or all listeners.
47
     *
48
     * @param string|null $event The name of the event.
49
     *
50
     * @return object[]|object[][] The event listeners for the specified event, or all event listeners.
51
     */
52 2
    public function getListeners($event = null)
53
    {
54 2
        return $event ? $this->_listeners[$event] : $this->_listeners;
55
    }
56
57
    /**
58
     * Checks whether an event has any registered listeners.
59
     *
60
     * @param string $event
61
     *
62
     * @return bool TRUE if the specified event has any listeners, FALSE otherwise.
63
     */
64 5
    public function hasListeners($event)
65
    {
66 5
        return ! empty($this->_listeners[$event]);
67
    }
68
69
    /**
70
     * Adds an event listener that listens on the specified events.
71
     *
72
     * @param string|string[] $events   The event(s) to listen on.
73
     * @param object          $listener The listener object.
74
     *
75
     * @return void
76
     */
77 5
    public function addEventListener($events, $listener)
78
    {
79
        // Picks the hash code related to that listener
80 5
        $hash = spl_object_hash($listener);
81
82 5
        foreach ((array) $events as $event) {
83
            // Overrides listener if a previous one was associated already
84
            // Prevents duplicate listeners on same event (same instance only)
85 5
            $this->_listeners[$event][$hash] = $listener;
86
        }
87 5
    }
88
89
    /**
90
     * Removes an event listener from the specified events.
91
     *
92
     * @param string|string[] $events
93
     * @param object          $listener
94
     *
95
     * @return void
96
     */
97 2
    public function removeEventListener($events, $listener)
98
    {
99
        // Picks the hash code related to that listener
100 2
        $hash = spl_object_hash($listener);
101
102 2
        foreach ((array) $events as $event) {
103 2
            unset($this->_listeners[$event][$hash]);
104
        }
105 2
    }
106
107
    /**
108
     * Adds an EventSubscriber. The subscriber is asked for all the events it is
109
     * interested in and added as a listener for these events.
110
     *
111
     * @param EventSubscriber $subscriber The subscriber.
112
     *
113
     * @return void
114
     */
115 2
    public function addEventSubscriber(EventSubscriber $subscriber)
116
    {
117 2
        $this->addEventListener($subscriber->getSubscribedEvents(), $subscriber);
118 2
    }
119
120
    /**
121
     * Removes an EventSubscriber. The subscriber is asked for all the events it is
122
     * interested in and removed as a listener for these events.
123
     *
124
     * @param EventSubscriber $subscriber The subscriber.
125
     *
126
     * @return void
127
     */
128 1
    public function removeEventSubscriber(EventSubscriber $subscriber)
129
    {
130 1
        $this->removeEventListener($subscriber->getSubscribedEvents(), $subscriber);
131 1
    }
132
}
133