QScanIterator::setMin()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Phloppy\Client\Queue;
4
5
use Phloppy\Client\AbstractClient;
6
use Phloppy\Stream\StreamInterface;
7
use Psr\Log\LoggerInterface;
8
9
/**
10
 * QSCAN Iterator.
11
 *
12
 * For the command options see https://github.com/antirez/disque
13
 */
14
class QScanIterator extends AbstractClient implements \Iterator {
15
16
    /**
17
     * @var int Current disque cursor.
18
     */
19
    private $cursor = -1;
20
21
    /**
22
     * @var array returned elements.
23
     */
24
    private $elements = [];
25
26
    /**
27
     * @var int current iterator index.
28
     */
29
    private $index = 0;
30
31
    /**
32
     * Pagination count per call.
33
     *
34
     * @var int
35
     */
36
    private $count = 100;
37
38
    /**
39
     * @var int
40
     */
41
    private $min = 0;
42
43
    /**
44
     * @var int
45
     */
46
    private $max = 0;
47
48
    /**
49
     * @var int
50
     */
51
    private $rate = 0;
52
53
54
    /**
55
     * @param StreamInterface $stream
56
     * @param LoggerInterface|null $log
57
     */
58 8
    public function __construct(StreamInterface $stream, LoggerInterface $log = null)
59
    {
60 8
        parent::__construct($stream, $log);
61 8
    }
62
63 4
    private function scan()
64
    {
65
        // initialize cursor
66 4
        if ($this->cursor < 0) {
67 4
            $this->cursor = 0;
68 4
        }
69
70
        // Iterating here because the response of the qscan
71
        // iteration might be empty due to filter restrictions
72
        // (disque internally limits the number of dictScan iterations).
73
        // Thus we rescan if we didn't get any elements and the cursor
74
        // is still valid.
75
        do {
76 4
            $command = ['QSCAN', $this->cursor];
77
78 4
            if ($this->count) {
79 2
                $command = array_merge($command, ['COUNT', $this->count]);
80 2
            } else {
81 2
                $command[] = 'BUSYLOOP';
82
            }
83
84 4
            if ($this->min) {
85 2
                $command = array_merge($command, ['MINLEN', $this->min]);
86 2
            }
87
88
89 4
            if ($this->max) {
90 1
                $command = array_merge($command, ['MAXLEN', $this->max]);
91 1
            }
92
93 4
            if ($this->rate) {
94 2
                $command = array_merge($command, ['IMPORTRATE', $this->rate]);
95 2
            }
96
97 4
            $response       = $this->send($command);
98 3
            $this->cursor   = (int) $response[0];
99 3
            $this->elements = array_merge($this->elements, $response[1]);
100 3
        } while ($this->cursor && empty($response[1]));
101 3
    }
102
103
104
    /**
105
     * Return the current element
106
     *
107
     * @link http://php.net/manual/en/iterator.current.php
108
     * @return string the next queue name
109
     */
110 3
    public function current()
111
    {
112 3
        return $this->elements[$this->index];
113
    }
114
115
    /**
116
     * Move forward to next element
117
     *
118
     * @link http://php.net/manual/en/iterator.next.php
119
     */
120 3
    public function next()
121
    {
122 3
        $this->index++;
123 3
    }
124
125
    /**
126
     * Return the key of the current element
127
     *
128
     * @link http://php.net/manual/en/iterator.key.php
129
     * @return int scalar on success, or null on failure.
130
     */
131 2
    public function key()
132
    {
133 2
        return $this->index;
134
    }
135
136
    /**
137
     * Checks if current position is valid
138
     *
139
     * @link http://php.net/manual/en/iterator.valid.php
140
     * @return boolean true on success or false on failure.
141
     */
142 4
    public function valid()
143
    {
144
        // initialize/refresh cursor
145 4
        if ($this->cursor != 0 && !isset($this->elements[$this->index])) {
146 4
            $this->scan();
147 3
        }
148
149 3
        return isset($this->elements[$this->index]);
150
    }
151
152
    /**
153
     * Rewind the Iterator to the first element
154
     *
155
     * @link http://php.net/manual/en/iterator.rewind.php
156
     */
157 2
    public function rewind()
158
    {
159 2
        $this->index = 0;
160 2
    }
161
162
163
    /**
164
     * @param int $count
165
     * @return QScanIterator
166
     */
167 5
    public function setCount($count)
168
    {
169 5
        $this->count = (int) $count;
170 5
        return $this;
171
    }
172
173
174
    /**
175
     * Set the minimum queue size.
176
     *
177
     * @param int $min min >= 0
178
     * @return QScanIterator
179
     */
180 3
    public function setMin($min)
181
    {
182 3
        $this->min = (int) $min;
183 3
        return $this;
184
    }
185
186
    /**
187
     * @param int $max
188
     * @return QScanIterator
189
     */
190 3
    public function setMax($max)
191
    {
192 3
        $this->max = (int) $max;
193 3
        return $this;
194
    }
195
196
197
    /**
198
     * @param int $rate
199
     * @return QScanIterator
200
     */
201 5
    public function setRate($rate)
202
    {
203 5
        $this->rate = $rate;
204 5
        return $this;
205
    }
206
}
207