CWorker::resume()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 1
nc 4
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Connections related worker
4
 * User: moyo
5
 * Date: 2018/5/23
6
 * Time: 10:17 AM
7
 */
8
9
namespace Carno\Pool\Chips;
10
11
use Carno\Pool\Connections;
12
use Carno\Timer\Timer;
13
14
trait CWorker
15
{
16
    /**
17
     * @var Connections
18
     */
19
    private $connections = null;
20
21
    /**
22
     * @var string
23
     */
24
    private $timer = null;
25
26
    /**
27
     * @var int
28
     */
29
    private $ivs = 0;
30
31
    /**
32
     * @var callable
33
     */
34
    private $ex = null;
35
36
    /**
37
     * @return Connections
38
     */
39
    protected function conn() : Connections
40
    {
41
        return $this->connections;
42
    }
43
44
    /**
45
     * @param Connections $connections
46
     */
47
    protected function track(Connections $connections) : void
48
    {
49
        $this->connections = $connections;
50
    }
51
52
    /**
53
     */
54
    public function untrack() : void
55
    {
56
        $this->ex = null;
57
        $this->connections = null;
58
    }
59
60
    /**
61
     * @param int $ivs
62
     * @param callable $processor
63
     */
64
    protected function ticker(int $ivs, callable $processor) : void
65
    {
66
        $ivs > 0 && $this->timer = Timer::loop(($this->ivs = $ivs) * 1000, $this->ex = $processor);
67
    }
68
69
    /**
70
     */
71
    public function pause() : void
72
    {
73
        $this->timer && Timer::clear($this->timer) && $this->timer = null;
74
    }
75
76
    /**
77
     */
78
    public function resume() : void
79
    {
80
        (is_null($this->timer) && $this->ex && $this->connections) && $this->ticker($this->ivs, $this->ex);
81
    }
82
83
    /**
84
     */
85
    public function shutdown() : void
86
    {
87
        $this->pause();
88
        $this->untrack();
89
    }
90
}
91