Random::getJobFromQueues()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 18
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 0
crap 12
1
<?php
2
3
/**
4
 * @author    Flipbox Factory
5
 * @copyright Copyright (c) 2017, Flipbox Digital
6
 * @link      https://github.com/flipbox/queue/releases/latest
7
 * @license   https://github.com/flipbox/queue/blob/master/LICENSE
8
 */
9
10
namespace flipbox\queue\strategies;
11
12
/**
13
 * @author Flipbox Factory <[email protected]>
14
 * @since 1.0.0
15
 */
16
class Random extends AbstractStrategy
17
{
18
    /**
19
     * @return void
20
     */
21
    public function init()
22
    {
23
        parent::init();
24
        srand();
25
    }
26
27
    /**
28
     * The number of attempt before returning false.
29
     *
30
     * @var int
31
     */
32
    public $maxAttempt = 5;
33
34
    /**
35
     * @inheritdoc
36
     */
37
    protected function getJobFromQueues()
38
    {
39
        $attempt = 0;
40
        $queues = $this->queue->getQueues();
41
        $count = count($queues);
42
        $keys = array_keys($queues);
43
        while ($attempt < $this->maxAttempt) {
44
            $index = rand(0, $count - 1);
45
            $key = $keys[$index];
46
            $queue = $this->queue->getQueue($key);
47
            $job = $queue->fetch();
48
            if ($job !== false) {
49
                return [$job, $key];
50
            }
51
            $attempt++;
52
        }
53
        return false;
54
    }
55
}
56