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.

ListenerAwareTrait   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 0
dl 0
loc 160
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A attachListener() 0 16 3
A detachListener() 0 16 4
A listenerEvents() 0 16 3
A expandToHandler() 0 13 4
A expandWithPriority() 0 16 4
A expandCallable() 0 10 2
A offListenerEvent() 0 9 2
A hasPriority() 0 4 2
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\Traits;
16
17
use Phossa2\Event\Interfaces\ListenerInterface;
18
use Phossa2\Event\Interfaces\EventManagerInterface;
19
use Phossa2\Event\Interfaces\ListenerAwareInterface;
20
21
/**
22
 * ListenerAwareTrait
23
 *
24
 * Implementation of ListenerAwareInterface with scope (shared manager)
25
 * support.
26
 *
27
 * @package Phossa2\Event
28
 * @author  Hong Zhang <[email protected]>
29
 * @see     ListenerAwareInterface
30
 * @see     EventManagerInterface
31
 * @version 2.1.4
32
 * @since   2.0.0 added
33
 * @since   2.1.0 updated
34
 * @since   2.1.4 added hasPriority()
35
 */
36
trait ListenerAwareTrait
37
{
38
    /**
39
     * {@inheritDoc}
40
     */
41
    public function attachListener(ListenerInterface $listener)/*# : bool */
42
    {
43
        // get the standardized handlers of the $listener
44
        $events = $this->listenerEvents($listener);
45
46
        // add to manager's event pool
47
        foreach ($events as $handler) {
48
            /* @var $em EventManagerInterface */
49
            $em = $this;
50
            if (null !== $handler[3]) { // found scope
51
                $em = static::getShareable($handler[3]);
52
            }
53
            $em->attach($handler[0], $handler[1], $handler[2]);
54
        }
55
        return true;
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61
    public function detachListener(
62
        ListenerInterface $listener,
63
        /*# string */ $eventName = ''
64
    )/*# : bool */ {
65
        // get the standardized handlers of the $listener
66
        $events = $this->listenerEvents($listener);
67
68
        // try find the match
69
        foreach ($events as $handler) {
70
            if ('' == $eventName || $handler[0] === $eventName) {
71
                $this->offListenerEvent($handler);
72
            }
73
        }
74
75
        return true;
76
    }
77
78
    /**
79
     * standardize events definition
80
     *
81
     * @param  ListenerInterface $listener
82
     * @return array
83
     * @access protected
84
     */
85
    protected function listenerEvents(
86
        ListenerInterface $listener
87
    )/*# : array */ {
88
        $result = [];
89
        foreach ($listener->eventsListening() as $eventName => $data) {
90
            $newData = $this->expandToHandler($data);
91
            foreach ($newData as $handler) {
92
                $result[] = $this->expandWithPriority(
93
                    $listener,
94
                    $eventName,
95
                    $handler
96
                );
97
            }
98
        }
99
        return $result;
100
    }
101
102
    /**
103
     * standardize to array of 'method1' or ['method1', 20]
104
     *
105
     * @param  mixed $data
106
     * @return array
107
     * @access protected
108
     */
109
    protected function expandToHandler($data)/*# : array */
110
    {
111
        if (is_callable($data)) {
112
            $result = [$data];
113
        } elseif (is_string($data)) {
114
            $result = [$data];
115
        } elseif ($this->hasPriority($data)) {
116
            $result = [$data];
117
        } else {
118
            $result = $data;
119
        }
120
        return (array) $result;
121
    }
122
123
    /**
124
     * standardize one 'method1' or ['method1', 20, $scope]
125
     * to [eventName, callable, priority, $scopeIfAny]
126
     *
127
     * @param  ListenerInterface $listener
128
     * @param  string $eventName
129
     * @param  mixed $data
130
     * @return array
131
     * @access protected
132
     */
133
    protected function expandWithPriority(
134
        ListenerInterface $listener,
135
        /*# string */ $eventName,
136
        $data
137
    )/*# : array */ {
138
        if (is_array($data) && is_int($data[1])) {
139
            $callable = $this->expandCallable($listener, $data[0]);
140
            $priority = $data[1];
141
            $scope = isset($data[2]) ? $data[2] : null;
142
        } else {
143
            $callable = $this->expandCallable($listener, $data);
144
            $priority = 0; // default
145
            $scope = null;
146
        }
147
        return [$eventName, $callable, $priority, $scope];
148
    }
149
150
    /**
151
     * standardize 'method' or callable to callable
152
     *
153
     * @param  ListenerInterface $listener
154
     * @param  mixed $callable
155
     * @return callable
156
     * @access protected
157
     */
158
    protected function expandCallable(
159
        ListenerInterface $listener,
160
        $callable
161
    )/*# : callable */ {
162
        if (is_callable($callable)) {
163
            return $callable;
164
        } else {
165
            return [$listener, $callable];
166
        }
167
    }
168
169
    /**
170
     * off listener event [$eventName, $handler, $priority, $scope]
171
     *
172
     * @param  array $data
173
     * @access protected
174
     */
175
    protected function offListenerEvent(array $data)
176
    {
177
        /* @var $em EventManagerInterface */
178
        $em = $this;
179
        if (null !== $data[3]) { // scope found
180
            $em = static::getShareable($data[3]);
181
        }
182
        $em->detach($data[0], $data[1]);
183
    }
184
185
    /**
186
     * the second value is the priority value
187
     *
188
     * @param array $data
189
     * @access protected
190
     */
191
    protected function hasPriority(array $data)/*# : bool */
192
    {
193
        return isset($data[1]) && is_int($data[1]);
194
    }
195
}
196