| Total Complexity | 42 |
| Total Lines | 295 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 34 | final class FileHandler |
||
| 35 | { |
||
| 36 | const CHUNK_LENGTH = 8192; |
||
| 37 | const CHUNK_FACTOR = 3; |
||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $file; |
||
| 42 | /** |
||
| 43 | * @var resource |
||
| 44 | */ |
||
| 45 | protected $handle; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * FileHandler constructor. |
||
| 49 | * |
||
| 50 | * @param string $file |
||
| 51 | */ |
||
| 52 | public function __construct(string $file) |
||
| 53 | { |
||
| 54 | $this->file = $file; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Writes data into file |
||
| 59 | * |
||
| 60 | * @param $data |
||
| 61 | * |
||
| 62 | * @return FileHandler |
||
| 63 | * @throws FileException |
||
| 64 | */ |
||
| 65 | public function write($data) |
||
| 66 | { |
||
| 67 | if (!is_resource($this->handle)) { |
||
| 68 | $this->open('wb'); |
||
| 69 | } |
||
| 70 | |||
| 71 | if (@fwrite($this->handle, $data) === false) { |
||
|
|
|||
| 72 | throw new FileException(sprintf(__('No es posible escribir en el archivo (%s)'), $this->file)); |
||
| 73 | } |
||
| 74 | |||
| 75 | return $this; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Opens the file |
||
| 80 | * |
||
| 81 | * @param $mode |
||
| 82 | * |
||
| 83 | * @return resource |
||
| 84 | * @throws FileException |
||
| 85 | */ |
||
| 86 | public function open($mode = 'r') |
||
| 87 | { |
||
| 88 | if (($this->handle = @fopen($this->file, $mode)) === false) { |
||
| 89 | throw new FileException(sprintf(__('No es posible abrir el archivo (%s)'), $this->file)); |
||
| 90 | } |
||
| 91 | |||
| 92 | return $this->handle; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Reads data from file into a string |
||
| 97 | * |
||
| 98 | * @return string Data read from file |
||
| 99 | * @throws FileException |
||
| 100 | */ |
||
| 101 | public function readToString(): string |
||
| 102 | { |
||
| 103 | if (($data = file_get_contents($this->file)) === false) { |
||
| 104 | throw new FileException(sprintf(__('No es posible leer desde el archivo (%s)'), $this->file)); |
||
| 105 | } |
||
| 106 | |||
| 107 | return $data; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Reads data from file into an array |
||
| 112 | * |
||
| 113 | * @throws FileException |
||
| 114 | */ |
||
| 115 | public function readToArray(): array |
||
| 116 | { |
||
| 117 | if (($data = @file($this->file, FILE_SKIP_EMPTY_LINES)) === false) { |
||
| 118 | throw new FileException(sprintf(__('No es posible leer desde el archivo (%s)'), $this->file)); |
||
| 119 | } |
||
| 120 | |||
| 121 | return $data; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Reads data from file into a string |
||
| 126 | * |
||
| 127 | * @param string $data Data to write into file |
||
| 128 | * |
||
| 129 | * @return FileHandler |
||
| 130 | * @throws FileException |
||
| 131 | */ |
||
| 132 | public function save($data) |
||
| 133 | { |
||
| 134 | if (file_put_contents($this->file, $data, LOCK_EX) === false) { |
||
| 135 | throw new FileException(sprintf(__('No es posible escribir en el archivo (%s)'), $this->file)); |
||
| 136 | } |
||
| 137 | |||
| 138 | return $this; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Reads data from file |
||
| 143 | * |
||
| 144 | * @return string Data read from file |
||
| 145 | * @throws FileException |
||
| 146 | */ |
||
| 147 | public function read() |
||
| 148 | { |
||
| 149 | if (!is_resource($this->handle)) { |
||
| 150 | $this->open('rb'); |
||
| 151 | } |
||
| 152 | |||
| 153 | $data = ''; |
||
| 154 | |||
| 155 | while (!feof($this->handle)) { |
||
| 156 | $data .= fread($this->handle, self::CHUNK_LENGTH); |
||
| 157 | } |
||
| 158 | |||
| 159 | $this->close(); |
||
| 160 | |||
| 161 | return $data; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Closes the file |
||
| 166 | * |
||
| 167 | * @throws FileException |
||
| 168 | * @return FileHandler |
||
| 169 | */ |
||
| 170 | public function close() |
||
| 171 | { |
||
| 172 | if (!is_resource($this->handle) || @fclose($this->handle) === false) { |
||
| 173 | throw new FileException(sprintf(__('No es posible cerrar el archivo (%s)'), $this->file)); |
||
| 174 | } |
||
| 175 | |||
| 176 | return $this; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param callable $chunker |
||
| 181 | * @param float $rate |
||
| 182 | * |
||
| 183 | * @throws FileException |
||
| 184 | */ |
||
| 185 | public function readChunked(callable $chunker = null, float $rate = null) |
||
| 186 | { |
||
| 187 | $maxRate = Util::getMaxDownloadChunk() / self::CHUNK_FACTOR; |
||
| 188 | |||
| 189 | if ($rate === null || $rate > $maxRate) { |
||
| 190 | $rate = $maxRate; |
||
| 191 | } |
||
| 192 | |||
| 193 | if (!is_resource($this->handle)) { |
||
| 194 | $this->open('rb'); |
||
| 195 | } |
||
| 196 | |||
| 197 | while (!feof($this->handle)) { |
||
| 198 | if ($chunker !== null) { |
||
| 199 | $chunker(fread($this->handle, round($rate))); |
||
| 200 | } else { |
||
| 201 | print fread($this->handle, round($rate)); |
||
| 202 | ob_flush(); |
||
| 203 | flush(); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | $this->close(); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Checks if the file is writable |
||
| 212 | * |
||
| 213 | * @throws FileException |
||
| 214 | * @return FileHandler |
||
| 215 | */ |
||
| 216 | public function checkIsWritable() |
||
| 217 | { |
||
| 218 | if (!is_writable($this->file) && @touch($this->file) === false) { |
||
| 219 | throw new FileException(sprintf(__('No es posible escribir el archivo (%s)'), $this->file)); |
||
| 220 | } |
||
| 221 | |||
| 222 | return $this; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Checks if the file exists |
||
| 227 | * |
||
| 228 | * @throws FileException |
||
| 229 | * @return FileHandler |
||
| 230 | */ |
||
| 231 | public function checkFileExists() |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public function getFile(): string |
||
| 244 | { |
||
| 245 | return $this->file; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param bool $isExceptionOnZero |
||
| 250 | * |
||
| 251 | * @return int |
||
| 252 | * @throws FileException |
||
| 253 | */ |
||
| 254 | public function getFileSize($isExceptionOnZero = false): int |
||
| 255 | { |
||
| 256 | $size = filesize($this->file); |
||
| 257 | |||
| 258 | if ($size === false || ($isExceptionOnZero === true && $size === 0)) { |
||
| 259 | throw new FileException(sprintf(__('No es posible leer el archivo (%s)'), $this->file)); |
||
| 260 | } |
||
| 261 | |||
| 262 | return $size; |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Clears the stat cache for the given file |
||
| 267 | * |
||
| 268 | * @return FileHandler |
||
| 269 | */ |
||
| 270 | public function clearCache() |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Deletes a file |
||
| 279 | * |
||
| 280 | * @return FileHandler |
||
| 281 | * @throws FileException |
||
| 282 | */ |
||
| 283 | public function delete() |
||
| 284 | { |
||
| 285 | if (@unlink($this->file) === false) { |
||
| 286 | throw new FileException(sprintf(__('No es posible eliminar el archivo (%s)'), $this->file)); |
||
| 287 | } |
||
| 288 | |||
| 289 | return $this; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Returns the content type in MIME format |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | * @throws FileException |
||
| 297 | */ |
||
| 298 | public function getFileType(): string |
||
| 299 | { |
||
| 300 | $this->checkIsReadable(); |
||
| 301 | |||
| 302 | return mime_content_type($this->file); |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Checks if the file is readable |
||
| 307 | * |
||
| 308 | * @throws FileException |
||
| 309 | * @return FileHandler |
||
| 310 | */ |
||
| 311 | public function checkIsReadable() |
||
| 312 | { |
||
| 313 | if (!is_readable($this->file)) { |
||
| 314 | throw new FileException(sprintf(__('No es posible leer el archivo (%s)'), $this->file)); |
||
| 315 | } |
||
| 316 | |||
| 317 | return $this; |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @return int |
||
| 322 | * @throws FileException |
||
| 323 | */ |
||
| 324 | public function getFileTime(): int |
||
| 329 | } |
||
| 330 | } |