AbstractEventDispatcher::hasListeners()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of Laravel HTTP Adapter.
5
 *
6
 * (c) Hidde Beydals <[email protected]>, Mark Redeman
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace HiddeCo\HttpAdapter\Adapters;
13
14
use Symfony\Component\EventDispatcher\Event;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface as SymfonyDispatcher;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
18
/**
19
 * This adapter provides a Laravel integration for applications
20
 * using the Symfony EventDispatcherInterface.
21
 *
22
 * It passes any request on to a Symfony Dispatcher and only
23
 * uses the Laravel Dispatcher when dispatching events.
24
 */
25
abstract class AbstractEventDispatcher implements SymfonyDispatcher
26
{
27
    /**
28
     * The Laravel Events Dispatcher.
29
     *
30
     * @var \Illuminate\Contracts\Events\Dispatcher|\Illuminate\Events\Dispatcher
31
     */
32
    protected $laravelDispatcher;
33
34
    /**
35
     * The Symfony Event Dispatcher.
36
     *
37
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
38
     */
39
    protected $symfonyDispatcher;
40
41
    /**
42
     * Dispatches an event to all registered listeners.
43
     *
44
     * @param string $eventName The name of the event to dispatch. The name of
45
     *                          the event is the name of the method that is
46
     *                          invoked on listeners.
47
     * @param Event  $event     The event to pass to the event handlers/listeners.
48
     *                          If not supplied, an empty Event instance is created.
49
     *
50
     * @return Event
51
     */
52
    public function dispatch($eventName, Event $event = null)
53
    {
54
        if ($event === null) {
55
            $event = new Event();
56
        }
57
58
        $event->setName($eventName);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\EventDispatcher\Event::setName() has been deprecated with message: since version 2.4, to be removed in 3.0. The event name is passed to the listener call.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
59
        $event->setDispatcher($this);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\EventD...\Event::setDispatcher() has been deprecated with message: since version 2.4, to be removed in 3.0. The event dispatcher is passed to the listener call.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
60
61
        $this->laravelDispatcher->fire($eventName, $event);
62
        $this->symfonyDispatcher->dispatch($eventName, $event);
63
64
        $event->setDispatcher($this);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\EventD...\Event::setDispatcher() has been deprecated with message: since version 2.4, to be removed in 3.0. The event dispatcher is passed to the listener call.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
65
66
        return $event;
67
    }
68
69
    /**
70
     * Adds an event listener that listens on the specified events.
71
     *
72
     * @param string   $eventName The event to listen on
73
     * @param callable $listener  The listener
74
     * @param int      $priority  The higher this value, the earlier an event
75
     *                            listener will be triggered in the chain.
76
     */
77
    public function addListener($eventName, $listener, $priority = 0)
78
    {
79
        $this->symfonyDispatcher->addListener($eventName, $listener, $priority);
80
    }
81
82
    /**
83
     * Adds an event subscriber.
84
     *
85
     * The subscriber is asked for all the events he is
86
     * interested in and added as a listener for these events.
87
     *
88
     * @param EventSubscriberInterface $subscriber The subscriber.
89
     */
90
    public function addSubscriber(EventSubscriberInterface $subscriber)
91
    {
92
        $this->symfonyDispatcher->addSubscriber($subscriber);
93
    }
94
95
    /**
96
     * Removes an event listener from the specified events.
97
     *
98
     * @param string   $eventName           The event to remove a listener from
99
     * @param callable $listenerToBeRemoved The listener to remove
100
     */
101
    public function removeListener($eventName, $listenerToBeRemoved)
102
    {
103
        $this->symfonyDispatcher->removeListener($eventName, $listenerToBeRemoved);
104
    }
105
106
    /**
107
     * Removes an event subscriber.
108
     *
109
     * @param EventSubscriberInterface $subscriber The subscriber
110
     */
111
    public function removeSubscriber(EventSubscriberInterface $subscriber)
112
    {
113
        $this->symfonyDispatcher->removeSubscriber($subscriber);
114
    }
115
116
    /**
117
     * Gets the listeners of a specific event or all listeners.
118
     *
119
     * @param string $eventName The name of the event
120
     *
121
     * @return array The event listeners for the specified event, or all event listeners by event name
122
     */
123
    public function getListeners($eventName = null)
124
    {
125
        return $this->symfonyDispatcher->getListeners($eventName);
126
    }
127
128
    /**
129
     * Checks whether an event has any registered listeners.
130
     *
131
     * @param string $eventName The name of the event
132
     *
133
     * @return bool true if the specified event has any listeners, false otherwise
134
     */
135
    public function hasListeners($eventName = null)
136
    {
137
        return ($this->symfonyDispatcher->hasListeners($eventName) ||
138
            $this->laravelDispatcher->hasListeners($eventName));
139
    }
140
}
141