Test Failed
Push — master ( 138499...684014 )
by Dev
02:21
created

SleepTrait::getSleep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace rOpenDev\Google;
4
5
trait SleepTrait
6
{
7
    /** @var int Time we need to wait between two request * */
8
    protected $sleep = 0;
9
10
    /**
11
     * Chainable `$waitBetweenRequests` setter.
12
     *
13
     * @return self
14
     */
15
    public function setSleep($seconds)
16
    {
17
        $this->sleep = $seconds * 1000000;
18
19
        return $this;
20
    }
21
22
    /**
23
     * Return the time the script need to sleep.
24
     *
25
     * @return int Microseconds
26
     */
27
    protected function getSleep()
28
    {
29
        $halfSleep = $this->sleep / 2;
30
        $sleepMin = (int) floor($this->sleep - $halfSleep);
31
        $sleepMax = (int) ceil($this->sleep + $halfSleep);
32
33
        return rand($sleepMin, $sleepMax);
34
    }
35
36
    /**
37
     * Exec sleep.
38
     *
39
     * @return int The time we rest
40
     */
41
    public function execSleep()
42
    {
43
        if ($this->sleep) {
44
            $sleep = $this->getSleep();
45
            usleep($sleep);
46
47
            return $sleep;
48
        }
49
    }
50
51
    /**
52
     * Exec a half sleep.
53
     *
54
     * @return int The time we rest
55
     */
56
    public function execHalfSleep()
57
    {
58
        if ($this->sleep) {
59
            $sleep = round($this->getSleep() / 2);
60
            usleep($sleep);
0 ignored issues
show
Bug introduced by
$sleep of type double is incompatible with the type integer expected by parameter $micro_seconds of usleep(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
            usleep(/** @scrutinizer ignore-type */ $sleep);
Loading history...
61
62
            return $sleep;
63
        }
64
    }
65
}
66