| Conditions | 6 |
| Paths | 7 |
| Total Lines | 54 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 70 | public function upload( |
||
| 71 | Request $request, |
||
| 72 | SerializerInterface $serializer, |
||
| 73 | string $gpxDirectory, // TODO Fix this; it should be using the image uploads directory |
||
| 74 | MessageBusInterface $messageBus, |
||
| 75 | UploaderHelper $uploaderHelper |
||
| 76 | ): Response |
||
| 77 | { |
||
| 78 | if ($request->isMethod('POST')) { |
||
| 79 | |||
| 80 | $token = $request->request->get('token'); |
||
| 81 | if (!$this->isCsrfTokenValid('image_upload', $token)) { |
||
| 82 | return $this->json([ 'error' => 'Invalid CSRF token'], 401); |
||
| 83 | } |
||
| 84 | |||
| 85 | $file = $request->files->get('file'); |
||
| 86 | if (!$file instanceof UploadedFile) { |
||
| 87 | throw new HttpException(500, "No uploaded file found."); |
||
| 88 | } |
||
| 89 | |||
| 90 | $image = new Image(); |
||
| 91 | $image->setImageFile($file); |
||
| 92 | $entityManager = $this->getDoctrine()->getManager(); |
||
| 93 | $entityManager->persist($image); |
||
| 94 | $entityManager->flush(); |
||
| 95 | |||
| 96 | // Queue up some image recognition |
||
| 97 | $id = $image->getId(); |
||
| 98 | if ($id !== null) { |
||
|
|
|||
| 99 | $messageBus->dispatch(new RecogniseImage($id)); |
||
| 100 | } |
||
| 101 | |||
| 102 | // And warm up the image cache the new image |
||
| 103 | $imagePath = $uploaderHelper->asset($image); |
||
| 104 | if ($imagePath !== null) { |
||
| 105 | $messageBus->dispatch(new WarmImageCache($imagePath)); |
||
| 106 | } |
||
| 107 | |||
| 108 | // It's not exactly an API response, but it'll do until we switch to handling this |
||
| 109 | // a bit more properly. At least it's a JSON repsonse and *doesn't include the entire |
||
| 110 | // file we just uploaded*, thanks to the IGNORED_ATTRIBUTES. Because we set up the |
||
| 111 | // image URIs in a postPersist event listener, this also contains everything you'd |
||
| 112 | // need to build an image in HTML. |
||
| 113 | return new JsonResponse($serializer->serialize($image, 'jsonld', [AbstractNormalizer::IGNORED_ATTRIBUTES => ['imageFile']]), 201,[], true); |
||
| 114 | } |
||
| 115 | |||
| 116 | // Normal GET request. |
||
| 117 | $disk = []; |
||
| 118 | $disk['free'] = disk_free_space($gpxDirectory); |
||
| 119 | $disk['total'] = disk_total_space($gpxDirectory); |
||
| 120 | $disk['used'] = $disk['total'] - $disk['free']; |
||
| 121 | $disk['percent'] = $disk['used'] / $disk['total']; |
||
| 122 | return $this->render('admin/image/upload.html.twig', [ |
||
| 123 | 'disk' => $disk |
||
| 124 | ]); |
||
| 203 |