| Conditions | 8 |
| Paths | 58 |
| Total Lines | 52 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 25 | public function __invoke(CLink $link, Request $request): Response |
||
| 26 | { |
||
| 27 | $removeImage = $request->request->getBoolean('removeImage', false); |
||
| 28 | $file = $request->files->get('customImage'); |
||
| 29 | |||
| 30 | if ($removeImage) { |
||
| 31 | if ($link->getCustomImage()) { |
||
| 32 | $this->entityManager->remove($link->getCustomImage()); |
||
| 33 | $link->setCustomImage(null); |
||
| 34 | $this->entityManager->persist($link); |
||
| 35 | $this->entityManager->flush(); |
||
| 36 | |||
| 37 | if (!$file) { |
||
| 38 | return new Response('Image removed successfully', Response::HTTP_OK); |
||
| 39 | } |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | if (!$file || !$file->isValid()) { |
||
| 44 | return new Response('Invalid or missing file', Response::HTTP_BAD_REQUEST); |
||
| 45 | } |
||
| 46 | |||
| 47 | try { |
||
| 48 | $asset = new Asset(); |
||
| 49 | $asset->setFile($file) |
||
| 50 | ->setCategory(Asset::LINK) |
||
| 51 | ->setTitle($file->getClientOriginalName()); |
||
| 52 | |||
| 53 | $this->entityManager->persist($asset); |
||
| 54 | $this->entityManager->flush(); |
||
| 55 | |||
| 56 | $uploadedFilePath = $file->getPathname(); |
||
| 57 | |||
| 58 | $croppedFilePath = $this->cropImage($uploadedFilePath); |
||
| 59 | |||
| 60 | if (!file_exists($croppedFilePath)) { |
||
| 61 | @unlink($uploadedFilePath); |
||
|
|
|||
| 62 | return new Response('Error creating cropped image', Response::HTTP_INTERNAL_SERVER_ERROR); |
||
| 63 | } |
||
| 64 | |||
| 65 | $asset->setFile(new File($croppedFilePath)); |
||
| 66 | $this->entityManager->persist($asset); |
||
| 67 | $this->entityManager->flush(); |
||
| 68 | |||
| 69 | $link->setCustomImage($asset); |
||
| 70 | $this->entityManager->persist($link); |
||
| 71 | $this->entityManager->flush(); |
||
| 72 | |||
| 73 | return new Response('Image uploaded and linked successfully', Response::HTTP_OK); |
||
| 74 | |||
| 75 | } catch (\Exception $e) { |
||
| 76 | return new Response('Error processing image: ' . $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR); |
||
| 77 | } |
||
| 127 |
If you suppress an error, we recommend checking for the error condition explicitly: