RedisQueue::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Phive Queue package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phive\Queue;
13
14
/**
15
 * RedisQueue requires redis server 2.6 or higher.
16
 */
17
class RedisQueue implements Queue
18
{
19
    const SCRIPT_PUSH = <<<'LUA'
20
        local id = redis.call('INCR', KEYS[2])
21
        return redis.call('ZADD', KEYS[1], ARGV[2], id..':'..ARGV[1])
22
LUA;
23
24
    const SCRIPT_POP = <<<'LUA'
25
        local items = redis.call('ZRANGEBYSCORE', KEYS[1], '-inf', ARGV[1], 'LIMIT', 0, 1)
26
        if 0 == #items then return -1 end
27
        redis.call('ZREM', KEYS[1], items[1])
28
        return string.sub(items[1], string.find(items[1], ':') + 1)
29
LUA;
30
31
    /**
32
     * @var \Redis
33
     */
34
    private $redis;
35
36 27
    public function __construct(\Redis $redis)
37
    {
38 27
        $this->redis = $redis;
39 27
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 23
    public function push($item, $eta = null)
45
    {
46 23
        $eta = QueueUtils::normalizeEta($eta);
47
48 23
        if (\Redis::SERIALIZER_NONE !== $this->redis->getOption(\Redis::OPT_SERIALIZER)) {
49 9
            $item = $this->redis->_serialize($item);
0 ignored issues
show
Bug introduced by
The method _serialize() does not exist on Redis. Did you maybe mean _unserialize()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
50 9
        }
51
52 23
        $result = $this->redis->evaluate(self::SCRIPT_PUSH, ['items', 'seq', $item, $eta], 2);
53 21
        $this->assertResult($result);
54 20
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 20
    public function pop()
60
    {
61 20
        $result = $this->redis->evaluate(self::SCRIPT_POP, ['items', time()], 1);
62 20
        $this->assertResult($result);
63
64 19
        if (-1 === $result) {
65 2
            throw new NoItemAvailableException($this);
66
        }
67
68 19
        if (\Redis::SERIALIZER_NONE !== $this->redis->getOption(\Redis::OPT_SERIALIZER)) {
69 8
            return $this->redis->_unserialize($result);
70
        }
71
72 11
        return $result;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 2
    public function count()
79
    {
80 2
        $result = $this->redis->zCard('items');
81 2
        $this->assertResult($result);
82
83 1
        return $result;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 2
    public function clear()
90
    {
91 2
        $result = $this->redis->del(['items', 'seq']);
92 2
        $this->assertResult($result);
93 1
    }
94
95
    /**
96
     * @param mixed $result
97
     *
98
     * @throws QueueException
99
     */
100 24
    protected function assertResult($result)
101
    {
102 24
        if (false === $result) {
103 4
            throw new QueueException($this, $this->redis->getLastError());
104
        }
105 20
    }
106
}
107