| Conditions | 10 |
| Paths | 14 |
| Total Lines | 73 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 78 | protected function sessionHandler($statusCode): int |
||
| 79 | { |
||
| 80 | if (self::RESPONSE_ALLOW !== $statusCode) { |
||
|
|
|||
| 81 | return $statusCode; |
||
| 82 | } |
||
| 83 | |||
| 84 | // If you don't enable `limit traffic`, ignore the following steps. |
||
| 85 | if (empty($this->sessionLimit['count'])) { |
||
| 86 | return self::RESPONSE_ALLOW; |
||
| 87 | |||
| 88 | } else { |
||
| 89 | |||
| 90 | // Get the proerties. |
||
| 91 | $limit = (int) ($this->sessionLimit['count'] ?? 0); |
||
| 92 | $period = (int) ($this->sessionLimit['period'] ?? 300); |
||
| 93 | $now = time(); |
||
| 94 | |||
| 95 | $sessionData = $this->driver->getAll('session'); |
||
| 96 | $sessionPools = []; |
||
| 97 | |||
| 98 | $i = 1; |
||
| 99 | $sessionOrder = 0; |
||
| 100 | |||
| 101 | if (!empty($sessionData)) { |
||
| 102 | foreach ($sessionData as $v) { |
||
| 103 | $sessionPools[] = $v['id']; |
||
| 104 | $lasttime = (int) $v['time']; |
||
| 105 | |||
| 106 | if (get_session()->get('id') === $v['id']) { |
||
| 107 | $sessionOrder = $i; |
||
| 108 | } |
||
| 109 | |||
| 110 | // Remove session if it expires. |
||
| 111 | if ($now - $lasttime > $period) { |
||
| 112 | $this->driver->delete($v['id'], 'session'); |
||
| 113 | } |
||
| 114 | $i++; |
||
| 115 | } |
||
| 116 | |||
| 117 | if (0 === $sessionOrder) { |
||
| 118 | $sessionOrder = $i; |
||
| 119 | } |
||
| 120 | } else { |
||
| 121 | $sessionOrder = 0; |
||
| 122 | } |
||
| 123 | |||
| 124 | // Count the online sessions. |
||
| 125 | $this->sessionStatus['count'] = count($sessionPools); |
||
| 126 | $this->sessionStatus['order'] = $sessionOrder; |
||
| 127 | $this->sessionStatus['queue'] = $sessionOrder - $limit; |
||
| 128 | |||
| 129 | if (!in_array(get_session()->get('id'), $sessionPools)) { |
||
| 130 | $this->sessionStatus['count']++; |
||
| 131 | |||
| 132 | // New session, record this data. |
||
| 133 | $data['id'] = get_session()->get('id'); |
||
| 134 | $data['ip'] = $this->ip; |
||
| 135 | $data['time'] = $now; |
||
| 136 | |||
| 137 | $microtimesamp = explode(' ', microtime()); |
||
| 138 | $microtimesamp = $microtimesamp[1] . str_replace('0.', '', $microtimesamp[0]); |
||
| 139 | $data['microtimesamp'] = $microtimesamp; |
||
| 140 | |||
| 141 | $this->driver->save(get_session()->get('id'), $data, 'session'); |
||
| 142 | } |
||
| 143 | |||
| 144 | // Online session count reached the limit. So return RESPONSE_LIMIT_SESSION response code. |
||
| 145 | if ($sessionOrder >= $limit) { |
||
| 146 | return self::RESPONSE_LIMIT_SESSION; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | return self::RESPONSE_ALLOW; |
||
| 151 | } |
||
| 171 |