1 | <?php |
||
15 | class SpeedRateCounter |
||
16 | { |
||
17 | /** |
||
18 | * Amount of processed bytes |
||
19 | * |
||
20 | * @var int |
||
21 | */ |
||
22 | private $totalBytesProcessed; |
||
23 | |||
24 | /** |
||
25 | * Time when request is started |
||
26 | * |
||
27 | * @var int |
||
28 | */ |
||
29 | private $initialTime; |
||
30 | |||
31 | /** |
||
32 | * Time of last measurement |
||
33 | * |
||
34 | * @var int |
||
35 | */ |
||
36 | private $currentTime; |
||
37 | |||
38 | /** |
||
39 | * Time when speed felt below minimal |
||
40 | * |
||
41 | * @var int |
||
42 | */ |
||
43 | private $slowStartTime; |
||
44 | |||
45 | /** |
||
46 | * Minimum allowed speed in bytes per second |
||
47 | * |
||
48 | * @var float |
||
49 | */ |
||
50 | private $minSpeed; |
||
51 | |||
52 | /** |
||
53 | * Maximum duration of minimum speed in seconds |
||
54 | * |
||
55 | * @var float |
||
56 | */ |
||
57 | private $maxDuration; |
||
58 | |||
59 | /** |
||
60 | * Current speed |
||
61 | * |
||
62 | * @var double |
||
63 | */ |
||
64 | private $currentSpeed; |
||
65 | |||
66 | /** |
||
67 | * SpeedRateCounter constructor. |
||
68 | * |
||
69 | * @param double $minSpeed Minimum allowed speed in bytes per second |
||
70 | * @param double $maxDuration Maximum duration of minimum speed in seconds |
||
71 | */ |
||
72 | 39 | public function __construct($minSpeed, $maxDuration) |
|
78 | |||
79 | /** |
||
80 | * Resets this counter |
||
81 | * |
||
82 | * @return void |
||
83 | */ |
||
84 | 39 | public function reset() |
|
92 | |||
93 | /** |
||
94 | * Process next measurement |
||
95 | * |
||
96 | * @param double $time Time in seconds |
||
97 | * @param double $value Amount of received data in bytes |
||
98 | * |
||
99 | * @return void |
||
100 | * @throws \OverflowException When speed is slower then desired |
||
101 | */ |
||
102 | 39 | public function advance($time, $value) |
|
120 | |||
121 | /** |
||
122 | * Adds measure for counter |
||
123 | * |
||
124 | * @param double $time Time for given value in absolute timestamp |
||
125 | * @param double $value A value |
||
126 | * |
||
127 | * @return void |
||
128 | */ |
||
129 | 39 | private function measure($time, $value) |
|
139 | |||
140 | /** |
||
141 | * Return average speed for measurements |
||
142 | * |
||
143 | * @return double|null |
||
144 | */ |
||
145 | 39 | private function getAverageSpeed() |
|
151 | |||
152 | /** |
||
153 | * Return current speed in bytes per seconds |
||
154 | * |
||
155 | * @return float |
||
156 | */ |
||
157 | 38 | public function getCurrentSpeed() |
|
161 | |||
162 | /** |
||
163 | * Return duration of current slow speed |
||
164 | * |
||
165 | * @return int |
||
166 | */ |
||
167 | 10 | public function getCurrentDuration() |
|
171 | } |
||
172 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.