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

SharedManagerTrait::onEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 8
nc 2
nop 4
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\Shared\Shareable\ShareableTrait;
18
use Phossa2\Event\Interfaces\EventManagerInterface;
19
20
/**
21
 * SharedManagerTrait
22
 *
23
 * Support for shared managers using ShareableTrait
24
 *
25
 * ```php
26
 * // one event manager instance
27
 * $event_dispatcher = new EventDispatcher();
28
 *
29
 * // global event manager, default scope is ''
30
 * $globalManager = EventDispatcher::getShareable();
31
 *
32
 * // shared manager for a scope, say 'MVC'
33
 * $MvcManager = EventDispatcher::getShareable('MVC');
34
 *
35
 * // class/interface level shared manager
36
 * $classManager = EventDispatcher::getShareable('Phossa\\Config\\Config');
37
 * ```
38
 *
39
 * @package Phossa2\Event
40
 * @author  Hong Zhang <[email protected]>
41
 * @see     SharedManagerInterface
42
 * @version 2.0.0
43
 * @since   2.0.0 added
44
 */
45
trait SharedManagerTrait
46
{
47
    use ShareableTrait;
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public static function onEvent(
53
        $scope,
54
        /*# string */ $eventName,
55
        callable $callable,
56
        /*# int */ $priority = 50
57
    ) {
58
        foreach ((array) $scope as $sc) {
59
            /* @var $em EventManagerInterface */
60
            $em = static::getShareable($sc);
61
            $em->on($eventName, $callable, $priority);
62
        }
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    public static function offEvent(
69
        $scope,
70
        /*# string */ $eventName,
71
        callable $callable = null
72
    ) {
73
        foreach ((array) $scope as $sc) {
74
            /* @var $em EventManagerInterface */
75
            $em = static::getShareable($sc);
76
            $em->off($eventName, $callable);
77
        }
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public static function onGlobalEvent(
84
        /*# string */ $eventName,
85
        callable $callable,
86
        /*# int */ $priority = 50
87
    ) {
88
        // scope '' means GLOBAL
89
        static::onEvent('', $eventName, $callable, $priority);
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95
    public static function offGlobalEvent(
96
        /*# string */ $eventName,
97
        callable $callable = null
98
    ) {
99
        // scope '' means GLOBAL
100
        static::offEvent('', $eventName, $callable);
101
    }
102
103
    /**
104
     * Override `getOwnScopes()` in ShareableTrait
105
     *
106
     * For $this's own scopes, looking for any parent class or interface in
107
     * all the available scopes.
108
     *
109
     * {@inheritDoc}
110
     */
111
    protected function getOwnScopes()/*# : array */
112
    {
113
        // result
114
        $result = [];
115
116
        // all types avaible
117
        $allTypes = $this->getAllTypes(static::getScopes());
118
119
        // loop thru own scopes
120
        foreach ($this->scopes as $scope) {
121
            // add $scope to result
122
            $result[$scope] = true;
123
124
            // scope is a type (class or interface)
125
            if ($this->isAType($scope)) {
126
                $this->matchParentType($scope, $allTypes, $result);
127
            }
128
        }
129
130
        // alway add global scope
131
        $result[''] = true;
132
133
        return array_keys($result);
134
    }
135
136
    /**
137
     * Is $type a classname or interface name ?
138
     *
139
     * @param  string $type
140
     * @return bool
141
     * @access protected
142
     */
143
    protected function isAType(/*# string */ $type)/*# : bool */
144
    {
145
        return class_exists($type) || interface_exists($type);
146
    }
147
148
    /**
149
     * Get all types (class or interface) from the given scopes
150
     *
151
     * @param  array $scopes
152
     * @return array
153
     * @access protected
154
     */
155
    protected function getAllTypes(array $scopes)/*# : array */
156
    {
157
        $result = [];
158
        foreach ($scopes as $scope) {
159
            if ($this->isAType($scope)) {
160
                $result[] = $scope;
161
            }
162
        }
163
        return $result;
164
    }
165
166
    /**
167
     * is $childType child type of one of the $typesToCheck.
168
     *
169
     * Returns the matched types
170
     *
171
     * @param  string $childType
172
     * @param  array $typesToCheck
173
     * @param  array &$result
174
     * @access protected
175
     */
176
    protected function matchParentType(
177
        /*# string */ $childType,
178
        array $typesToCheck,
179
        array &$result
180
    )/*# : bool */ {
181
        foreach ($typesToCheck as $type) {
182
            if (is_a($childType, $type, true)) {
183
                $result[$type] = true;
184
            }
185
        }
186
    }
187
}
188