| Conditions | 16 |
| Paths | 36 |
| Total Lines | 124 |
| Code Lines | 66 |
| 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 |
||
| 248 | public function download($filename = null) |
||
| 249 | { |
||
| 250 | $filename = isset($filename) ? $filename : $this->fileinfo[ 'basename' ]; |
||
| 251 | |||
| 252 | if ($this->partialRequest) { |
||
| 253 | if ($this->resumeable) { |
||
| 254 | // Turn on resume capability |
||
| 255 | output() |
||
| 256 | ->sendHeaderStatus(206, 'Partial Content', '1.0') |
||
| 257 | ->sendHeader('Status', '206 Partial Content') |
||
| 258 | ->sendHeader('Accept-Ranges', 'bytes'); |
||
| 259 | |||
| 260 | output()->sendHeader('Content-range', |
||
| 261 | 'bytes ' . $this->seekStart . '-' . $this->seekEnd . '/' . $this->filesize); |
||
| 262 | } else { |
||
| 263 | // Turn off resume capability |
||
| 264 | $this->seekStart = 0; |
||
| 265 | $this->seekEnd = $this->filesize - 1; |
||
| 266 | $this->seekFileSize = $this->filesize; |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | // Common Download Headers content type, content disposition, content length and last modified |
||
| 271 | output() |
||
| 272 | ->sendHeader('Content-Type', $this->filemime) |
||
| 273 | ->sendHeader('Content-Disposition', 'attachment; filename=' . $filename) |
||
| 274 | ->sendHeader('Content-Length', $this->seekFileSize) |
||
| 275 | ->sendHeader('Last-Modified', date('D, d M Y H:i:s \G\M\T', $this->lastModified)); |
||
| 276 | // End Headers Stage |
||
| 277 | |||
| 278 | // Work On Download Speed Limit |
||
| 279 | if ($this->speedLimit) { |
||
| 280 | // how many buffers ticks per second |
||
| 281 | $bufferTicks = 10; //10 |
||
| 282 | // how long one buffering tick takes by micro second |
||
| 283 | $bufferMicroTime = 150; // 100 |
||
| 284 | // Calculate sleep micro time after each tick |
||
| 285 | $sleepMicroTime = round((1000000 - ($bufferTicks * $bufferMicroTime)) / $bufferTicks); |
||
| 286 | // Calculate required buffer per one tick, make sure it is integer so round the result |
||
| 287 | $this->bufferSize = round($this->speedLimit * 1024 / $bufferTicks); |
||
| 288 | } |
||
| 289 | // Immediatly Before Downloading |
||
| 290 | // clean any output buffer |
||
| 291 | @ob_end_clean(); |
||
| 292 | |||
| 293 | // get oignore_user_abort value, then change it to yes |
||
| 294 | $oldUserAbortSetting = ignore_user_abort(); |
||
| 295 | ignore_user_abort(true); |
||
| 296 | // set script execution time to be unlimited |
||
| 297 | @set_time_limit(0); |
||
| 298 | |||
| 299 | |||
| 300 | // Download According Download Mode |
||
| 301 | if ($this->mode === self::MODE_FILESTREAM) { |
||
| 302 | // Download Data by fopen |
||
| 303 | $downloadFileBytes = $this->seekFileSize; |
||
| 304 | $downloaded = 0; |
||
| 305 | // goto the position of the first byte to download |
||
| 306 | fseek($this->filedata, $this->seekStart); |
||
| 307 | while ($downloadFileBytes > 0 && ! (connection_aborted() || connection_status() == 1)) { |
||
| 308 | // still Downloading |
||
| 309 | if ($downloadFileBytes > $this->bufferSize) { |
||
| 310 | // send buffer size |
||
| 311 | echo fread($this->filedata, $this->bufferSize); // this also will seek to after last read byte |
||
| 312 | $downloaded += $this->bufferSize; // updated downloaded |
||
| 313 | $downloadFileBytes -= $this->bufferSize; // update remaining bytes |
||
| 314 | } else { |
||
| 315 | // send required size |
||
| 316 | // this will happens when we reaches the end of the file normally we wll download remaining bytes |
||
| 317 | echo fread($this->filedata, $downloadFileBytes); // this also will seek to last reat |
||
| 318 | |||
| 319 | $downloaded += $downloadFileBytes; // Add to downloaded |
||
| 320 | |||
| 321 | |||
| 322 | $downloadFileBytes = 0; // Here last bytes have been written |
||
| 323 | } |
||
| 324 | // send to buffer |
||
| 325 | flush(); |
||
| 326 | // Check For Download Limit |
||
| 327 | if ($this->speedLimit) { |
||
| 328 | usleep($sleepMicroTime); |
||
| 329 | } |
||
| 330 | |||
| 331 | |||
| 332 | } |
||
| 333 | // all bytes have been sent to user |
||
| 334 | // Close File |
||
| 335 | fclose($this->filedata); |
||
| 336 | } elseif ($this->mode === self::MODE_DATASTREAM) { |
||
| 337 | // Download Data String |
||
| 338 | $downloadFileBytes = $this->seekFileSize; |
||
| 339 | |||
| 340 | $downloaded = 0; |
||
| 341 | $offset = $this->seekStart; |
||
| 342 | while ($downloadFileBytes > 0 && ( ! connection_aborted())) { |
||
| 343 | if ($downloadFileBytes > $this->bufferSize) { |
||
| 344 | // Download by buffer |
||
| 345 | echo mb_strcut($this->filedata, $offset, $this->bufferSize); |
||
| 346 | $downloadFileBytes -= $this->bufferSize; |
||
| 347 | $downloaded += $this->bufferSize; |
||
| 348 | $offset += $this->bufferSize; |
||
| 349 | } else { |
||
| 350 | // download last bytes |
||
| 351 | echo mb_strcut($this->filedata, $offset, $downloadFileBytes); |
||
| 352 | $downloaded += $downloadFileBytes; |
||
| 353 | $offset += $downloadFileBytes; |
||
| 354 | $downloadFileBytes = 0; |
||
| 355 | } |
||
| 356 | // Send Data to Buffer |
||
| 357 | flush(); |
||
| 358 | // Check Limit |
||
| 359 | if ($this->speedLimit) { |
||
| 360 | usleep($sleepMicroTime); |
||
| 361 | } |
||
| 362 | |||
| 363 | } |
||
| 364 | } |
||
| 365 | |||
| 366 | // Set Downloaded Bytes |
||
| 367 | $this->downloadedFileSize = $downloaded; |
||
| 368 | ignore_user_abort($oldUserAbortSetting); // Restore old user abort settings |
||
| 369 | set_time_limit(ini_get('max_execution_time')); // Restore Default script max execution Time |
||
| 370 | |||
| 371 | exit; |
||
| 372 | } |
||
| 406 | } |
If you suppress an error, we recommend checking for the error condition explicitly: