Complex classes like MediaController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MediaController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class MediaController extends Controller |
||
|
|||
24 | { |
||
25 | /** |
||
26 | * @param Request $request |
||
27 | * @param int $mediaId |
||
28 | * |
||
29 | * @Route("/{mediaId}", requirements={"mediaId" = "\d+"}, name="KunstmaanMediaBundle_media_show") |
||
30 | * |
||
31 | * @return Response |
||
32 | */ |
||
33 | public function showAction(Request $request, $mediaId) |
||
77 | |||
78 | /** |
||
79 | * @param Request $request |
||
80 | * @param int $mediaId |
||
81 | * |
||
82 | * @Route("/delete/{mediaId}", requirements={"mediaId" = "\d+"}, name="KunstmaanMediaBundle_media_delete") |
||
83 | * |
||
84 | * @return RedirectResponse |
||
85 | */ |
||
86 | public function deleteAction(Request $request, $mediaId) |
||
118 | |||
119 | /** |
||
120 | * @param int $folderId |
||
121 | * |
||
122 | * @Route("bulkupload/{folderId}", requirements={"folderId" = "\d+"}, name="KunstmaanMediaBundle_media_bulk_upload") |
||
123 | * @Template("@KunstmaanMedia/Media/bulkUpload.html.twig") |
||
124 | * |
||
125 | * @return array|RedirectResponse |
||
126 | */ |
||
127 | public function bulkUploadAction($folderId) |
||
136 | |||
137 | /** |
||
138 | * @param Request $request |
||
139 | * @param int $folderId |
||
140 | * |
||
141 | * @Route("bulkuploadsubmit/{folderId}", requirements={"folderId" = "\d+"}, name="KunstmaanMediaBundle_media_bulk_upload_submit") |
||
142 | * |
||
143 | * @return JsonResponse |
||
144 | */ |
||
145 | public function bulkUploadSubmitAction(Request $request, $folderId) |
||
146 | { |
||
147 | // Settings |
||
148 | if (\ini_get('upload_tmp_dir')) { |
||
149 | $tempDir = \ini_get('upload_tmp_dir'); |
||
150 | } else { |
||
151 | $tempDir = \sys_get_temp_dir(); |
||
152 | } |
||
153 | $targetDir = \rtrim($tempDir, '/').DIRECTORY_SEPARATOR.'plupload'; |
||
154 | $cleanupTargetDir = true; // Remove old files |
||
155 | $maxFileAge = 5 * 60 * 60; // Temp file age in seconds |
||
156 | |||
157 | // Create target dir |
||
158 | if (!\file_exists($targetDir)) { |
||
159 | @\mkdir($targetDir); |
||
160 | } |
||
161 | |||
162 | // Get a file name |
||
163 | if ($request->request->has('name')) { |
||
164 | $fileName = $request->request->get('name'); |
||
165 | } elseif (0 !== $request->files->count()) { |
||
166 | $fileName = $request->files->get('file')['name']; |
||
167 | } else { |
||
168 | $fileName = \uniqid('file_', false); |
||
169 | } |
||
170 | $filePath = $targetDir.DIRECTORY_SEPARATOR.$fileName; |
||
171 | |||
172 | $chunk = 0; |
||
173 | $chunks = 0; |
||
174 | // Chunking might be enabled |
||
175 | if ($request->request->has('chunk')) { |
||
176 | $chunk = $request->request->getInt('chunk'); |
||
177 | } |
||
178 | if ($request->request->has('chunks')) { |
||
179 | $chunks = $request->request->getInt('chunks'); |
||
180 | } |
||
181 | |||
182 | // Remove old temp files |
||
183 | if ($cleanupTargetDir) { |
||
184 | if (!\is_dir($targetDir) || !$dir = \opendir($targetDir)) { |
||
185 | return $this->returnJsonError('100', 'Failed to open temp directory.'); |
||
186 | } |
||
187 | |||
188 | while (($file = \readdir($dir)) !== false) { |
||
189 | $tmpFilePath = $targetDir.DIRECTORY_SEPARATOR.$file; |
||
190 | |||
191 | // If temp file is current file proceed to the next |
||
192 | if ($tmpFilePath === "{$filePath}.part") { |
||
193 | continue; |
||
194 | } |
||
195 | |||
196 | // Remove temp file if it is older than the max age and is not the current file |
||
197 | if (\preg_match('/\.part$/', $file) && (\filemtime($tmpFilePath) < \time() - $maxFileAge)) { |
||
198 | $success = @\unlink($tmpFilePath); |
||
199 | if ($success !== true) { |
||
200 | return $this->returnJsonError('106', 'Could not remove temp file: '.$filePath); |
||
201 | } |
||
202 | } |
||
203 | } |
||
204 | \closedir($dir); |
||
205 | } |
||
206 | |||
207 | // Open temp file |
||
208 | if (!$out = @\fopen("{$filePath}.part", $chunks ? 'ab' : 'wb')) { |
||
209 | return $this->returnJsonError('102', 'Failed to open output stream.'); |
||
210 | } |
||
211 | |||
212 | if (0 !== $request->files->count()) { |
||
213 | $_file = $request->files->get('file'); |
||
214 | if ($_file->getError() > 0 || !\is_uploaded_file($_file->getRealPath())) { |
||
215 | return $this->returnJsonError('103', 'Failed to move uploaded file.'); |
||
216 | } |
||
217 | |||
218 | // Read binary input stream and append it to temp file |
||
219 | if (!$input = @\fopen($_file->getRealPath(), 'rb')) { |
||
220 | return $this->returnJsonError('101', 'Failed to open input stream.'); |
||
221 | } |
||
222 | } else { |
||
223 | if (!$input = @\fopen('php://input', 'rb')) { |
||
224 | return $this->returnJsonError('101', 'Failed to open input stream.'); |
||
225 | } |
||
226 | } |
||
227 | |||
228 | while ($buff = \fread($input, 4096)) { |
||
229 | \fwrite($out, $buff); |
||
230 | } |
||
231 | |||
232 | @\fclose($out); |
||
233 | @\fclose($input); |
||
234 | |||
235 | // Check if file has been uploaded |
||
236 | if (!$chunks || $chunk === $chunks - 1) { |
||
237 | // Strip the temp .part suffix off |
||
238 | \rename("{$filePath}.part", $filePath); |
||
239 | } |
||
240 | |||
241 | $em = $this->getDoctrine()->getManager(); |
||
242 | /* @var Folder $folder */ |
||
243 | $folder = $em->getRepository(Folder::class)->getFolder($folderId); |
||
244 | $file = new File($filePath); |
||
245 | |||
246 | if (!$this->get('kunstmaan_media.media_manager')->isExtensionAllowed($file)) { |
||
247 | return $this->returnJsonError('104', 'This file type is not allowed on the server'); |
||
248 | } |
||
249 | |||
250 | try { |
||
251 | /* @var Media $media */ |
||
252 | $media = $this->get('kunstmaan_media.media_manager')->getHandler($file)->createNew($file); |
||
253 | $media->setFolder($folder); |
||
254 | $em->getRepository(Media::class)->save($media); |
||
255 | } catch (Exception $e) { |
||
256 | return $this->returnJsonError('104', 'Failed performing save on media-manager'); |
||
257 | } |
||
258 | |||
259 | $success = \unlink($filePath); |
||
260 | if ($success !== true) { |
||
261 | return $this->returnJsonError('105', 'Could not remove temp file: '.$filePath); |
||
262 | } |
||
263 | |||
264 | // Send headers making sure that the file is not cached (as it happens for example on iOS devices) |
||
265 | $response = new JsonResponse( |
||
266 | [ |
||
267 | 'jsonrpc' => '2.0', |
||
268 | 'result' => '', |
||
269 | 'id' => 'id', |
||
270 | ], JsonResponse::HTTP_OK, [ |
||
271 | 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT', |
||
272 | 'Last-Modified' => \gmdate('D, d M Y H:i:s').' GMT', |
||
273 | 'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', |
||
274 | 'Pragma' => 'no-cache', |
||
275 | ] |
||
276 | ); |
||
277 | |||
278 | return $response; |
||
279 | } |
||
280 | |||
281 | private function returnJsonError($code, $message) |
||
282 | { |
||
283 | return new JsonResponse( |
||
284 | [ |
||
285 | 'jsonrpc' => '2.0', |
||
286 | 'error ' => [ |
||
287 | 'code' => $code, |
||
288 | 'message' => $message, |
||
289 | ], |
||
290 | 'id' => 'id', |
||
291 | ] |
||
292 | ); |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * @param Request $request |
||
297 | * @param int $folderId |
||
298 | * |
||
299 | * @Route("drop/{folderId}", requirements={"folderId" = "\d+"}, name="KunstmaanMediaBundle_media_drop_upload", methods={"GET", "POST"}) |
||
300 | * |
||
301 | * @return JsonResponse |
||
302 | */ |
||
303 | public function dropAction(Request $request, $folderId) |
||
304 | { |
||
305 | $em = $this->getDoctrine()->getManager(); |
||
306 | |||
307 | /* @var Folder $folder */ |
||
308 | $folder = $em->getRepository(Folder::class)->getFolder($folderId); |
||
309 | |||
310 | $drop = null; |
||
311 | |||
312 | if ($request->files->has('files') && $request->files->get('files')['error'] === 0) { |
||
313 | $drop = $request->files->get('files'); |
||
314 | } else { |
||
315 | if ($request->files->get('file')) { |
||
316 | $drop = $request->files->get('file'); |
||
317 | } else { |
||
318 | $drop = $request->get('text'); |
||
319 | } |
||
320 | } |
||
321 | $mediaManager = $this->get('kunstmaan_media.media_manager'); |
||
322 | $media = $mediaManager->createNew($drop); |
||
323 | if ($media) { |
||
324 | $media->setFolder($folder); |
||
325 | $em->getRepository(Media::class)->save($media); |
||
326 | |||
327 | return new JsonResponse(['status' => $this->get('translator')->trans('kuma_admin.media.flash.drop_success')]); |
||
328 | } |
||
329 | |||
330 | if (!$mediaManager->isExtensionAllowed($drop)) { |
||
331 | $request->getSession()->getFlashBag()->add( |
||
332 | FlashTypes::DANGER, |
||
333 | $this->get('translator')->trans('media.flash.not_valid') |
||
334 | ); |
||
335 | |||
336 | return new JsonResponse(['status' => $this->get('translator')->trans('kuma_admin.media.flash.drop_unrecognized')]); |
||
337 | } |
||
338 | |||
339 | $request->getSession()->getFlashBag()->add( |
||
340 | FlashTypes::DANGER, |
||
341 | $this->get('translator')->trans('kuma_admin.media.flash.drop_unrecognized') |
||
342 | ); |
||
343 | |||
344 | return new JsonResponse(['status' => $this->get('translator')->trans('media.flash.not_valid')]); |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * @param Request $request |
||
349 | * @param int $folderId The folder id |
||
350 | * @param string $type The type |
||
351 | * |
||
352 | * @Route("create/{folderId}/{type}", requirements={"folderId" = "\d+", "type" = ".+"}, name="KunstmaanMediaBundle_media_create", methods={"GET", "POST"}) |
||
353 | * @Template("@KunstmaanMedia/Media/create.html.twig") |
||
354 | * |
||
355 | * @return array|RedirectResponse |
||
356 | */ |
||
357 | public function createAction(Request $request, $folderId, $type) |
||
358 | { |
||
359 | return $this->createAndRedirect($request, $folderId, $type, 'KunstmaanMediaBundle_folder_show'); |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * @param Request $request |
||
364 | * @param int $folderId The folder Id |
||
365 | * @param string $type The type |
||
366 | * @param string $redirectUrl The url where we want to redirect to on success |
||
367 | * @param array $extraParams The extra parameters that will be passed wen redirecting |
||
368 | * |
||
369 | * @return array|RedirectResponse |
||
370 | */ |
||
371 | private function createAndRedirect(Request $request, $folderId, $type, $redirectUrl, $extraParams = [], $isInModal = false) |
||
372 | { |
||
373 | $em = $this->getDoctrine()->getManager(); |
||
374 | |||
375 | /* @var Folder $folder */ |
||
376 | $folder = $em->getRepository(Folder::class)->getFolder($folderId); |
||
377 | |||
378 | /* @var MediaManager $mediaManager */ |
||
379 | $mediaManager = $this->get('kunstmaan_media.media_manager'); |
||
380 | $handler = $mediaManager->getHandlerForType($type); |
||
381 | $media = new Media(); |
||
382 | $helper = $handler->getFormHelper($media); |
||
383 | |||
384 | $form = $this->createForm($handler->getFormType(), $helper, $handler->getFormTypeOptions()); |
||
385 | |||
386 | if ($request->isMethod('POST')) { |
||
387 | $params = ['folderId' => $folder->getId()]; |
||
388 | $params = \array_merge($params, $extraParams); |
||
389 | |||
390 | $form->handleRequest($request); |
||
391 | |||
392 | if ($form->isSubmitted() && $form->isValid()) { |
||
393 | if ($mediaManager->isExtensionAllowed($helper->getFile())) { |
||
394 | $media = $helper->getMedia(); |
||
395 | $media->setFolder($folder); |
||
396 | $em->getRepository(Media::class)->save($media); |
||
397 | |||
398 | $this->addFlash( |
||
399 | FlashTypes::SUCCESS, |
||
400 | $this->get('translator')->trans( |
||
401 | 'media.flash.created', |
||
402 | [ |
||
403 | '%medianame%' => $media->getName(), |
||
404 | ] |
||
405 | ) |
||
406 | ); |
||
407 | |||
408 | return new RedirectResponse($this->generateUrl($redirectUrl, $params)); |
||
409 | } |
||
410 | |||
411 | $this->addFlash( |
||
412 | FlashTypes::DANGER, |
||
413 | $this->get('translator')->trans( |
||
414 | 'media.flash.not_valid', |
||
415 | [ |
||
416 | '%medianame%' => $media->getName(), |
||
417 | ] |
||
418 | ) |
||
419 | ); |
||
420 | |||
421 | return new RedirectResponse($this->generateUrl($redirectUrl, $params)); |
||
422 | } |
||
423 | |||
424 | if ($isInModal) { |
||
425 | $this->addFlash( |
||
426 | FlashTypes::DANGER, |
||
427 | $this->get('translator')->trans( |
||
428 | 'media.flash.not_created', |
||
429 | [ |
||
430 | '%mediaerrors%' => $form->getErrors(true, true), |
||
431 | ] |
||
432 | ) |
||
433 | ); |
||
434 | |||
435 | return new RedirectResponse($this->generateUrl($redirectUrl, $params)); |
||
436 | } |
||
437 | } |
||
438 | |||
439 | return [ |
||
440 | 'type' => $type, |
||
441 | 'form' => $form->createView(), |
||
442 | 'folder' => $folder, |
||
443 | ]; |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * @param Request $request |
||
448 | * @param int $folderId The folder id |
||
449 | * @param string $type The type |
||
450 | * |
||
451 | * @Route("create/modal/{folderId}/{type}", requirements={"folderId" = "\d+", "type" = ".+"}, name="KunstmaanMediaBundle_media_modal_create", methods={"POST"}) |
||
452 | * |
||
453 | * @return array|RedirectResponse |
||
454 | */ |
||
455 | public function createModalAction(Request $request, $folderId, $type) |
||
477 | |||
478 | /** |
||
479 | * @param Request $request |
||
480 | * |
||
481 | * @Route("move/", name="KunstmaanMediaBundle_media_move", methods={"POST"}) |
||
482 | * |
||
483 | * @return string |
||
484 | */ |
||
485 | public function moveMedia(Request $request) |
||
507 | |||
508 | /** |
||
509 | * @Route("/bulk-move", name="KunstmaanMediaBundle_media_bulk_move") |
||
510 | * |
||
511 | * @param Request $request |
||
512 | * |
||
513 | * @return JsonResponse|Response |
||
514 | * |
||
515 | * @throws \Doctrine\DBAL\DBALException |
||
516 | */ |
||
517 | public function bulkMoveAction(Request $request) |
||
554 | } |
||
555 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.