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 (#388)
by Ben
03:39
created

RoundRobinQueue::dequeue()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4.128

Importance

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

This check looks for for loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
105
            // noop
106
        }
107
108 1
        while (count($envelopes) < $limit && count($drained) < $it->count()) {
109 1
            $queue = $it->current();
110 1
            $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...
111 1
            if ($peeked = $queue->peek($indexes[$name], 1)) {
112 1
                if ($shift < $index) {
113 1
                    ++$shift;
114 1
                    ++$indexes[$name];
115 1
                } else {
116 1
                    $envelopes[] = array_shift($peeked);
117
                }
118 1
            } else {
119 1
                $drained[$name] = true;
120
            }
121 1
            $it->next();
122 1
        }
123
124 1
        return $envelopes;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 1
    public function acknowledge(Envelope $envelope)
131
    {
132 1
        if (!$this->envelopes->contains($envelope)) {
133
            throw new \DomainException(
134
                'Unrecognized queue specified: '.$envelope->getName()
135
            );
136
        }
137
138 1
        $queue = $this->envelopes[$envelope];
139 1
        $queue->acknowledge($envelope);
140 1
        $this->envelopes->detach($envelope);
141 1
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146 1
    public function __toString()
147
    {
148 1
        return (string) current($this->queues);
149
    }
150
151
    /**
152
     * @return int
153
     */
154 1
    public function count()
155
    {
156 1
        return array_sum(array_map('count', $this->queues));
157
    }
158
159
    /**
160
     * @param Queue[] $queues
161
     */
162 11
    protected function validateQueues(array $queues)
163
    {
164 11
        if (empty($queues)) {
165
            throw new \DomainException('$queues cannot be empty');
166
        }
167
168 11
        $filtered = array_filter(
169 11
            $queues,
170
            function ($queue) {
171 11
                return !$queue instanceof Queue;
172
            }
173 11
        );
174 11
        if (!empty($filtered)) {
175
            throw new \DomainException('All elements of $queues must implement Queue');
176
        }
177 11
    }
178
179
    /**
180
     * @param Queue[] $queues
181
     *
182
     * @return Queue[]
183
     */
184 11
    protected function indexQueues(array $queues)
185
    {
186 11
        return array_combine(
187 11
            array_map(
188 11
                function ($queue) {
189 11
                    return (string) $queue;
190 11
                },
191
                $queues
192 11
            ),
193
            $queues
194 11
        );
195
    }
196
197
    /**
198
     * @param Envelope $envelope
199
     */
200 8
    protected function verifyEnvelope(Envelope $envelope)
201
    {
202 8
        $queue = $envelope->getName();
203 8
        if (isset($this->queues[$queue])) {
204 6
            return;
205
        }
206 2
        throw new \DomainException('Unrecognized queue specified: '.$queue);
207
    }
208
}
209