| Conditions | 7 |
| Paths | 6 |
| Total Lines | 86 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 10 | ||
| Bugs | 1 | 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 |
||
| 37 | public function getImage(Request $request) |
||
|
|
|||
| 38 | { |
||
| 39 | /** |
||
| 40 | * Check the cache |
||
| 41 | */ |
||
| 42 | $cacheKey = 'image:' . $request->imageName . ':' . $request->imageExtension; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * File cached |
||
| 46 | */ |
||
| 47 | if (Cache::has($cacheKey)) { |
||
| 48 | |||
| 49 | $imageMeta = Cache::get($cacheKey); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * File not cached |
||
| 54 | */ |
||
| 55 | else{ |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Get META information |
||
| 59 | */ |
||
| 60 | $imageMeta = Image::where(['url' => $request->imageName, 'image_extension' => $request->imageExtension])->first(['image_mime_type', 'image_size', 'id', 'updated_at', 'image_etag']); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * File does not exist |
||
| 64 | */ |
||
| 65 | if(empty($imageMeta) == TRUE){ |
||
| 66 | App::abort(404); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Save meta information to cache |
||
| 71 | */ |
||
| 72 | Cache::forever($cacheKey, $imageMeta); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Get filename |
||
| 77 | */ |
||
| 78 | $filename = Helpers::getStorageFilename(env('APP_IMAGE_STORAGE_DIRECTORY', 'images'), $imageMeta->id); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Prepare stream |
||
| 82 | */ |
||
| 83 | $stream = Storage::readStream($filename); |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * File headers |
||
| 88 | */ |
||
| 89 | $headers = array( |
||
| 90 | 'Content-Description' => 'File Transfer', |
||
| 91 | 'Content-Type' => $imageMeta->image_mime_type, |
||
| 92 | 'Content-Transfer-Encoding' => 'binary', |
||
| 93 | 'Pragma' => 'public', |
||
| 94 | 'Expires' => Carbon::createFromTimestamp(time()+3600)->toRfc2822String(), |
||
| 95 | 'Last-Modified' => $imageMeta->updated_at->toRfc2822String(), |
||
| 96 | 'Etag' => $imageMeta->image_etag, |
||
| 97 | ); |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Response code cached |
||
| 101 | */ |
||
| 102 | if( (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $imageMeta->image_etag) |
||
| 103 | || (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && @$_SERVER['HTTP_IF_MODIFIED_SINCE'] == $imageMeta->updated_at->toRfc2822String()) ){ |
||
| 104 | |||
| 105 | $responseCode = 304; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Response code not cached, but OK |
||
| 110 | */ |
||
| 111 | else{ |
||
| 112 | |||
| 113 | $responseCode = 200; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Stream to browser |
||
| 118 | */ |
||
| 119 | return Response::stream(function() use ($stream) { |
||
| 120 | fpassthru($stream); |
||
| 121 | }, $responseCode, $headers); |
||
| 122 | } |
||
| 123 | } |
||
| 124 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: