TarantoolQueue::push()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 13
cts 13
cp 1
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 2
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
class TarantoolQueue implements Queue
15
{
16
    /**
17
     * @var \Tarantool
18
     */
19
    private $tarantool;
20
21
    /**
22
     * @var string
23
     */
24
    private $space;
25
26
    /**
27
     * @var string
28
     */
29
    private $tubeName;
30
31 16
    public function __construct(\Tarantool $tarantool, $tubeName, $space = null)
32
    {
33 16
        $this->tarantool = $tarantool;
34 16
        $this->space = null === $space ? '0' : (string) $space;
35 16
        $this->tubeName = $tubeName;
36 16
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 15
    public function push($item, $eta = null)
42
    {
43
        // see https://github.com/tarantool/tarantool/issues/336
44 15
        $item .= '         ';
45 13
        $eta = QueueUtils::calculateDelay($eta);
46
47 13
        $this->tarantool->call('queue.put', [
48 13
            $this->space,
49 13
            $this->tubeName,
50 13
            (string) $eta,
51 13
            '0',
52 13
            '0',
53 13
            '0',
54 13
            $item,
55 13
        ]);
56 13
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 12
    public function pop()
62
    {
63 12
        $result = $this->tarantool->call('queue.take', [
64 12
            $this->space,
65 12
            $this->tubeName,
66 12
            '0.00000001',
67 12
        ]);
68
69 12
        if (0 === $result['count']) {
70 2
            throw new NoItemAvailableException($this);
71
        }
72
73 12
        $tuple = $result['tuples_list'][0];
74
75 12
        $this->tarantool->call('queue.delete', [
76 12
            $this->space,
77 12
            $tuple[0],
78 12
        ]);
79
80 12
        return substr($tuple[3], 0, -9);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 1
    public function count()
87
    {
88 1
        $result = $this->tarantool->call('queue.statistics', [
89 1
            $this->space,
90 1
            $this->tubeName,
91 1
        ]);
92
93 1
        $tuple = $result['tuples_list'][0];
94 1
        $index = array_search("space{$this->space}.{$this->tubeName}.tasks.total", $tuple, true);
95
96 1
        return (int) $tuple[$index + 1];
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 1
    public function clear()
103
    {
104 1
        $this->tarantool->call('queue.truncate', [$this->space, $this->tubeName]);
105 1
    }
106
}
107