GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b46d2a...48d25d )
by Hong
02:49
created

EventDispatcher   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 7

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 18
c 4
b 0
f 0
lcom 3
cbo 7
dl 0
loc 191
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A many() 0 23 2
A one() 0 7 1
A off() 0 16 3
A newEvent() 0 14 3
A getMatchedQueue() 0 13 2
A getEventNames() 0 4 1
A matchEventQueues() 0 12 3
A hashCallable() 0 4 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Event
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Event;
16
17
use Phossa2\Event\Interfaces\EventInterface;
18
use Phossa2\Event\Traits\NameGlobbingTrait;
19
use Phossa2\Event\Traits\SharedManagerTrait;
20
use Phossa2\Event\Traits\ListenerAwareTrait;
21
use Phossa2\Event\Traits\StaticManagerTrait;
22
use Phossa2\Event\Interfaces\CountableInterface;
23
use Phossa2\Event\Interfaces\SharedManagerInterface;
24
use Phossa2\Event\Interfaces\ListenerAwareInterface;
25
26
/**
27
 * EventDispatcher
28
 *
29
 * Advanced event manager with
30
 *
31
 * - event name globbing
32
 * - shared manager support
33
 * - attach/detach listener
34
 * - use as a event manager statically
35
 * - able to trigger an event with countable times
36
 *
37
 * @package Phossa2\Event
38
 * @author  Hong Zhang <[email protected]>
39
 * @version 2.0.0
40
 * @since   2.0.0 added
41
 */
42
class EventDispatcher extends EventManager implements SharedManagerInterface, ListenerAwareInterface, CountableInterface
43
{
44
    use NameGlobbingTrait,
45
        SharedManagerTrait,
46
        ListenerAwareTrait,
47
        StaticManagerTrait;
48
49
    /**
50
     * event prototype
51
     *
52
     * @var    EventInterface|null
53
     * @access protected
54
     */
55
    protected $event_proto;
56
57
    /**
58
     * callable mapping
59
     *
60
     * @var    callable[];
61
     * @access protected
62
     */
63
    protected $callable_map = [];
64
65
    /**
66
     * Create a event manager with defined scopes
67
     *
68
     * @param  string|array $scopes
69
     * @param  EventInterface $event_proto event prototype if any
70
     * @access public
71
     */
72
    public function __construct(
73
        $scopes = '',
74
        EventInterface $event_proto = null
75
    ) {
76
        // set scopes
77
        if ('' !== $scopes) {
78
            $this->scopes = (array) $scopes;
79
        }
80
81
        // set event prototype
82
        $this->event_proto = $event_proto;
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88
    public function many(
89
        /*# int */ $times,
90
        /*# string */ $eventName,
91
        callable $callable,
92
        /*# int */ $priority = 50
93
    ) {
94
        // wrap the callable
95
        $wrapper = function (EventInterface $event) use ($callable, $times) {
96
            static $cnt = 0;
97
            if ($cnt++ < $times) {
98
                call_user_func($callable, $event);
99
            }
100
        };
101
102
        // mapping callable
103
        $oid = $this->hashCallable($callable);
104
        $this->callable_map[$eventName][$oid] = $wrapper;
105
106
        // bind wrapper instead of the $callable
107
        $this->on($eventName, $wrapper, $priority);
108
109
        return $this;
110
    }
111
112
    /**
113
     * {@inheritDoc}
114
     */
115
    public function one(
116
        /*# string */ $eventName,
117
        callable $callable,
118
        /*# int */ $priority = 50
119
    ) {
120
        return $this->many(1, $eventName, $callable, $priority);
121
    }
122
123
    /**
124
     * Override `off()` in EventManager
125
     *
126
     * Added support for countable callable
127
     *
128
     * {@inheritDoc}
129
     */
130
    public function off(
131
        /*# string */ $eventName,
132
        callable $callable = null
133
    ) {
134
        if (null !== $callable) {
135
            $oid = $this->hashCallable($callable);
136
            if (isset($this->callable_map[$eventName][$oid])) {
137
                $callable = $this->callable_map[$eventName][$oid];
138
                unset($this->callable_map[$eventName][$oid]);
139
            }
140
            return parent::off($eventName, $callable);
141
        } else {
142
            unset($this->callable_map[$eventName]);
143
            return parent::off($eventName);
144
        }
145
    }
146
147
    /**
148
     * Override `newEvent()` in EventManager.
149
     *
150
     * Added event prototype support
151
     *
152
     * {@inheritDoc}
153
     */
154
    protected function newEvent(
155
        $eventName,
156
        $context,
157
        array $properties
158
    )/*# : EventInterface */ {
159
        if (is_object($eventName)) {
160
            return $eventName;
161
        } elseif ($this->event_proto) {
162
            $evt = clone $this->event_proto;
163
            return $evt($eventName, $context, $properties);
164
        } else {
165
            return new Event($eventName, $context, $properties);
166
        }
167
    }
168
169
    /**
170
     * Override `getMatchedQueue()` in EventManager.
171
     *
172
     * Support for shared manager & name globbing
173
     *
174
     * {@inheritDoc}
175
     */
176
    protected function getMatchedQueue(
177
        /*# : string */ $eventName
178
    )/*# : EventQueueInterface */ {
179
        // get all shared managers
180
        $managers = $this->getShareables();
181
182
        /* @var $mgr EventDispatcher */
183
        $nqueue = $this->newEventQueue();
184
        foreach ($managers as $mgr) {
185
            $nqueue = $nqueue->combine($mgr->matchEventQueues($eventName));
186
        }
187
        return $nqueue;
188
    }
189
190
    /**
191
     * Get all event names of $this manager
192
     *
193
     * @return array
194
     * @access protected
195
     */
196
    protected function getEventNames()/*# : array */
197
    {
198
        return array_keys($this->events);
199
    }
200
201
    /**
202
     * Get a merged queue in $this manager for the given event name
203
     *
204
     * @param  string $eventName
205
     * @return EventQueueInterface
206
     * @access protected
207
     */
208
    protected function matchEventQueues(
209
        /*# string */ $eventName
210
    )/*: EventQueueInterface */ {
211
        $nqueue = $this->newEventQueue();
212
        $names  = $this->globEventNames($eventName, $this->getEventNames());
213
        foreach ($names as $evtName) {
214
            if ($this->hasEventQueue($evtName)) {
215
                $nqueue = $nqueue->combine($this->events[$evtName]);
216
            }
217
        }
218
        return $nqueue;
219
    }
220
221
    /**
222
     * Returns a unique id for $callable with $eventName
223
     *
224
     * @param  callable $callable
225
     * @return string
226
     * @access protected
227
     */
228
    protected function hashCallable(callable $callable)/*# string */
229
    {
230
        return spl_object_hash((object) $callable);
231
    }
232
}
233