Completed
Push — master ( b23105...c7c667 )
by Eugene
03:16
created

Queue::take()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * This file is part of the Tarantool 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
declare(strict_types=1);
13
14
namespace Tarantool\Queue;
15
16
use Tarantool\Client\Client;
17
18
final class Queue
19
{
20
    private $client;
21
    private $name;
22
23
    /**
24
     * @param \Tarantool|Client $client
25
     * @param string $name
26
     *
27
     * @throws \InvalidArgumentException
28
     */
29 352
    public function __construct($client, string $name)
30
    {
31 352
        if ($client instanceof \Tarantool) {
32 174
            $client = new TarantoolAdapter($client);
33 178
        } elseif (!$client instanceof Client) {
0 ignored issues
show
introduced by
$client is always a sub-type of Tarantool\Client\Client.
Loading history...
34 4
            throw new \InvalidArgumentException(\sprintf(
35 4
                '%s() expects parameter 1 to be %s or Tarantool, %s given.',
36 4
                __METHOD__, Client::class, \is_object($client) ? \get_class($client) : \gettype($client)
37
            ));
38
        }
39
40 348
        $this->client = $client;
41 348
        $this->name = $name;
42 348
    }
43
44 8
    public function getName() : string
45
    {
46 8
        return $this->name;
47
    }
48
49 64
    public function put($data, array $options = []) : Task
50
    {
51 64
        return Task::createFromTuple(
52 64
            $this->client->call("queue.tube.$this->name:put", $data, $options)[0]
53
        );
54
    }
55
56 48
    public function take(float $timeout = null) : ?Task
57
    {
58 48
        $result = $this->client->call("queue.tube.$this->name:take", $timeout);
59
60 48
        return empty($result[0]) ? null : Task::createFromTuple($result[0]);
61
    }
62
63 12
    public function touch(int $taskId, float $increment) : ?Task
64
    {
65 12
        $result = $this->client->call("queue.tube.$this->name:touch", $taskId, $increment);
66
67 12
        return empty($result[0]) ? null : Task::createFromTuple($result[0]);
68
    }
69
70 16
    public function ack(int $taskId) : Task
71
    {
72 16
        return Task::createFromTuple(
73 16
            $this->client->call("queue.tube.$this->name:ack", $taskId)[0]
74
        );
75
    }
76
77 24
    public function release(int $taskId, array $options = []) : Task
78
    {
79 24
        return Task::createFromTuple(
80 24
            $this->client->call("queue.tube.$this->name:release", $taskId, $options)[0]
81
        );
82
    }
83
84 24
    public function peek(int $taskId) : Task
85
    {
86 24
        return Task::createFromTuple(
87 24
            $this->client->call("queue.tube.$this->name:peek", $taskId)[0]
88
        );
89
    }
90
91 16
    public function bury(int $taskId) : Task
92
    {
93 16
        return Task::createFromTuple(
94 16
            $this->client->call("queue.tube.$this->name:bury", $taskId)[0]
95
        );
96
    }
97
98 16
    public function kick(int $count) : int
99
    {
100 16
        return $this->client->call("queue.tube.$this->name:kick", $count)[0];
101
    }
102
103 20
    public function delete(int $taskId) : Task
104
    {
105 20
        return Task::createFromTuple(
106 20
            $this->client->call("queue.tube.$this->name:delete", $taskId)[0]
107
        );
108
    }
109
110 16
    public function truncate() : void
111
    {
112 16
        $this->client->call("queue.tube.$this->name:truncate");
113 16
    }
114
115
    /**
116
     * @param string|null $path
117
     *
118
     * @throws \InvalidArgumentException
119
     *
120
     * @return array|int
121
     */
122 116
    public function stats(string $path = null)
123
    {
124 116
        [$stats] = $this->client->call('queue.stats', $this->name);
125
126 116
        if (null === $path) {
127 16
            return $stats;
128
        }
129
130 100
        foreach (\explode('.', $path) as $key) {
131 100
            if (!isset($stats[$key])) {
132 72
                throw new \InvalidArgumentException(\sprintf('Invalid path "%s".', $path));
133
            }
134 60
            $stats = $stats[$key];
135
        }
136
137 28
        return $stats;
138
    }
139
140 8
    public function call(string $methodName, ...$args) : array
141
    {
142 8
        return $this->client->call("queue.tube.$this->name:$methodName", ...$args);
143
    }
144
}
145