| Total Complexity | 46 |
| Total Lines | 342 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like FileHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FileHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class FileHandler extends AbstractHandler |
||
| 27 | {
|
||
| 28 | /** |
||
| 29 | * Platform Name |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $platform = 'file'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * File Handle |
||
| 37 | * |
||
| 38 | * @var resource |
||
| 39 | */ |
||
| 40 | protected $file; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * File write path |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $path; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * File Name and Path |
||
| 51 | * |
||
| 52 | * @var resource |
||
| 53 | */ |
||
| 54 | protected $filePath; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Is New File Flag |
||
| 58 | * |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | protected $isNewFile; |
||
| 62 | |||
| 63 | // ------------------------------------------------------------------------ |
||
| 64 | |||
| 65 | /** |
||
| 66 | * FileHandler::open |
||
| 67 | * |
||
| 68 | * Initialize session |
||
| 69 | * |
||
| 70 | * @link http://php.net/manual/en/sessionhandlerinterface.open.php |
||
| 71 | * |
||
| 72 | * @param string $save_path The path where to store/retrieve the session. |
||
| 73 | * @param string $name The session name. |
||
| 74 | * |
||
| 75 | * @return bool <p> |
||
| 76 | * The return value (usually TRUE on success, FALSE on failure). |
||
| 77 | * Note this value is returned internally to PHP for processing. |
||
| 78 | * </p> |
||
| 79 | * @since 5.4.0 |
||
| 80 | */ |
||
| 81 | public function open($save_path, $name) |
||
| 82 | {
|
||
| 83 | $this->path = $this->config[ 'filePath' ]; |
||
| 84 | |||
| 85 | if ($this->isSupported() === false) {
|
||
| 86 | if ($this->logger instanceof LoggerInterface) {
|
||
|
|
|||
| 87 | $this->logger->error('SESSION_E_FILE_UNSUPPORTED', [$this->path]);
|
||
| 88 | } |
||
| 89 | |||
| 90 | return $this->failure; |
||
| 91 | } |
||
| 92 | |||
| 93 | $this->filePath = $this->path |
||
| 94 | . $name . '-' // we'll use the session cookie name as a prefix to avoid collisions |
||
| 95 | . ($this->config[ 'match' ]->ip ? md5($_SERVER[ 'REMOTE_ADDR' ]) . '-' : ''); |
||
| 96 | |||
| 97 | return $this->success; |
||
| 98 | } |
||
| 99 | |||
| 100 | // ------------------------------------------------------------------------ |
||
| 101 | |||
| 102 | /** |
||
| 103 | * FileHandler::isSupported |
||
| 104 | * |
||
| 105 | * Checks if this platform is supported on this system. |
||
| 106 | * |
||
| 107 | * @return bool Returns FALSE if unsupported. |
||
| 108 | */ |
||
| 109 | public function isSupported() |
||
| 110 | {
|
||
| 111 | if ( ! is_writable($this->path)) {
|
||
| 112 | @mkdir($this->path, 0777, true); |
||
| 113 | } |
||
| 114 | |||
| 115 | return (bool)is_writable($this->path); |
||
| 116 | } |
||
| 117 | |||
| 118 | // ------------------------------------------------------------------------ |
||
| 119 | |||
| 120 | /** |
||
| 121 | * FileHandler::destroy |
||
| 122 | * |
||
| 123 | * Destroy a session |
||
| 124 | * |
||
| 125 | * @link http://php.net/manual/en/sessionhandlerinterface.destroy.php |
||
| 126 | * |
||
| 127 | * @param string $session_id The session ID being destroyed. |
||
| 128 | * |
||
| 129 | * @return bool <p> |
||
| 130 | * The return value (usually TRUE on success, FALSE on failure). |
||
| 131 | * Note this value is returned internally to PHP for processing. |
||
| 132 | * </p> |
||
| 133 | * @since 5.4.0 |
||
| 134 | */ |
||
| 135 | public function destroy($session_id) |
||
| 136 | {
|
||
| 137 | if ($this->close()) {
|
||
| 138 | return file_exists($this->filePath . $session_id) |
||
| 139 | ? (unlink($this->filePath . $session_id) && $this->destroyCookie()) |
||
| 140 | : true; |
||
| 141 | } elseif ($this->filePath !== null) {
|
||
| 142 | clearstatcache(); |
||
| 143 | |||
| 144 | return file_exists($this->filePath . $session_id) |
||
| 145 | ? (unlink($this->filePath . $session_id) && $this->destroyCookie()) |
||
| 146 | : true; |
||
| 147 | } |
||
| 148 | |||
| 149 | return false; |
||
| 150 | } |
||
| 151 | |||
| 152 | // ------------------------------------------------------------------------ |
||
| 153 | |||
| 154 | /** |
||
| 155 | * FileHandler::close |
||
| 156 | * |
||
| 157 | * Close the session |
||
| 158 | * |
||
| 159 | * @link http://php.net/manual/en/sessionhandlerinterface.close.php |
||
| 160 | * @return bool <p> |
||
| 161 | * The return value (usually TRUE on success, FALSE on failure). |
||
| 162 | * Note this value is returned internally to PHP for processing. |
||
| 163 | * </p> |
||
| 164 | * @since 5.4.0 |
||
| 165 | */ |
||
| 166 | public function close() |
||
| 167 | {
|
||
| 168 | if (is_resource($this->file)) {
|
||
| 169 | flock($this->file, LOCK_UN); |
||
| 170 | fclose($this->file); |
||
| 171 | |||
| 172 | $this->file = $this->isNewFile = $this->sessionId = null; |
||
| 173 | |||
| 174 | return true; |
||
| 175 | } |
||
| 176 | |||
| 177 | return true; |
||
| 178 | } |
||
| 179 | |||
| 180 | // ------------------------------------------------------------------------ |
||
| 181 | |||
| 182 | /** |
||
| 183 | * FileHandler::gc |
||
| 184 | * |
||
| 185 | * Cleanup old sessions |
||
| 186 | * |
||
| 187 | * @link http://php.net/manual/en/sessionhandlerinterface.gc.php |
||
| 188 | * |
||
| 189 | * @param int $maxlifetime <p> |
||
| 190 | * Sessions that have not updated for |
||
| 191 | * the last maxlifetime seconds will be removed. |
||
| 192 | * </p> |
||
| 193 | * |
||
| 194 | * @return bool <p> |
||
| 195 | * The return value (usually TRUE on success, FALSE on failure). |
||
| 196 | * Note this value is returned internally to PHP for processing. |
||
| 197 | * </p> |
||
| 198 | * @since 5.4.0 |
||
| 199 | */ |
||
| 200 | public function gc($maxlifetime) |
||
| 201 | {
|
||
| 202 | if ( ! is_dir($this->path) || ($directory = opendir($this->path)) === false) {
|
||
| 203 | if ($this->logger instanceof LoggerInterface) {
|
||
| 204 | $this->logger->error('SESSION_E_FILE_ON_GC', [$this->path]);
|
||
| 205 | } |
||
| 206 | |||
| 207 | return false; |
||
| 208 | } |
||
| 209 | |||
| 210 | $ts = time() - $maxlifetime; |
||
| 211 | |||
| 212 | while (($file = readdir($directory)) !== false) {
|
||
| 213 | // If the filename doesn't match this pattern, it's either not a session file or is not ours |
||
| 214 | if ( ! preg_match('/[' . $this->config[ 'name' ] . '-]+[0-9-a-f]+/', $file)
|
||
| 215 | || ! is_file($this->path . '/' . $file) |
||
| 216 | || ($mtime = filemtime($this->path . '/' . $file)) === false |
||
| 217 | || $mtime > $ts |
||
| 218 | ) {
|
||
| 219 | continue; |
||
| 220 | } |
||
| 221 | |||
| 222 | unlink($this->path . '/' . $file); |
||
| 223 | } |
||
| 224 | |||
| 225 | closedir($directory); |
||
| 226 | |||
| 227 | return true; |
||
| 228 | } |
||
| 229 | |||
| 230 | // ------------------------------------------------------------------------ |
||
| 231 | |||
| 232 | /** |
||
| 233 | * FileHandler::write |
||
| 234 | * |
||
| 235 | * Write session data |
||
| 236 | * |
||
| 237 | * @link http://php.net/manual/en/sessionhandlerinterface.write.php |
||
| 238 | * |
||
| 239 | * @param string $session_id The session id. |
||
| 240 | * @param string $session_data <p> |
||
| 241 | * The encoded session data. This data is the |
||
| 242 | * result of the PHP internally encoding |
||
| 243 | * the $_SESSION superglobal to a serialized |
||
| 244 | * string and passing it as this parameter. |
||
| 245 | * Please note sessions use an alternative serialization method. |
||
| 246 | * </p> |
||
| 247 | * |
||
| 248 | * @return bool <p> |
||
| 249 | * The return value (usually TRUE on success, FALSE on failure). |
||
| 250 | * Note this value is returned internally to PHP for processing. |
||
| 251 | * </p> |
||
| 252 | * @since 5.4.0 |
||
| 253 | */ |
||
| 254 | public function write($session_id, $session_data) |
||
| 255 | {
|
||
| 256 | // If the two IDs don't match, we have a session_regenerate_id() call |
||
| 257 | // and we need to close the old handle and open a new one |
||
| 258 | if ($session_id !== $this->sessionId && ( ! $this->close() || $this->read($session_id) === false)) {
|
||
| 259 | return false; |
||
| 260 | } |
||
| 261 | |||
| 262 | if ( ! is_resource($this->file)) {
|
||
| 263 | return false; |
||
| 264 | } elseif ($this->fingerprint === md5($session_data)) {
|
||
| 265 | return ($this->isNewFile) |
||
| 266 | ? true |
||
| 267 | : touch($this->filePath . $session_id); |
||
| 268 | } |
||
| 269 | |||
| 270 | if ( ! $this->isNewFile) {
|
||
| 271 | ftruncate($this->file, 0); |
||
| 272 | rewind($this->file); |
||
| 273 | } |
||
| 274 | |||
| 275 | if (($length = strlen($session_data)) > 0) {
|
||
| 276 | for ($written = 0; $written < $length; $written += $result) {
|
||
| 277 | if (($result = fwrite($this->file, substr($session_data, $written))) === false) {
|
||
| 278 | break; |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | if ( ! is_int($result)) {
|
||
| 283 | $this->fingerprint = md5(substr($session_data, 0, $written)); |
||
| 284 | |||
| 285 | if ($this->logger instanceof LoggerInterface) {
|
||
| 286 | $this->logger->error('SESSION_E_FILE_ON_WRITE');
|
||
| 287 | } |
||
| 288 | |||
| 289 | return false; |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | $this->fingerprint = md5($session_data); |
||
| 294 | |||
| 295 | return true; |
||
| 296 | } |
||
| 297 | |||
| 298 | // ------------------------------------------------------------------------ |
||
| 299 | |||
| 300 | /** |
||
| 301 | * FileHandler::read |
||
| 302 | * |
||
| 303 | * Read session data |
||
| 304 | * |
||
| 305 | * @link http://php.net/manual/en/sessionhandlerinterface.read.php |
||
| 306 | * |
||
| 307 | * @param string $session_id The session id to read data for. |
||
| 308 | * |
||
| 309 | * @return string <p> |
||
| 310 | * Returns an encoded string of the read data. |
||
| 311 | * If nothing was read, it must return an empty string. |
||
| 312 | * Note this value is returned internally to PHP for processing. |
||
| 313 | * </p> |
||
| 314 | * @since 5.4.0 |
||
| 315 | */ |
||
| 316 | public function read($session_id) |
||
| 368 | } |
||
| 369 | } |