Repeater::repeat()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zored\Telegram\Util\Repeater;
6
7
class Repeater implements RepeaterInterface
8
{
9
    private const NANO_IN_MILLISECOND = 1000;
10
11
    /**
12
     * @var int
13
     */
14
    private $intervalMilliseconds;
15
16
    /**
17
     * @var int
18
     */
19
    private $maxTimeMilliseconds;
20
21
    /**
22
     * @param int $intervalMilliseconds
23
     */
24
    public function __construct(int $intervalMilliseconds, int $maxTimeMilliseconds)
25
    {
26
        $this->intervalMilliseconds = $intervalMilliseconds;
27
        $this->maxTimeMilliseconds = $maxTimeMilliseconds;
28
    }
29
30
    public function repeat(callable $callable): void
31
    {
32
        $time = 0;
33
        while (true) {
34
            if ($time >= $this->maxTimeMilliseconds) {
35
                break;
36
            }
37
            $callable();
38
            usleep($this->intervalMilliseconds * self::NANO_IN_MILLISECOND);
39
            $time += $this->intervalMilliseconds;
40
        }
41
    }
42
}
43