Completed
Pull Request — master (#20)
by Haralan
18:05 queued 16:21
created

Attempt   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 66.67%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 4
c 2
b 0
f 1
lcom 0
cbo 0
dl 0
loc 28
ccs 6
cts 9
cp 0.6667
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A make() 0 17 4
1
<?php
2
3
namespace Openbuildings\Spiderling;
4
5
/**
6
 * A helper class to express attempting to do something several times with a small interval
7
 *
8
 * @package    Openbuildings\Spiderling
9
 * @author     Ivan Kerin
10
 * @copyright  (c) 2013 OpenBuildings Ltd.
11
 * @license    http://spdx.org/licenses/BSD-3-Clause
12
 */
13
class Attempt {
14
15
	/**
16
	 * Execute $callable until $timeout is reached. By doing an attempt each $step milliseconds
17
	 * @param  Callable  $callbale the method to be called, if the result is FALSE, try again until TRUE or timeout reached
18
	 * @param  integer $timeout  timeout in milliseconds
19
	 * @param  integer $step
20
	 * @return mixed            first not FALSE result
21
	 */
22 7
	public static function make($callbale, $timeout = 2000, $step = 50)
23
	{
24
		$retries = ceil($timeout / $step);
25
26
		do
27
		{
28
			$result = $callbale();
29 7
			$retries -= 1;
30 7
			if ( ! $result)
31
			{
32
				usleep($step * 1000);
33
			}
34
		}
35 7
		while ($retries > 0 AND ! $result);
36
37 7
		return $result;
38 3
	}
39
40
}
41