| Conditions | 10 |
| Paths | 14 |
| Total Lines | 75 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 130 | protected function sessionHandler($statusCode): int |
||
| 131 | { |
||
| 132 | if (Kernel::RESPONSE_ALLOW !== $statusCode) { |
||
| 133 | return $statusCode; |
||
| 134 | } |
||
| 135 | |||
| 136 | // If you don't enable `limit traffic`, ignore the following steps. |
||
| 137 | if (empty($this->sessionLimit['count'])) { |
||
| 138 | return Kernel::RESPONSE_ALLOW; |
||
| 139 | |||
| 140 | } else { |
||
| 141 | |||
| 142 | // Get the proerties. |
||
| 143 | $limit = (int) ($this->sessionLimit['count'] ?? 0); |
||
| 144 | $period = (int) ($this->sessionLimit['period'] ?? 300); |
||
| 145 | $now = time(); |
||
| 146 | |||
| 147 | $this->sessionData = $this->driver->getAll('session'); |
||
| 148 | $sessionPools = []; |
||
| 149 | |||
| 150 | $i = 1; |
||
| 151 | $sessionOrder = 0; |
||
| 152 | |||
| 153 | if (!empty($this->sessionData)) { |
||
| 154 | foreach ($this->sessionData as $v) { |
||
| 155 | $sessionPools[] = $v['id']; |
||
| 156 | $lasttime = (int) $v['time']; |
||
| 157 | |||
| 158 | if (get_session()->get('id') === $v['id']) { |
||
| 159 | $sessionOrder = $i; |
||
| 160 | } |
||
| 161 | |||
| 162 | // Remove session if it expires. |
||
| 163 | if ($now - $lasttime > $period) { |
||
| 164 | $this->driver->delete($v['id'], 'session'); |
||
| 165 | } |
||
| 166 | $i++; |
||
| 167 | } |
||
| 168 | |||
| 169 | if (0 === $sessionOrder) { |
||
| 170 | $sessionOrder = $i; |
||
| 171 | } |
||
| 172 | } else { |
||
| 173 | $sessionOrder = 0; |
||
| 174 | } |
||
| 175 | |||
| 176 | // Count the online sessions. |
||
| 177 | $this->sessionStatus['count'] = count($sessionPools); |
||
| 178 | $this->sessionStatus['order'] = $sessionOrder; |
||
| 179 | $this->sessionStatus['queue'] = $sessionOrder - $limit; |
||
| 180 | |||
| 181 | if (!in_array(get_session()->get('id'), $sessionPools)) { |
||
| 182 | $this->sessionStatus['count']++; |
||
| 183 | |||
| 184 | $data = []; |
||
| 185 | |||
| 186 | // New session, record this data. |
||
| 187 | $data['id'] = get_session()->get('id'); |
||
| 188 | $data['ip'] = $this->ip; |
||
| 189 | $data['time'] = $now; |
||
| 190 | |||
| 191 | $microtimesamp = explode(' ', microtime()); |
||
| 192 | $microtimesamp = $microtimesamp[1] . str_replace('0.', '', $microtimesamp[0]); |
||
| 193 | $data['microtimesamp'] = $microtimesamp; |
||
| 194 | |||
| 195 | $this->driver->save(get_session()->get('id'), $data, 'session'); |
||
| 196 | } |
||
| 197 | |||
| 198 | // Online session count reached the limit. So return RESPONSE_LIMIT_SESSION response code. |
||
| 199 | if ($sessionOrder >= $limit) { |
||
| 200 | return Kernel::RESPONSE_LIMIT_SESSION; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | return Kernel::RESPONSE_ALLOW; |
||
| 205 | } |
||
| 244 |