Completed
Push — dev ( 086b39...e240c9 )
by Vlad
05:40
created

Iterator::finish()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
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'
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
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
    const OPT_FINISH = 'finish';     // flag for finish iteration
24
25
    /**
26
     * @var \Simplario\Quedis\Queue
27
     */
28
    protected $queue;
29
30
    /**
31
     * @var array
32
     */
33
    protected $options;
34
35
    /**
36
     * Iterator constructor.
37
     *
38
     * @param QueueInterface $queue
39
     * @param array          $options
40
     */
41 8
    public function __construct(QueueInterface $queue, array $options = [])
42
    {
43 8
        $options[self::OPT_TIMEOUT] = isset($options[self::OPT_TIMEOUT]) ? (int) $options[self::OPT_TIMEOUT] : 0;
44 8
        $options[self::OPT_SLEEP] = isset($options[self::OPT_SLEEP]) ? (int) $options[self::OPT_SLEEP] : 0;
45 8
        $options[self::OPT_STRATEGY] = isset($options[self::OPT_STRATEGY]) ? $options[self::OPT_STRATEGY] : 'pop';
46 8
        $options[self::OPT_FINISH] = false;
47
48 8
        $this->queue = $queue;
0 ignored issues
show
Documentation Bug introduced by
$queue is of type object<Simplario\Quedis\...erfaces\QueueInterface>, but the property $queue was declared to be of type object<Simplario\Quedis\Queue>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
49 8
        $this->options = $options;
50 8
    }
51
52
    /**
53
     * @param callable $callback
54
     *
55
     * @return $this
56
     */
57 7
    public function each(callable $callback)
58
    {
59 7
        $options = $this->options;
60 7
        $method = in_array($options[self::OPT_STRATEGY], ['pop', 'reserve']) ? $options[self::OPT_STRATEGY] : 'pop';
61
62 7
        $queueName = $options[self::OPT_QUEUE];
63 7
        $timeout = $options[self::OPT_TIMEOUT];
64
65 7
        $count = 0;
66 7
        $loop = 0;
67
68 7
        while ($message = $this->queue->{$method}($queueName, $timeout)) {
69
70 7
            if ($message instanceof MessageInterface) {
71 7
                $callback($message, $this->queue, $this);
72 7
                $count++;
73
            }
74
75 7
            if ($this->isFinish()) {
76 1
                break;
77
            }
78
79 6
            if ($this->isMessagesExceed($count) || $this->isLoopsExceed($loop++)) {
80 2
                break;
81
            }
82
83 5
            $this->trySleep();
84
        }
85
86 7
        return $this;
87
    }
88
89
    /**
90
     * @return $this
91
     */
92 1
    public function finish()
93
    {
94 1
        $this->options[self::OPT_FINISH] = true;
95
96 1
        return $this;
97
    }
98
99
    /**
100
     * @return bool
101
     */
102 7
    protected function isFinish()
103
    {
104 7
        return $this->options[self::OPT_FINISH];
105
    }
106
107
    /**
108
     * @param $count
109
     *
110
     * @return bool
111
     */
112 6
    protected function isMessagesExceed($count)
113
    {
114 6
        return isset($this->options[self::OPT_MESSAGES]) && $this->options[self::OPT_MESSAGES] <= $count;
115
    }
116
117
    /**
118
     * @param $loop
119
     *
120
     * @return bool
121
     */
122 5
    protected function isLoopsExceed($loop)
123
    {
124 5
        return isset($this->options[self::OPT_LOOPS]) && $this->options[self::OPT_LOOPS] <= $loop;
125
    }
126
127
    /**
128
     * @return $this
129
     */
130 5
    protected function trySleep()
131
    {
132 5
        if ($this->options[self::OPT_SLEEP] > 0) {
133 1
            sleep($this->options[self::OPT_SLEEP]);
134
        }
135
136 5
        return $this;
137
    }
138
}