DelayEventDispatcher   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 48.57%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 118
ccs 17
cts 35
cp 0.4857
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A dispatch() 0 20 3
A addListener() 0 4 1
A addSubscriber() 0 4 1
A removeListener() 0 4 1
A removeSubscriber() 0 4 1
A getListeners() 0 4 1
A hasListeners() 0 4 1
A __call() 0 4 1
A isEventEligible() 0 4 2
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 1
    public function __construct(EventDispatcherInterface $dispatcher, array $eligibleEventNames)
32
    {
33 1
        $this->dispatcher = $dispatcher;
34 1
        $this->eligibleEventNames = $eligibleEventNames;
35 1
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40 1
    public function dispatch($eventName, Event $event = null)
41
    {
42 1
        if (!$event instanceof DelayEvent) {
43 1
            $this->dispatcher->dispatch($eventName, $event);
44
45 1
            return $event;
46
        }
47
48 1
        if ($this->isEventEligible($eventName, $event)) {
49 1
            $event->setOriginalName($eventName);
50
51
            // Override event name to dispatch a delayed event
52 1
            $event = new DelayableEvent($event);
53 1
            $eventName = DelayableEvents::DELAY;
54 1
        }
55
56 1
        $this->dispatcher->dispatch($eventName, $event);
57
58 1
        return $event;
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64
    public function addListener($eventName, $listener, $priority = 0)
65
    {
66
        $this->dispatcher->addListener($eventName, $listener, $priority);
67
    }
68
69
    /**
70
     * {@inheritDoc}
71
     */
72
    public function addSubscriber(EventSubscriberInterface $subscriber)
73
    {
74
        $this->dispatcher->addSubscriber($subscriber);
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80
    public function removeListener($eventName, $listener)
81
    {
82
        $this->dispatcher->removeListener($eventName, $listener);
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88
    public function removeSubscriber(EventSubscriberInterface $subscriber)
89
    {
90
        $this->dispatcher->removeSubscriber($subscriber);
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     */
96
    public function getListeners($eventName = null)
97
    {
98
        return $this->dispatcher->getListeners($eventName);
99
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104
    public function hasListeners($eventName = null)
105
    {
106
        return $this->dispatcher->hasListeners($eventName);
107
    }
108
109
    /**
110
     * Proxies all method calls to the original event dispatcher.
111
     *
112
     * @param string $method    The method name
113
     * @param array  $arguments The method arguments
114
     *
115
     * @return mixed
116
     */
117
    public function __call($method, $arguments)
118
    {
119
        return call_user_func_array(array($this->dispatcher, $method), $arguments);
120
    }
121
122
    /**
123
     * @param String     $eventName
124
     * @param DelayEvent $event
125
     *
126
     * @return bool
127
     */
128 1
    private function isEventEligible($eventName, DelayEvent $event)
129
    {
130 1
        return $event->isDelayed() && in_array($eventName, $this->eligibleEventNames);
131
    }
132
}
133