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 ( e2957c...f178a6 )
by Simon
02:06
created

ProjectEnabledDispatcher::dispatch()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 6
nc 4
nop 3
crap 4
1
<?php
2
namespace SmoothPhp\EventDispatcher;
3
4
use SmoothPhp\Contracts\EventDispatcher\EventDispatcher;
5
use SmoothPhp\Contracts\EventDispatcher\Projection;
6
use SmoothPhp\Contracts\EventDispatcher\Subscriber;
7
8
/**
9
 * Class ProjectEnabledDispatcher
10
 * @package SmoothPhp\EventDispatcher
11
 * @author Simon Bennett <[email protected]>
12
 */
13
final class ProjectEnabledDispatcher implements EventDispatcher
14
{
15
    /**
16
     * @var array
17
     */
18
    private $listeners = [];
19
20
    /**
21
     * @var array
22
     */
23
    private $sorted = [];
24
25
    /**
26
     * @param string $eventName
27
     * @param array $arguments
28
     * @param bool $runProjectionsOnly
29
     */
30 24
    public function dispatch($eventName, array $arguments, $runProjectionsOnly = false)
31
    {
32 24
        if (!isset($this->listeners[$eventName])) {
33 3
            return;
34
        }
35 21
        foreach ($this->getListenersInOrder($eventName) as $listener) {
36 21
            if ($this->listenerCanRun($runProjectionsOnly, $listener)) {
37 21
                call_user_func_array($listener, $arguments);
38 21
            }
39 21
        }
40 21
    }
41
42
    /**
43
     * @param string $eventName
44
     * @param callable $callable
45
     * @param int $priority
46
     */
47 24
    public function addListener($eventName, callable $callable, $priority = 0)
48
    {
49 24
        $dotEventName = str_replace('\\', '.', $eventName);
50 24
        $this->listeners[$dotEventName][$priority][] = $callable;
51 24
        unset($this->sorted[$dotEventName]);
52
53 24
    }
54
55
56
    /**
57
     * @param Subscriber $subscriber
58
     * @return void
59
     */
60 6
    public function addSubscriber(Subscriber $subscriber)
61
    {
62 6
        foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
63 6
            if (is_string($params)) {
64 3
                $this->addListener($eventName, array($subscriber, $params));
65 6
            } elseif (is_string($params[0])) {
66 6
                $this->addListener($eventName, array($subscriber, $params[0]), isset($params[1]) ? $params[1] : 0);
67 6
            } else {
68 3
                foreach ($params as $listener) {
69 3
                    $this->addListener($eventName, array($subscriber, $listener[0]), isset($listener[1]) ? $listener[1] : 0);
70 3
                }
71
            }
72 6
        }
73 6
    }
74
75
    /**
76
     * @param $runProjectionsOnly
77
     * @param $listener
78
     * @return bool
79
     */
80 21
    protected function listenerCanRun($runProjectionsOnly, $listener)
81
    {
82 21
        return !$runProjectionsOnly || (is_array($listener) && $listener[0] instanceof Projection);
83
    }
84
85
    /**
86
     * @param $eventName
87
     * @return array
88
     */
89 21
    protected function getListenersInOrder($eventName)
90
    {
91 21
        if (!isset($this->listeners[$eventName])) {
92
            return [];
93
        }
94 21
        if (!isset($this->sorted[$eventName])) {
95 21
            $this->sortListeners($eventName);
96 21
        }
97
98 21
        return $this->sorted[$eventName];
99
    }
100
101
    /**
102
     * Sorts the internal list of listeners for the given event by priority.
103
     *
104
     * @param string $eventName The name of the event.
105
     */
106 21
    private function sortListeners($eventName)
107
    {
108 21
        $this->sorted[$eventName] = array();
109
110 21
        krsort($this->listeners[$eventName]);
111 21
        $this->sorted[$eventName] = call_user_func_array('array_merge', $this->listeners[$eventName]);
112
    }
113
}