| Total Complexity | 6 |
| Total Lines | 58 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 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() |
||
| 63 | } |
||
| 64 | } |
||
| 65 | } |
||
| 66 |