Completed
Push — master ( 848ba6...466d1c )
by Eugene
07:02
created

Queue::call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
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
namespace Tarantool\Queue;
13
14
use Tarantool\Client\Client;
15
16
class Queue
17
{
18
    private $client;
19
    private $name;
20
21
    /**
22
     * @param \Tarantool|\Tarantool\Client\Client $client
23
     * @param string $name
24
     *
25
     * @throws \InvalidArgumentException
26
     */
27 173
    public function __construct($client, $name)
28
    {
29 173
        if ($client instanceof Client) {
30 171
            $client = new ClientAdapter($client);
31 2
        } else if (!$client instanceof \Tarantool) {
0 ignored issues
show
Bug introduced by
The class Tarantool does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
32 2
            throw new \InvalidArgumentException(sprintf(
33 2
                '%s() expects parameter 1 to be Tarantool or Tarantool\Client\Client, %s given.',
34 2
                __METHOD__, is_object($client) ? get_class($client) : gettype($client)
35
            ));
36
        }
37
38 171
        $this->client = $client;
39 171
        $this->name = $name;
40 171
    }
41
42
    /**
43
     * @return string
44
     */
45 1
    public function getName()
46
    {
47 1
        return $this->name;
48
    }
49
50
    /**
51
     * @param mixed $data
52
     * @param array $options
53
     *
54
     * @return Task
55
     */
56 32 View Code Duplication
    public function put($data, array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58 32
        $args = $options ? [$data, $options] : [$data];
59 32
        $result = $this->client->call("queue.tube.$this->name:put", $args);
60
61 32
        return Task::createFromTuple($result[0]);
62
    }
63
64
    /**
65
     * @param int|float|null $timeout
66
     *
67
     * @return Task|null
68
     */
69 24
    public function take($timeout = null)
70
    {
71 24
        $args = null === $timeout ? [] : [$timeout];
72 24
        $result = $this->client->call("queue.tube.$this->name:take", $args);
73
74 24
        return empty($result[0]) ? null : Task::createFromTuple($result[0]);
75
    }
76
77
    /**
78
     * @param int $taskId
79
     * @param int|float $increment
80
     *
81
     * @return Task|null
82
     */
83 6
    public function touch($taskId, $increment)
84
    {
85 6
        $result = $this->client->call("queue.tube.$this->name:touch", [$taskId, $increment]);
86
87 6
        return empty($result[0]) ? null : Task::createFromTuple($result[0]);
88
    }
89
90
    /**
91
     * @param int $taskId
92
     *
93
     * @return Task
94
     */
95 8
    public function ack($taskId)
96
    {
97 8
        $result = $this->client->call("queue.tube.$this->name:ack", [$taskId]);
98
99 4
        return Task::createFromTuple($result[0]);
100
    }
101
102
    /**
103
     * @param int $taskId
104
     * @param array $options
105
     *
106
     * @return Task
107
     */
108 12 View Code Duplication
    public function release($taskId, array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
    {
110 12
        $args = $options ? [$taskId, $options] : [$taskId];
111 12
        $result = $this->client->call("queue.tube.$this->name:release", $args);
112
113 8
        return Task::createFromTuple($result[0]);
114
    }
115
116
    /**
117
     * @param int $taskId
118
     *
119
     * @return Task
120
     */
121 12
    public function peek($taskId)
122
    {
123 12
        $result = $this->client->call("queue.tube.$this->name:peek", [$taskId]);
124
125 8
        return Task::createFromTuple($result[0]);
126
    }
127
128
    /**
129
     * @param int $taskId
130
     *
131
     * @return Task
132
     */
133 8
    public function bury($taskId)
134
    {
135 8
        $result = $this->client->call("queue.tube.$this->name:bury", [$taskId]);
136
137 4
        return Task::createFromTuple($result[0]);
138
    }
139
140
    /**
141
     * @param int $count
142
     *
143
     * @return int
144
     */
145 12
    public function kick($count)
146
    {
147 12
        $result = $this->client->call("queue.tube.$this->name:kick", [$count]);
148
149 8
        return $result[0][0];
150
    }
151
152
    /**
153
     * @param int $taskId
154
     *
155
     * @return Task
156
     */
157 10
    public function delete($taskId)
158
    {
159 10
        $result = $this->client->call("queue.tube.$this->name:delete", [$taskId]);
160
161 6
        return Task::createFromTuple($result[0]);
162
    }
163
164 8
    public function truncate()
165
    {
166 8
        $this->client->call("queue.tube.$this->name:truncate");
167 8
    }
168
169
    /**
170
     * @param string|null $path
171
     *
172
     * @return array|int
173
     *
174
     * @throws \InvalidArgumentException
175
     */
176 58
    public function stats($path = null)
177
    {
178 58
        $result = $this->client->call('queue.stats', [$this->name]);
179
180 58
        if (null === $path) {
181 8
            return $result[0][0];
182
        }
183
184 50
        $result = $result[0][0];
185 50
        foreach (explode('.', $path) as $key) {
186 50
            if (!isset($result[$key])) {
187 36
                throw new \InvalidArgumentException(sprintf('Invalid path "%s".', $path));
188
            }
189 30
            $result = $result[$key];
190
        }
191
192 14
        return $result;
193
    }
194
195
    /**
196
     * @param string $method
197
     * @param array $args
198
     *
199
     * @return array
200
     */
201 4
    public function call($method, array $args = [])
202
    {
203 4
        return $this->client->call("queue.tube.$this->name:$method", $args);
204
    }
205
}
206