EventDispatcherAdapter::hasListeners()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
/**
3
 * @package php-tmdb\laravel
4
 * @author Mark Redeman <[email protected]>
5
 * @copyright (c) 2014, Mark Redeman
6
 */
7
namespace Tmdb\Laravel\Adapters;
8
9
use Symfony\Component\EventDispatcher\Event;
10
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface as SymfonyDispatcher;
12
13
/**
14
 * This adapter provides a Laravel integration for applications
15
 * using the Symfony EventDispatcherInterface
16
 * It passes any request on to a Symfony Dispatcher and only
17
 * uses the Laravel Dispatcher when dispatching events
18
 */
19
abstract class EventDispatcherAdapter implements SymfonyDispatcher
20
{
21
22
    /**
23
     * The Laravel Events Dispatcher
24
     * @var \Illuminate\Contracts\Events\Dispatcher or \Illuminate\Events\Dispatcher
25
     */
26
    protected $laravelDispatcher;
27
28
    /**
29
     * The Symfony Event Dispatcher
30
     * @var  \Symfony\Component\EventDispatcher\EventDispatcherInterface
31
     */
32
    protected $symfonyDispatcher;
33
34
    /**
35
     * Dispatches an event to all registered listeners.
36
     *
37
     * @param string $eventName The name of the event to dispatch. The name of
38
     *                          the event is the name of the method that is
39
     *                          invoked on listeners.
40
     * @param Event  $event     The event to pass to the event handlers/listeners.
41
     *                          If not supplied, an empty Event instance is created.
42
     *
43
     * @return Event
44
     *
45
     * @api
46
     */
47
    public function dispatch($eventName, Event $event = null)
48
    {
49
        $this->laravelDispatcher->fire($eventName, $event);
50
        return $this->symfonyDispatcher->dispatch($eventName, $event);
51
    }
52
53
    /**
54
     * Adds an event listener that listens on the specified events.
55
     *
56
     * @param string   $eventName The event to listen on
57
     * @param callable $listener  The listener
58
     * @param int      $priority  The higher this value, the earlier an event
59
     *                            listener will be triggered in the chain (defaults to 0)
60
     *
61
     * @api
62
     */
63
    public function addListener($eventName, $listener, $priority = 0)
64
    {
65
        $this->symfonyDispatcher->addListener($eventName, $listener, $priority);
66
    }
67
68
    /**
69
     * Adds an event subscriber.
70
     *
71
     * The subscriber is asked for all the events he is
72
     * interested in and added as a listener for these events.
73
     *
74
     * @param EventSubscriberInterface $subscriber The subscriber.
75
     *
76
     * @api
77
     */
78
    public function addSubscriber(EventSubscriberInterface $subscriber)
79
    {
80
        $this->symfonyDispatcher->addSubscriber($subscriber);
81
    }
82
83
    /**
84
     * Removes an event listener from the specified events.
85
     *
86
     * @param string   $eventName The event to remove a listener from
87
     * @param callable $listenerToBeRemoved The listener to remove
88
     */
89
    public function removeListener($eventName, $listenerToBeRemoved)
90
    {
91
        $this->symfonyDispatcher->removeListener($eventName, $listenerToBeRemoved);
92
    }
93
94
    /**
95
     * Removes an event subscriber.
96
     *
97
     * @param EventSubscriberInterface $subscriber The subscriber
98
     */
99
    public function removeSubscriber(EventSubscriberInterface $subscriber)
100
    {
101
        $this->symfonyDispatcher->removeSubscriber($subscriber);
102
    }
103
104
    /**
105
     * Gets the listeners of a specific event or all listeners.
106
     *
107
     * @param string $eventName The name of the event
108
     *
109
     * @return array The event listeners for the specified event, or all event listeners by event name
110
     */
111
    public function getListeners($eventName = null)
112
    {
113
        return $this->symfonyDispatcher->getListeners($eventName);
114
    }
115
116
    /**
117
     * Checks whether an event has any registered listeners.
118
     *
119
     * @param string $eventName The name of the event
120
     *
121
     * @return bool true if the specified event has any listeners, false otherwise
122
     */
123
    public function hasListeners($eventName = null)
124
    {
125
        return ($this->symfonyDispatcher->hasListeners($eventName) ||
126
            $this->laravelDispatcher->hasListeners($eventName));
127
    }
128
129
    /**
130
     * Gets the listener priority for a specific event.
131
     *
132
     * Returns null if the event or the listener does not exist.
133
     *
134
     * @param string   $eventName The name of the event
135
     * @param callable $listener  The listener
136
     *
137
     * @return int|null The event listener priority
138
     */
139
    public function getListenerPriority($eventName, $listener)
140
    {
141
        return $this->symfonyDispatcher->getListenerPriority($eventName, $listener);
142
    }
143
}
144