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