1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Simplario\Quedis; |
4
|
|
|
|
5
|
|
|
use Simplario\Quedis\Interfaces\IteratorInterface; |
6
|
|
|
use Simplario\Quedis\Interfaces\MessageInterface; |
7
|
|
|
use Simplario\Quedis\Interfaces\QueueInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Iterator |
11
|
|
|
* |
12
|
|
|
* @package Simplario\Quedis |
13
|
|
|
*/ |
14
|
|
|
class Iterator implements IteratorInterface |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
const OPT_QUEUE = 'queue'; // queue name |
18
|
|
|
const OPT_TIMEOUT = 'timeout'; // 0 .. 999999999 sec |
19
|
|
|
const OPT_STRATEGY = 'strategy'; // 'pop' or 'reserve' |
|
|
|
|
20
|
|
|
const OPT_MESSAGES = 'messages'; // 0 .. 999999999 items |
21
|
|
|
const OPT_LOOPS = 'loops'; // 0 .. 999999999 sec |
22
|
|
|
const OPT_SLEEP = 'sleep'; // 0 .. 999999999 sec |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Simplario\Quedis\Queue |
26
|
|
|
*/ |
27
|
|
|
protected $queue; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $options; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Iterator constructor. |
36
|
|
|
* |
37
|
|
|
* @param QueueInterface $queue |
38
|
|
|
* @param array $options |
39
|
|
|
*/ |
40
|
7 |
|
public function __construct(QueueInterface $queue, array $options = []) |
41
|
|
|
{ |
42
|
7 |
|
$options[self::OPT_TIMEOUT] = (int)isset($options[self::OPT_MESSAGES]) ? $options[self::OPT_MESSAGES] : 0; |
43
|
7 |
|
$options[self::OPT_SLEEP] = (int)isset($options[self::OPT_SLEEP]) ? $options[self::OPT_SLEEP] : 0; |
44
|
7 |
|
$options[self::OPT_STRATEGY] = isset($options[self::OPT_STRATEGY]) ? $options[self::OPT_STRATEGY] : 'pop'; |
45
|
|
|
|
46
|
7 |
|
$this->queue = $queue; |
|
|
|
|
47
|
7 |
|
$this->options = $options; |
48
|
7 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param callable $callback |
52
|
|
|
* |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
6 |
|
public function each(callable $callback) |
56
|
|
|
{ |
57
|
6 |
|
$options = $this->options; |
58
|
6 |
|
$method = in_array($options[self::OPT_STRATEGY], ['pop', 'reserve']) ? $options[self::OPT_STRATEGY] : 'pop'; |
59
|
|
|
|
60
|
6 |
|
$queueName = $options[self::OPT_QUEUE]; |
61
|
6 |
|
$timeout = $options[self::OPT_TIMEOUT]; |
62
|
|
|
|
63
|
6 |
|
$count = 0; |
64
|
6 |
|
$loop = 0; |
65
|
|
|
|
66
|
6 |
|
while ($message = $this->queue->{$method}($queueName, $timeout)) { |
67
|
|
|
|
68
|
6 |
|
if ($message instanceof MessageInterface) { |
69
|
6 |
|
$callback($message, $this->queue); |
70
|
6 |
|
$count++; |
71
|
|
|
} |
72
|
|
|
|
73
|
6 |
|
if ($this->isMessagesExceed($count) || $this->isLoopsExceed($loop++)) { |
74
|
2 |
|
break; |
75
|
|
|
} |
76
|
|
|
|
77
|
5 |
|
$this->trySleep(); |
78
|
|
|
} |
79
|
|
|
|
80
|
6 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $count |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
6 |
|
protected function isMessagesExceed($count) |
89
|
|
|
{ |
90
|
6 |
|
return isset($this->options[self::OPT_MESSAGES]) && $this->options[self::OPT_MESSAGES] <= $count; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $loop |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
5 |
|
protected function isLoopsExceed($loop) |
99
|
|
|
{ |
100
|
5 |
|
return isset($this->options[self::OPT_LOOPS]) && $this->options[self::OPT_LOOPS] <= $loop; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
5 |
|
protected function trySleep() |
107
|
|
|
{ |
108
|
5 |
|
if ($this->options[self::OPT_SLEEP] > 0) { |
109
|
1 |
|
sleep($this->options[self::OPT_SLEEP]); |
110
|
|
|
} |
111
|
|
|
|
112
|
5 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.