JobQueue   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 45
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A push() 0 8 3
A pop() 0 9 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