| Conditions | 9 |
| Paths | 42 |
| Total Lines | 67 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| 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 |
||
| 158 | public function showMediaAction(Request $request, Media $media, $filter = null) |
||
| 159 | { |
||
| 160 | $response = new Response(); |
||
| 161 | $lastModified = new \DateTime('now'); |
||
| 162 | |||
| 163 | //Checking if it is an image or not |
||
| 164 | $src = $this->get('alpixel_media.manager')->getAbsolutePath($media); |
||
| 165 | $isImage = @getimagesize($src); |
||
| 166 | |||
| 167 | try { |
||
| 168 | if ($isImage) { |
||
| 169 | $response->headers->set('Content-disposition', 'inline;filename='.$media->getName()); |
||
| 170 | if (!empty($filter) && $isImage) { |
||
| 171 | $src = $this->get('alpixel_media.manager')->getAbsolutePath($media, $filter); |
||
| 172 | $dataManager = $this->get('liip_imagine.data.manager'); // the data manager service |
||
| 173 | $filterManager = $this->get('liip_imagine.filter.manager'); // the filter manager service |
||
| 174 | $uploadDir = $this->get('alpixel_media.manager')->getUploadDir($filter); |
||
| 175 | |||
| 176 | if (!is_file($src)) { |
||
| 177 | $fs = new Filesystem(); |
||
| 178 | if (!$fs->exists($uploadDir.$media->getFolder())) { |
||
| 179 | $fs->mkdir($uploadDir.$media->getFolder()); |
||
| 180 | } |
||
| 181 | |||
| 182 | $path = 'upload/'.$media->getUri(); |
||
| 183 | |||
| 184 | // find the image and determine its type |
||
| 185 | $image = $dataManager->find($filter, $path); |
||
| 186 | |||
| 187 | // run the filter |
||
| 188 | $responseData = $filterManager->applyFilter($image, $filter); |
||
| 189 | $data = $responseData->getContent(); |
||
| 190 | file_put_contents($uploadDir.$media->getUri(), $data); |
||
| 191 | } else { |
||
| 192 | $data = file_get_contents($src); |
||
| 193 | $lastModified->setTimestamp(filemtime($src)); |
||
| 194 | } |
||
| 195 | } else { |
||
| 196 | $src = $this->get('alpixel_media.manager')->getAbsolutePath($media); |
||
| 197 | $lastModified->setTimestamp(filemtime($src)); |
||
| 198 | $data = file_get_contents($src); |
||
| 199 | } |
||
| 200 | } else { |
||
| 201 | $lastModified->setTimestamp(filemtime($src)); |
||
| 202 | $data = file_get_contents($src); |
||
| 203 | $response->headers->set('Content-disposition', 'attachment;filename='.basename($media->getUri())); |
||
| 204 | } |
||
| 205 | |||
| 206 | if ($data === false) { |
||
| 207 | throw $this->createNotFoundException(); |
||
| 208 | } |
||
| 209 | } catch (\Exception $e) { |
||
| 210 | throw $this->createNotFoundException($e->getMessage()); |
||
| 211 | } |
||
| 212 | |||
| 213 | $response->setLastModified($lastModified); |
||
| 214 | $response->setPublic(); |
||
| 215 | $response->headers->set('Content-Type', $media->getMime()); |
||
| 216 | |||
| 217 | if ($response->isNotModified($request)) { |
||
| 218 | return $response; |
||
| 219 | } |
||
| 220 | |||
| 221 | $response->setContent($data); |
||
| 222 | |||
| 223 | return $response; |
||
| 224 | } |
||
| 225 | } |
||
| 226 |
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: