Timers::clearing()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Timers manager
4
 * User: moyo
5
 * Date: 2018/5/19
6
 * Time: 3:07 PM
7
 */
8
9
namespace Carno\Monitor\Chips;
10
11
use Carno\Timer\Timer;
12
use Closure;
13
14
trait Timers
15
{
16
    /**
17
     * @var array array
18
     */
19
    private $timers = [];
20
21
    /**
22
     * @param int $period
23
     * @param callable $do
24
     * @return string
25
     */
26
    protected function insert(int $period, callable $do) : string
27
    {
28
        $ik = sprintf('ik-%d', $period);
29
30
        if (isset($this->timers[$ik])) {
31
            return $ik;
32
        }
33
34
        $this->timers[$ik] = Timer::loop($period * 1000, static function () use ($ik, $do) {
35
            call_user_func($do, $ik);
36
        });
37
38
        return $ik;
39
    }
40
41
    /**
42
     * @param Closure $sync
43
     */
44
    protected function clearing(Closure $sync) : void
45
    {
46
        foreach ($this->timers as $ik => $timer) {
47
            $sync($ik);
48
            Timer::clear($timer);
49
        }
50
    }
51
}
52