1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Itkg\DelayEventBundle\EventDispatcher; |
4
|
|
|
|
5
|
|
|
use Itkg\DelayEventBundle\Event\DelayableEvent; |
6
|
|
|
use Itkg\DelayEventBundle\Event\DelayableEvents; |
7
|
|
|
use Symfony\Component\EventDispatcher\Event; |
8
|
|
|
use Itkg\DelayEventBundle\Model\Event as DelayEvent; |
9
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
10
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* class DelayEventDispatcher |
14
|
|
|
*/ |
15
|
|
|
class DelayEventDispatcher implements EventDispatcherInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $eligibleEventNames = array(); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var EventDispatcherInterface |
24
|
|
|
*/ |
25
|
|
|
private $dispatcher; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param EventDispatcherInterface $dispatcher |
29
|
|
|
* @param array $eligibleEventNames |
30
|
|
|
*/ |
31
|
|
|
public function __construct(EventDispatcherInterface $dispatcher, array $eligibleEventNames) |
32
|
|
|
{ |
33
|
|
|
$this->dispatcher = $dispatcher; |
34
|
|
|
$this->eligibleEventNames = $eligibleEventNames; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritDoc} |
39
|
|
|
*/ |
40
|
|
|
public function dispatch($eventName, Event $event = null) |
41
|
|
|
{ |
42
|
|
|
if ($this->isEventEligible($eventName, $event)) { |
|
|
|
|
43
|
|
|
$event->setOriginalName($eventName); |
|
|
|
|
44
|
|
|
// Override event name to dispatch an delayable event |
45
|
|
|
$event = new DelayableEvent($event); |
|
|
|
|
46
|
|
|
$eventName = DelayableEvents::DELAY; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->dispatcher->dispatch($eventName, $event); |
50
|
|
|
|
51
|
|
|
return $event; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritDoc} |
56
|
|
|
*/ |
57
|
|
|
public function addListener($eventName, $listener, $priority = 0) |
58
|
|
|
{ |
59
|
|
|
$this->dispatcher->addListener($eventName, $listener, $priority); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritDoc} |
64
|
|
|
*/ |
65
|
|
|
public function addSubscriber(EventSubscriberInterface $subscriber) |
66
|
|
|
{ |
67
|
|
|
$this->dispatcher->addSubscriber($subscriber); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritDoc} |
72
|
|
|
*/ |
73
|
|
|
public function removeListener($eventName, $listener) |
74
|
|
|
{ |
75
|
|
|
$this->dispatcher->removeListener($eventName, $listener); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
|
|
public function removeSubscriber(EventSubscriberInterface $subscriber) |
82
|
|
|
{ |
83
|
|
|
$this->dispatcher->removeSubscriber($subscriber); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritDoc} |
88
|
|
|
*/ |
89
|
|
|
public function getListeners($eventName = null) |
90
|
|
|
{ |
91
|
|
|
return $this->dispatcher->getListeners($eventName); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritDoc} |
96
|
|
|
*/ |
97
|
|
|
public function hasListeners($eventName = null) |
98
|
|
|
{ |
99
|
|
|
return $this->dispatcher->hasListeners($eventName); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Proxies all method calls to the original event dispatcher. |
104
|
|
|
* |
105
|
|
|
* @param string $method The method name |
106
|
|
|
* @param array $arguments The method arguments |
107
|
|
|
* |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
|
|
public function __call($method, $arguments) |
111
|
|
|
{ |
112
|
|
|
return call_user_func_array(array($this->dispatcher, $method), $arguments); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param String $eventName |
117
|
|
|
* @param Event $event |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
private function isEventEligible($eventName, $event) |
122
|
|
|
{ |
123
|
|
|
return $event instanceof DelayEvent && $event->isDelayed() && in_array($eventName, $this->eligibleEventNames); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):