| Conditions | 21 |
| Paths | 480 |
| Total Lines | 107 |
| Code Lines | 69 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
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 namespace crocodicstudio\crudbooster\controllers; |
||
| 11 | public function getPreview($one, $two = null, $three = null, $four = null, $five = null) |
||
| 12 | { |
||
| 13 | |||
| 14 | if ($two) { |
||
| 15 | $fullFilePath = 'uploads'.DIRECTORY_SEPARATOR.$one.DIRECTORY_SEPARATOR.$two; |
||
| 16 | $filename = $two; |
||
| 17 | if ($three) { |
||
| 18 | $fullFilePath = 'uploads'.DIRECTORY_SEPARATOR.$one.DIRECTORY_SEPARATOR.$two.DIRECTORY_SEPARATOR.$three; |
||
| 19 | $filename = $three; |
||
| 20 | if ($four) { |
||
| 21 | $fullFilePath = 'uploads'.DIRECTORY_SEPARATOR.$one.DIRECTORY_SEPARATOR.$two.DIRECTORY_SEPARATOR.$three.DIRECTORY_SEPARATOR.$four; |
||
| 22 | $filename = $four; |
||
| 23 | if ($five) { |
||
| 24 | $fullFilePath = 'uploads'.DIRECTORY_SEPARATOR.$one.DIRECTORY_SEPARATOR.$two.DIRECTORY_SEPARATOR.$three.DIRECTORY_SEPARATOR.$four.DIRECTORY_SEPARATOR.$five; |
||
| 25 | $filename = $five; |
||
| 26 | } |
||
| 27 | } |
||
| 28 | } |
||
| 29 | } else { |
||
| 30 | $fullFilePath = 'uploads'.DIRECTORY_SEPARATOR.$one; |
||
| 31 | $filename = $one; |
||
| 32 | } |
||
| 33 | |||
| 34 | $fullStoragePath = storage_path('app/'.$fullFilePath); |
||
| 35 | $lifetime = 31556926; // One year in seconds |
||
| 36 | |||
| 37 | |||
| 38 | |||
| 39 | if (! Storage::exists($fullFilePath)) { |
||
| 40 | abort(404); |
||
| 41 | } |
||
| 42 | |||
| 43 | $handler = new \Symfony\Component\HttpFoundation\File\File(storage_path('app/'.$fullFilePath)); |
||
| 44 | |||
| 45 | $extension = strtolower(File::extension($fullStoragePath)); |
||
| 46 | $images_ext = config('crudbooster.IMAGE_EXTENSIONS', 'jpg,png,gif,bmp'); |
||
| 47 | $images_ext = explode(',', $images_ext); |
||
| 48 | $imageFileSize = 0; |
||
| 49 | |||
| 50 | if (in_array($extension, $images_ext)) { |
||
| 51 | $defaultThumbnail = config('crudbooster.DEFAULT_THUMBNAIL_WIDTH'); |
||
| 52 | if ($defaultThumbnail != 0) { |
||
| 53 | $w = Request::get('w') ?: $defaultThumbnail; |
||
| 54 | $h = Request::get('h') ?: $w; |
||
| 55 | } else { |
||
| 56 | $w = Request::get('w'); |
||
| 57 | $h = Request::get('h') ?: $w; |
||
| 58 | } |
||
| 59 | |||
| 60 | $imgRaw = Image::cache(function ($image) use ($fullStoragePath, $w, $h) { |
||
| 61 | $im = $image->make($fullStoragePath); |
||
| 62 | if ($w) { |
||
| 63 | if (! $h) { |
||
| 64 | $im->fit($w); |
||
| 65 | } else { |
||
| 66 | $im->fit($w, $h); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | return $im; |
||
| 71 | }); |
||
| 72 | |||
| 73 | $imageFileSize = mb_strlen($imgRaw, '8bit') ?: 0; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Prepare some header variables |
||
| 78 | */ |
||
| 79 | $file_time = $handler->getMTime(); // Get the last modified time for the file (Unix timestamp) |
||
| 80 | |||
| 81 | $header_content_type = $handler->getMimeType(); |
||
| 82 | $header_content_length = ($imageFileSize) ?: $handler->getSize(); |
||
| 83 | $header_etag = md5($file_time.$fullFilePath); |
||
| 84 | $header_last_modified = gmdate('r', $file_time); |
||
| 85 | $header_expires = gmdate('r', $file_time + $lifetime); |
||
| 86 | |||
| 87 | $headers = [ |
||
| 88 | 'Content-Disposition' => 'inline; filename="'.$filename.'"', |
||
| 89 | 'Last-Modified' => $header_last_modified, |
||
| 90 | 'Cache-Control' => 'must-revalidate', |
||
| 91 | 'Expires' => $header_expires, |
||
| 92 | 'Pragma' => 'public', |
||
| 93 | 'Etag' => $header_etag, |
||
| 94 | ]; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Is the resource cached? |
||
| 98 | */ |
||
| 99 | $h1 = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $header_last_modified; |
||
| 100 | $h2 = isset($_SERVER['HTTP_IF_NONE_MATCH']) && str_replace('"', '', stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])) == $header_etag; |
||
| 101 | |||
| 102 | $headers = array_merge($headers, [ |
||
| 103 | 'Content-Type' => $header_content_type, |
||
| 104 | 'Content-Length' => $header_content_length, |
||
| 105 | ]); |
||
| 106 | |||
| 107 | if (in_array($extension, $images_ext)) { |
||
| 108 | if ($h1 || $h2) { |
||
| 109 | return Response::make('', 304, $headers); // File (image) is cached by the browser, so we don't have to send it again |
||
| 110 | } else { |
||
| 111 | return Response::make($imgRaw, 200, $headers); |
||
|
|
|||
| 112 | } |
||
| 113 | } else { |
||
| 114 | if (Request::get('download')) { |
||
| 115 | return Response::download(storage_path('app/'.$fullFilePath), $filename, $headers); |
||
| 116 | } else { |
||
| 117 | return Response::file(storage_path('app/'.$fullFilePath), $headers); |
||
| 118 | } |
||
| 122 |