JobQueue::push()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace cvweiss\projectbase;
4
5
class JobQueue
6
{
7
    private $redis = null;
8
    private $queueName = null;
9
    private $numQueues = 5;
10
    private $blockTime;
11
12
    private $q0 = null;
13
    private $q1 = null;
14
    private $q2 = null;
15
    private $q3 = null;
16
    private $q4 = null;
17
18
    public function __construct(\Redis $redis, string $queueName = 'queue', int $blockTime = 1)
19
    {
20
        $this->redis = $redis;
21
        $this->queueName = $queueName;
22
        $this->blockTime = $blockTime;
23
24
        $this->q0 = $queueName . "0";
25
        $this->q1 = $queueName . "1";
26
        $this->q2 = $queueName . "2";
27
        $this->q3 = $queueName . "3";
28
        $this->q4 = $queueName . "4";
29
    }
30
31
    public function push($value, int $priority = 0)
32
    {
33
        if ($priority < 0 || $priority >= $this->numQueues) {
34
            throw new \InvalidArgumentException("Invalid priority");
35
        }
36
37
        $this->redis->rPush($this->queueName . $priority, serialize($value));
38
    }
39
40
    public function pop()
41
    {
42
        $array = $this->redis->blPop($this->q0, $this->q1, $this->q2, $this->q3, $this->q4, $this->blockTime);
43
        if (sizeof($array) == 0) {
44
            return null;
45
        }
46
47
        return unserialize($array[1]);
48
    }
49
}
50