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 ( 090a25...8af299 )
by Hong
02:29
created

ListenerAwareTrait::offListenerEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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