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.

QueuedEventDispatcher::addSubscriber()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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