Completed
Pull Request — master (#19)
by Pascal
03:03
created

DelayEventDispatcher::dispatch()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 18
ccs 0
cts 13
cp 0
rs 9.4285
cc 3
eloc 9
nc 3
nop 2
crap 12
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 (!$event instanceof DelayEvent) {
43
            return $event;
44
        }
45
46
        if ($this->isEventEligible($eventName, $event)) {
47
            $event->setOriginalName($eventName);
48
49
            // Override event name to dispatch a delayed event
50
            $event = new DelayableEvent($event);
51
            $eventName = DelayableEvents::DELAY;
52
        }
53
54
        $this->dispatcher->dispatch($eventName, $event);
55
56
        return $event;
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    public function addListener($eventName, $listener, $priority = 0)
63
    {
64
        $this->dispatcher->addListener($eventName, $listener, $priority);
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70
    public function addSubscriber(EventSubscriberInterface $subscriber)
71
    {
72
        $this->dispatcher->addSubscriber($subscriber);
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    public function removeListener($eventName, $listener)
79
    {
80
        $this->dispatcher->removeListener($eventName, $listener);
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86
    public function removeSubscriber(EventSubscriberInterface $subscriber)
87
    {
88
        $this->dispatcher->removeSubscriber($subscriber);
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94
    public function getListeners($eventName = null)
95
    {
96
        return $this->dispatcher->getListeners($eventName);
97
    }
98
99
    /**
100
     * {@inheritDoc}
101
     */
102
    public function hasListeners($eventName = null)
103
    {
104
        return $this->dispatcher->hasListeners($eventName);
105
    }
106
107
    /**
108
     * Proxies all method calls to the original event dispatcher.
109
     *
110
     * @param string $method    The method name
111
     * @param array  $arguments The method arguments
112
     *
113
     * @return mixed
114
     */
115
    public function __call($method, $arguments)
116
    {
117
        return call_user_func_array(array($this->dispatcher, $method), $arguments);
118
    }
119
120
    /**
121
     * @param String     $eventName
122
     * @param DelayEvent $event
123
     *
124
     * @return bool
125
     */
126
    private function isEventEligible($eventName, DelayEvent $event)
127
    {
128
        return $event->isDelayed() && in_array($eventName, $this->eligibleEventNames);
129
    }
130
}
131