Completed
Push — master ( f56fd8...8e5f9e )
by Cees-Jan
09:09
created

InfoProvider::setupTimers()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 42
Code Lines 27

Duplication

Lines 26
Ratio 61.9 %

Code Coverage

Tests 19
CRAP Score 5.3741

Importance

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