Random   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 40
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A getJobFromQueues() 0 18 3
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