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.

ProjectEnabledDispatcher   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 89
ccs 30
cts 31
cp 0.9677
rs 10
c 0
b 0
f 0

5 Methods

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