HScanner::gathering()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Harvester scanner
4
 * User: moyo
5
 * Date: 2018/5/19
6
 * Time: 2:44 PM
7
 */
8
9
namespace Carno\Monitor\Collector;
10
11
use Carno\Monitor\Chips\Timers;
12
use Carno\Monitor\Contracts\Telemetry;
13
use Carno\Monitor\Daemon;
14
use Carno\Process\Piping;
15
16
class HScanner
17
{
18
    use Timers;
19
20
    /**
21
     * @var array
22
     */
23
    private $tasks = [];
24
25
    /**
26
     * @var Daemon
27
     */
28
    private $transport = null;
29
30
    /**
31
     * @var string
32
     */
33
    private $identify = null;
34
35
    /**
36
     * Scanner constructor.
37
     * @param Piping $daemon
38
     */
39
    public function __construct(Piping $daemon)
40
    {
41
        $this->transport = $daemon;
0 ignored issues
show
Documentation Bug introduced by
It seems like $daemon of type Carno\Process\Piping is incompatible with the declared type Carno\Monitor\Daemon of property $transport.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
        $this->identify = sprintf('p-%d', getmypid());
43
    }
44
45
    /**
46
     * @param Telemetry $source
47
     * @param int $period
48
     */
49
    public function sourcing(Telemetry $source, int $period) : void
50
    {
51
        $this->tasks[$this->insert($period, function (string $ik) {
52
            $this->running($ik);
53
        })][] = $source;
54
55
        $this->gathering($source);
56
    }
57
58
    /**
59
     * @param Telemetry $source
60
     */
61
    public function removing(Telemetry $source) : void
62
    {
63
        foreach ($this->tasks as $ik => $sources) {
64
            foreach ($sources as $idx => $exists) {
65
                if ($exists === $source) {
66
                    unset($this->tasks[$ik][$idx]);
67
                    $this->transport->remove($this->identify, ...$source->reporting());
68
                }
69
            }
70
        }
71
    }
72
73
    /**
74
     */
75
    public function stopping() : void
76
    {
77
        $this->clearing(function (string $ik) {
78
            unset($this->tasks[$ik]);
79
        });
80
    }
81
82
    /**
83
     * @param string $ik
84
     */
85
    private function running(string $ik) : void
86
    {
87
        $w = $this->tasks[$ik] ?? [];
88
        array_walk($w, function (Telemetry $source) {
89
            $this->gathering($source);
90
        });
91
    }
92
93
    /**
94
     * @param Telemetry $source
95
     */
96
    private function gathering(Telemetry $source) : void
97
    {
98
        ($posted = $source->reporting()) && $this->transport->metrics($this->identify, ...$posted);
99
    }
100
}
101