Completed
Push — master ( 489239...8799d0 )
by Eugene
08:40
created

src/Queue.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 163
    public function __construct($client, $name)
28
    {
29 163
        if ($client instanceof Client) {
30 161
            $client = new ClientAdapter($client);
31 2
        } else if (!$client instanceof \Tarantool) {
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 161
        $this->client = $client;
39 161
        $this->name = $name;
40 161
    }
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
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 18
    public function take($timeout = null)
70
    {
71 18
        $args = null === $timeout ? [] : [$timeout];
72 18
        $result = $this->client->call("queue.tube.$this->name:take", $args);
73
74 18
        return empty($result[0]) ? null : Task::createFromTuple($result[0]);
75
    }
76
77
    /**
78
     * @param int $taskId
79
     *
80
     * @return Task
81
     */
82 8
    public function ack($taskId)
83
    {
84 8
        $result = $this->client->call("queue.tube.$this->name:ack", [$taskId]);
85
86 4
        return Task::createFromTuple($result[0]);
87
    }
88
89
    /**
90
     * @param int $taskId
91
     * @param array $options
92
     *
93
     * @return Task
94
     */
95 10 View Code Duplication
    public function release($taskId, array $options = [])
0 ignored issues
show
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...
96
    {
97 10
        $args = $options ? [$taskId, $options] : [$taskId];
98 10
        $result = $this->client->call("queue.tube.$this->name:release", $args);
99
100 6
        return Task::createFromTuple($result[0]);
101
    }
102
103
    /**
104
     * @param int $taskId
105
     *
106
     * @return Task
107
     */
108 12
    public function peek($taskId)
109
    {
110 12
        $result = $this->client->call("queue.tube.$this->name:peek", [$taskId]);
111
112 8
        return Task::createFromTuple($result[0]);
113
    }
114
115
    /**
116
     * @param int $taskId
117
     *
118
     * @return Task
119
     */
120 8
    public function bury($taskId)
121
    {
122 8
        $result = $this->client->call("queue.tube.$this->name:bury", [$taskId]);
123
124 4
        return Task::createFromTuple($result[0]);
125
    }
126
127
    /**
128
     * @param int $count
129
     *
130
     * @return int
131
     */
132 12
    public function kick($count)
133
    {
134 12
        $result = $this->client->call("queue.tube.$this->name:kick", [$count]);
135
136 8
        return $result[0][0];
137
    }
138
139
    /**
140
     * @param int $taskId
141
     *
142
     * @return Task
143
     */
144 10
    public function delete($taskId)
145
    {
146 10
        $result = $this->client->call("queue.tube.$this->name:delete", [$taskId]);
147
148 6
        return Task::createFromTuple($result[0]);
149
    }
150
151 8
    public function truncate()
152
    {
153 8
        $this->client->call("queue.tube.$this->name:truncate");
154 8
    }
155
156
    /**
157
     * @param string|null $path
158
     *
159
     * @return array|int
160
     *
161
     * @throws \InvalidArgumentException
162
     */
163 58
    public function stats($path = null)
164
    {
165 58
        $result = $this->client->call('queue.stats', [$this->name]);
166
167 58
        if (null === $path) {
168 8
            return $result[0][0];
169
        }
170
171 50
        $result = $result[0][0];
172 50
        foreach (explode('.', $path) as $key) {
173 50
            if (!isset($result[$key])) {
174 36
                throw new \InvalidArgumentException(sprintf('Invalid path "%s".', $path));
175
            }
176 30
            $result = $result[$key];
177
        }
178
179 14
        return $result;
180
    }
181
}
182