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

Queue   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 166
Duplicated Lines 8.43 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 2
dl 14
loc 166
ccs 52
cts 52
cp 1
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 4
A getName() 0 4 1
A put() 7 7 2
A take() 0 7 3
A ack() 0 6 1
A release() 7 7 2
A peek() 0 6 1
A bury() 0 6 1
A kick() 0 6 1
A delete() 0 6 1
A truncate() 0 4 1
A stats() 0 18 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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) {
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 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
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 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
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...
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