Loop   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 33
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 8 3
A stop() 0 3 1
1
<?php
2
3
namespace EasyHttp;
4
5
use EasyHttp\Utils\Toolkit;
6
7
/**
8
 * Loop class
9
 *
10
 * @link    https://github.com/shahradelahi/easy-http
11
 * @author  Shahrad Elahi (https://github.com/shahradelahi)
12
 * @license https://github.com/shahradelahi/easy-http/blob/master/LICENSE (MIT License)
13
 */
14
class Loop
15
{
16
17
	/**
18
	 * Whether the loop is running or not
19
	 *
20
	 * @var bool
21
	 */
22
	protected static bool $running = false;
23
24
	/**
25
	 * @param callable $callback
26
	 * @param int $interval in milliseconds
27
	 * @return void
28
	 */
29
	public static function run(callable $callback, int $interval = 500): void
30
	{
31
		static::$running = true;
32
		$last_hit = Toolkit::time();
33
		while (static::$running) {
34
			if (Toolkit::time() - $last_hit > $interval) {
35
				$callback();
36
				$last_hit = Toolkit::time();
37
			}
38
		}
39
	}
40
41
	/**
42
	 * @return void
43
	 */
44
	public static function stop(): void
45
	{
46
		static::$running = false;
47
	}
48
49
}