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 ( 30f161...93b463 )
by Simon
04:54
created

QueuedEventDispatcher::addSubscriber()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 4
nop 1
dl 0
loc 14
rs 8.2222
c 0
b 0
f 0
1
<?php declare (strict_types=1);
2
3
namespace SmoothPhp\LaravelAdapter\QueuedEventDispatcher;
4
5
use Illuminate\Contracts\Config\Repository;
6
use Illuminate\Contracts\Queue\Queue;
7
use SmoothPhp\Contracts\EventDispatcher\EventDispatcher;
8
use SmoothPhp\Contracts\EventDispatcher\Subscriber;
9
use SmoothPhp\Contracts\Serialization\Serializer;
10
11
/**
12
 * Class QueuedEventDispatcher
13
 * @package SmoothPhp\LaravelAdapter\QueuedEventDispatcher
14
 * @author Simon Bennett <[email protected]>
15
 */
16
final class QueuedEventDispatcher implements EventDispatcher
17
{
18
    /**
19
     * @var array
20
     */
21
    private $listeners = [];
22
23
    /**
24
     * @var array
25
     */
26
    private $sorted = [];
27
    /** @var Queue */
28
    private $queue;
29
    /** @var Serializer */
30
    private $serializer;
31
    /** @var Repository */
32
    private $config;
33
34
    /**
35
     * QueuedEventDispatcher constructor.
36
     * @param Queue $queue
37
     * @param Serializer $serializer
38
     * @param Repository $config
39
     */
40
    public function __construct(Queue $queue, Serializer $serializer, Repository $config)
41
    {
42
        $this->queue = $queue;
43
        $this->serializer = $serializer;
44
        $this->config = $config;
45
    }
46
47
    /**
48
     * @param string $eventName
49
     * @param array $arguments
50
     * @param bool $runProjectionsOnly
51
     */
52
    public function dispatch($eventName, array $arguments, $runProjectionsOnly = false)
53
    {
54
        if (!isset($this->listeners[$eventName])) {
55
            return;
56
        }
57
        $listeners = $this->getListenersInOrder($eventName);
58
59
        if (count($listeners) === 1) {
60
            call_user_func_array($listeners[0], $arguments);
61
62
            return;
63
        }
64
65
        foreach ($this->getListenersInOrder($eventName) as $listener) {
66
            $this->queue->push(
67
                QueuedEventHandler::class,
68
                [
69
                    'listener_class' => get_class($listener[0]),
70
                    'listener_method'         => $listener[1],
71
                    'event'      => $this->serializer->serialize($arguments[0])
72
                ],
73
                $this->config->get('cqrses.queue_name_handler', 'default')
74
            );
75
        }
76
    }
77
78
    /**
79
     * @param string $eventName
80
     * @param callable $callable
81
     * @param int $priority
82
     */
83
    public function addListener($eventName, callable $callable, $priority = 0)
84
    {
85
        $dotEventName = str_replace('\\', '.', $eventName);
86
        $this->listeners[$dotEventName][$priority][] = $callable;
87
        unset($this->sorted[$dotEventName]);
88
89
    }
90
91
92
    /**
93
     * @param Subscriber $subscriber
94
     * @return void
95
     */
96
    public function addSubscriber(Subscriber $subscriber)
97
    {
98
        foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
99
            if (is_string($params)) {
100
                $this->addListener($eventName, [$subscriber, $params]);
101
            } elseif (is_string($params[0])) {
102
                $this->addListener($eventName, [$subscriber, $params[0]], isset($params[1]) ? $params[1] : 0);
103
            } else {
104
                foreach ($params as $listener) {
105
                    $this->addListener($eventName, [$subscriber, $listener[0]], isset($listener[1]) ? $listener[1] : 0);
106
                }
107
            }
108
        }
109
    }
110
111
    /**
112
     * @param $eventName
113
     * @return array
114
     */
115
    protected function getListenersInOrder($eventName)
116
    {
117
        if (!isset($this->listeners[$eventName])) {
118
            return [];
119
        }
120
        if (!isset($this->sorted[$eventName])) {
121
            $this->sortListeners($eventName);
122
        }
123
124
        return $this->sorted[$eventName];
125
    }
126
127
    /**
128
     * Sorts the internal list of listeners for the given event by priority.
129
     *
130
     * @param string $eventName The name of the event.
131
     */
132
    private function sortListeners($eventName)
133
    {
134
        $this->sorted[$eventName] = [];
135
136
        krsort($this->listeners[$eventName]);
137
        $this->sorted[$eventName] = call_user_func_array('array_merge', $this->listeners[$eventName]);
138
    }
139
}
140