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
Pull Request — master (#296)
by Andrew
02:32
created

RoundRobinQueue::setOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Bernard\Queue;
4
5
use Bernard\Envelope;
6
use Bernard\Queue;
7
8
/**
9
 * @package Bernard
10
 */
11
class RoundRobinQueue implements Queue
12
{
13
    /**
14
     * @var Queue[]
15
     */
16
    protected $queues;
17
18
    /**
19
     * @var bool
20
     */
21
    protected $closed;
22
23
    /**
24
     * @var \SplObjectStorage
25
     */
26
    protected $envelopes;
27
28
    /**
29
     * @var array
30
     */
31
    protected $options = [];
32
33
    /**
34
     * @param Queue[] $queues
35
     */
36
    public function __construct(array $queues)
37
    {
38
        $this->validateQueues($queues);
39
40
        $this->queues = $this->indexQueues($queues);
41
        $this->envelopes = new \SplObjectStorage();
42
        $this->closed = false;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function enqueue(Envelope $envelope, array $options = [])
49
    {
50
        $this->verifyEnvelope($envelope);
51
52
        $this->queues[$envelope->getName()]->enqueue($envelope, $options);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function dequeue()
59
    {
60
        $envelope = null;
61
        $checked = [];
62
63
        while (count($checked) < count($this->queues)) {
64
            $queue = current($this->queues);
65
            $envelope = $queue->dequeue();
66
            if (false === next($this->queues)) {
67
                reset($this->queues);
68
            }
69
            if ($envelope) {
70
                $this->envelopes->attach($envelope, $queue);
71
                break;
72
            } else {
73
                $checked[] = $queue;
74
            }
75
        }
76
77
        return $envelope;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function close()
84
    {
85
        if ($this->closed) {
86
            return;
87
        }
88
89
        foreach ($this->queues as $queue) {
90
            $queue->close();
91
        }
92
93
        $this->closed = true;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function peek($index = 0, $limit = 20)
100
    {
101
        $it = new \InfiniteIterator(new \ArrayIterator($this->queues));
102
        $envelopes = $drained = $indexes = [];
103
        foreach (array_keys($this->queues) as $name) {
104
            $indexes[$name] = 0;
105
        }
106
        $shift = 0;
107
108
        $key = key($this->queues);
109
        for ($it->rewind(); $it->key() != $key; $it->next()) {
110
            // noop
111
        }
112
113
        while (count($envelopes) < $limit && count($drained) < $it->count()) {
114
            $queue = $it->current();
115
            $name = $it->key();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $name is correct as $it->key() (which targets InfiniteIterator::key()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
116
            if ($peeked = $queue->peek($indexes[$name], 1)) {
117
                if ($shift < $index) {
118
                    $shift++;
119
                    $indexes[$name]++;
120
                } else {
121
                    $envelopes[] = array_shift($peeked);
122
                }
123
            } else {
124
                $drained[$name] = true;
125
            }
126
            $it->next();
127
        }
128
129
        return $envelopes;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function acknowledge(Envelope $envelope)
136
    {
137
        if (!$this->envelopes->contains($envelope)) {
138
            throw new \DomainException(
139
                'Unrecognized queue specified: ' . $envelope->getName()
140
            );
141
        }
142
143
        $queue = $this->envelopes[$envelope];
144
        $queue->acknowledge($envelope);
145
        $this->envelopes->detach($envelope);
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function getOptions()
152
    {
153
        return $this->options;
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function setOptions(array $options)
160
    {
161
        $this->options = $options;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function __toString()
168
    {
169
        return (string) current($this->queues);
170
    }
171
172
    /**
173
     * @return int
174
     */
175
    public function count()
176
    {
177
        return array_sum(array_map('count', $this->queues));
178
    }
179
180
    /**
181
     * @param Queue[] $queues
182
     */
183
    protected function validateQueues(array $queues)
184
    {
185
        if (empty($queues)) {
186
            throw new \DomainException('$queues cannot be empty');
187
        }
188
189
        $filtered = array_filter(
190
            $queues,
191
            function ($queue) {
192
                return !$queue instanceof Queue;
193
            }
194
        );
195
        if (!empty($filtered)) {
196
            throw new \DomainException('All elements of $queues must implement Queue');
197
        }
198
    }
199
200
    /**
201
     * @param Queue[] $queues
202
     *
203
     * @return Queue[]
204
     */
205
    protected function indexQueues(array $queues)
206
    {
207
        return array_combine(
208
            array_map(
209
                function ($queue) {
210
                    return (string) $queue;
211
                },
212
                $queues
213
            ),
214
            $queues
215
        );
216
    }
217
218
    /**
219
     * @param Envelope $envelope
220
     */
221
    protected function verifyEnvelope(Envelope $envelope)
222
    {
223
        $queue = $envelope->getName();
224
        if (isset($this->queues[$queue])) {
225
            return;
226
        }
227
        throw new \DomainException('Unrecognized queue specified: ' . $queue);
228
    }
229
}
230