Completed
Push — dev ( d32538...b5bde9 )
by Vlad
01:23
created

Iterator::each()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 15
cts 15
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 15
nc 10
nop 1
crap 6
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
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;
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...
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
}