Conditions | 7 |
Paths | 10 |
Total Lines | 59 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 13 | ||
Bugs | 1 | 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 |
||
114 | public function showMediaAction(Request $request, Media $media, $filter = null) |
||
115 | { |
||
116 | $response = new Response(); |
||
117 | $lastModified = new \DateTime('now'); |
||
118 | |||
119 | //Checking if it is an image or not |
||
120 | $src = $this->get('alpixel_media.manager')->getAbsolutePath($media); |
||
121 | $isImage = @getimagesize($src); |
||
122 | |||
123 | if ($isImage) { |
||
124 | $response->headers->set('Content-disposition', 'inline;filename=' . $media->getName()); |
||
125 | if (!empty($filter) && $isImage) { |
||
126 | $src = $this->get('alpixel_media.manager')->getAbsolutePath($media, $filter); |
||
127 | $dataManager = $this->get('liip_imagine.data.manager'); // the data manager service |
||
128 | $filterManager = $this->get('liip_imagine.filter.manager'); // the filter manager service |
||
129 | $uploadDir = $this->get('alpixel_media.manager')->getUploadDir($filter); |
||
130 | |||
131 | if (!is_file($src)) { |
||
132 | $fs = new Filesystem(); |
||
133 | if (!$fs->exists($uploadDir . $media->getFolder())) { |
||
134 | $fs->mkdir($uploadDir . $media->getFolder()); |
||
135 | } |
||
136 | |||
137 | $path = 'upload/' . $media->getUri(); |
||
138 | |||
139 | // find the image and determine its type |
||
140 | $image = $dataManager->find($filter, $path); |
||
141 | |||
142 | // run the filter |
||
143 | $responseData = $filterManager->applyFilter($image, $filter); |
||
144 | $data = $responseData->getContent(); |
||
145 | file_put_contents($uploadDir . $media->getUri(), $data); |
||
146 | } else { |
||
147 | $data = file_get_contents($src); |
||
148 | $lastModified->setTimestamp(filemtime($src)); |
||
149 | } |
||
150 | } else { |
||
151 | $src = $this->get('alpixel_media.manager')->getAbsolutePath($media); |
||
152 | $lastModified->setTimestamp(filemtime($src)); |
||
153 | $data = file_get_contents($src); |
||
154 | } |
||
155 | } else { |
||
156 | $lastModified->setTimestamp(filemtime($src)); |
||
157 | $data = file_get_contents($src); |
||
158 | $response->headers->set('Content-disposition', 'attachment;filename=' . basename($media->getUri())); |
||
159 | } |
||
160 | |||
161 | $response->setLastModified($lastModified); |
||
162 | $response->setPublic(); |
||
163 | $response->headers->set('Content-Type', $media->getMime()); |
||
164 | |||
165 | if ($response->isNotModified($request)) { |
||
166 | return $response; |
||
167 | } |
||
168 | |||
169 | $response->setContent($data); |
||
170 | |||
171 | return $response; |
||
172 | } |
||
173 | } |
||
174 |
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: