Completed
Push — master ( 516660...f56fd8 )
by Cees-Jan
06:37
created

InfoProvider::reset()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.8571
c 0
b 0
f 0
ccs 26
cts 26
cp 1
cc 1
eloc 25
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\Inspector;
4
5
use React\EventLoop\Timer\TimerInterface;
6
7
class InfoProvider
8
{
9
    /**
10
     * @var LoopDecorator
11
     */
12
    protected $loop;
13
14
    /**
15
     * @var array
16
     */
17
    protected $counters = [];
18
19
    /**
20
     * @var array
21
     */
22
    protected $streamsRead = [];
23
24
    /**
25
     * @var array
26
     */
27
    protected $streamsWrite = [];
28
29
    /**
30
     * @var array
31
     */
32
    protected $streamsDuplex = [];
33
34
    /**
35
     * @param LoopDecorator $loop
36
     */
37 9
    public function __construct(LoopDecorator $loop)
38
    {
39 9
        $this->loop = $loop;
40 9
        $this->reset();
41
42 9
        $this->setupTicks($loop);
43 9
        $this->setupTimers($loop);
44 9
        $this->setupStreams($loop);
45 9
    }
46
47 9
    public function reset()
48
    {
49
        /**
50 3
         * Streams.
51 3
         */
52 9
        GlobalState::set('streams.read.min', 0);
53
        GlobalState::set('streams.read.current', 0);
54 3
        GlobalState::set('streams.read.max', 0);
55 3
        GlobalState::set('streams.read.total', 0);
56 9
        GlobalState::set('streams.read.ticks', 0);
57
        GlobalState::set('streams.total.min', 0);
58 1
        GlobalState::set('streams.total.current', 0);
59 1
        GlobalState::set('streams.total.max', 0);
60 9
        GlobalState::set('streams.total.total', 0);
61
        GlobalState::set('streams.total.ticks', 0);
62 1
        GlobalState::set('streams.write.min', 0);
63 1
        GlobalState::set('streams.write.current', 0);
64 9
        GlobalState::set('streams.write.max', 0);
65 9
        GlobalState::set('streams.write.total', 0);
66
        GlobalState::set('streams.write.ticks', 0);
67 9
68
        /**
69
         * Timers.
70 1
         */
71 1
        GlobalState::set('timers.once.current', 0);
72 9
        GlobalState::set('timers.once.total', 0);
73
        GlobalState::set('timers.once.ticks', 0);
74 1
        GlobalState::set('timers.periodic.current', 0);
75 1
        GlobalState::set('timers.periodic.total', 0);
76 9
        GlobalState::set('timers.periodic.ticks', 0);
77
78 1
        /**
79 1
         * Ticks.
80 9
         */
81
        GlobalState::set('ticks.future.current', 0);
82 1
        GlobalState::set('ticks.future.total', 0);
83 9
        GlobalState::set('ticks.future.ticks', 0);
84
    }
85 1
86 1
    public function resetTotals()
87 1
    {
88 9
        GlobalState::set('streams.read.total', 0);
89 9
        GlobalState::set('streams.total.total', 0);
90
        GlobalState::set('streams.write.total', 0);
91 9
        GlobalState::set('timers.once.total', 0);
92
        GlobalState::set('timers.periodic.total', 0);
93
        GlobalState::set('ticks.future.total', 0);
94 2
    }
95
96 2
    public function resetTicks()
97 2
    {
98
        GlobalState::set('streams.read.ticks', 0);
99 2
        GlobalState::set('streams.total.ticks', 0);
100 2
        GlobalState::set('streams.write.ticks', 0);
101 2
        GlobalState::set('timers.once.ticks', 0);
102 2
        GlobalState::set('timers.periodic.ticks', 0);
103 2
        GlobalState::set('ticks.future.ticks', 0);
104 2
    }
105 9
106
    /**
107
     * @return array
108
     */
109 9
    public function getCounters()
110
    {
111 2
        return GlobalState::get();
112
    }
113 2
114 2
    protected function setupTicks(LoopDecorator $loop)
115 2
    {
116 2
        $loop->on('futureTick', function () {
117 2
            GlobalState::incr('ticks.future.current');
118 2
            GlobalState::incr('ticks.future.total');
119
        });
120 2
        $loop->on('futureTickTick', function () {
121 2
            GlobalState::decr('ticks.future.current');
122 9
            GlobalState::incr('ticks.future.ticks');
123
        });
124
    }
125 2
126
    protected function setupTimers(LoopDecorator $loop)
127 2
    {
128 2
        $loop->on('addTimer', function () {
129
            GlobalState::incr('timers.once.current');
130 2
            GlobalState::incr('timers.once.total');
131 2
        });
132 2
        $loop->on('timerTick', function () {
133
            GlobalState::decr('timers.once.current');
134 2
            GlobalState::incr('timers.once.ticks');
135 2
        });
136 2
        $loop->on('addPeriodicTimer', function () {
137 9
            GlobalState::incr('timers.periodic.current');
138
            GlobalState::incr('timers.periodic.total');
139
        });
140
        $loop->on('periodicTimerTick', function () {
141 9
            GlobalState::incr('timers.periodic.ticks');
142
        });
143 2
        $loop->on('cancelTimer', function (TimerInterface $timer) {
144
            if ($timer->isPeriodic()) {
145 2
                GlobalState::decr('timers.periodic.current');
146 2
            }
147 2
        });
148 2
    }
149 2
150 2
    protected function setupStreams(LoopDecorator $loop)
151
    {
152 2 View Code Duplication
        $loop->on('addReadStream', function ($stream) {
153 2
            $key = (int) $stream;
154 9
155
            $this->streamsRead[$key] = $stream;
156 9
            $this->streamsDuplex[$key] = $stream;
157
158
            GlobalState::set('streams.read.current', count($this->streamsRead));
159
            GlobalState::set('streams.total.current', count($this->streamsDuplex));
160
            GlobalState::incr('streams.read.total');
161
            if (!isset($this->streamsWrite[$key])) {
162
                GlobalState::incr('streams.total.total');
163
            }
164
        });
165
        $loop->on('readStreamTick', function () {
166
            GlobalState::incr('streams.read.ticks');
167
            GlobalState::incr('streams.total.ticks');
168
        });
169 View Code Duplication
        $loop->on('removeReadStream', function ($stream) {
170
            $key = (int) $stream;
171
172 9
            if (isset($this->streamsRead[$key])) {
173 9
                unset($this->streamsRead[$key]);
174
            }
175 9
            if (isset($this->streamsDuplex[$key]) && !isset($this->streamsWrite[$key])) {
176
                unset($this->streamsDuplex[$key]);
177 9
            }
178
179
            GlobalState::set('streams.read.current', count($this->streamsRead));
180 9
            GlobalState::set('streams.total.current', count($this->streamsDuplex));
181 9
        });
182 9
183 9 View Code Duplication
        $loop->on('addWriteStream', function ($stream) {
184 9
            $key = (int) $stream;
185 9
186
            $this->streamsWrite[$key] = $stream;
187 9
            $this->streamsDuplex[$key] = $stream;
188 9
189 9
            GlobalState::set('streams.write.current', count($this->streamsWrite));
190 9
            GlobalState::set('streams.total.current', count($this->streamsDuplex));
191 9
            GlobalState::incr('streams.write.total');
192 9
193
            if (!isset($this->streamsRead[$key])) {
194 9
                GlobalState::incr('streams.total.total');
195 9
            }
196 9
        });
197 9
        $loop->on('writeStreamTick', function () {
198 9
            GlobalState::incr('streams.write.ticks');
199 9
            GlobalState::incr('streams.total.ticks');
200 9
        });
201 View Code Duplication
        $loop->on('removeWriteStream', function ($stream) {
202
            $key = (int) $stream;
203 9
204 9
            if (isset($this->streamsWrite[$key])) {
205 9
                unset($this->streamsWrite[$key]);
206 9
            }
207
            if (isset($this->streamsDuplex[$key]) && !isset($this->streamsRead[$key])) {
208 9
                unset($this->streamsDuplex[$key]);
209 9
            }
210 9
211 9
            GlobalState::set('streams.write.current', count($this->streamsWrite));
212 9
            GlobalState::set('streams.total.current', count($this->streamsDuplex));
213
        });
214
215 9
        $loop->on('removeStream', function ($stream) {
216 9
            $key = (int) $stream;
217 9
218 9
            if (isset($this->streamsRead[$key])) {
219
                unset($this->streamsRead[$key]);
220 9
            }
221 9
            if (isset($this->streamsWrite[$key])) {
222 9
                unset($this->streamsWrite[$key]);
223 9
            }
224 9
            if (isset($this->streamsDuplex[$key])) {
225
                unset($this->streamsDuplex[$key]);
226 9
            }
227
228 1
            GlobalState::set('streams.read.current', count($this->streamsRead));
229
            GlobalState::set('streams.write.current', count($this->streamsWrite));
230 1
            GlobalState::set('streams.total.current', count($this->streamsDuplex));
231 1
        });
232 1
    }
233
}
234