| Conditions | 22 |
| Paths | 181 |
| Total Lines | 96 |
| Code Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 55 | public function execute(Request $request, WorkingFolder $workingFolder, EventDispatcher $dispatcher, Acl $acl, ResizedImageRepository $resizedImageRepository, ThumbnailRepository $thumbnailRepository, Config $config) |
||
| 56 | { |
||
| 57 | $fileName = (string) $request->get('fileName'); |
||
| 58 | $newFileName = (string) $request->get('newFileName'); |
||
| 59 | |||
| 60 | $editedImage = new EditedImage($fileName, $this->app, $newFileName); |
||
| 61 | |||
| 62 | $resourceType = $workingFolder->getResourceType(); |
||
| 63 | |||
| 64 | if (null === $newFileName) { |
||
|
|
|||
| 65 | $resourceTypeName = $resourceType->getName(); |
||
| 66 | $path = $workingFolder->getClientCurrentFolder(); |
||
| 67 | |||
| 68 | if (!$acl->isAllowed($resourceTypeName, $path, Permission::FILE_DELETE)) { |
||
| 69 | throw new UnauthorizedException(sprintf('Unauthorized: no FILE_DELETE permission in %s:%s', $resourceTypeName, $path)); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | if (!Image::isSupportedExtension($editedImage->getExtension())) { |
||
| 74 | throw new InvalidExtensionException('Unsupported image type or not image file'); |
||
| 75 | } |
||
| 76 | |||
| 77 | $image = Image::create($editedImage->getContents()); |
||
| 78 | |||
| 79 | $actions = (array) $request->get('actions'); |
||
| 80 | |||
| 81 | if (empty($actions)) { |
||
| 82 | throw new InvalidRequestException(); |
||
| 83 | } |
||
| 84 | |||
| 85 | foreach ($actions as $actionInfo) { |
||
| 86 | if (!isset($actionInfo['action'])) { |
||
| 87 | throw new InvalidRequestException('ImageEdit: action name missing'); |
||
| 88 | } |
||
| 89 | |||
| 90 | switch ($actionInfo['action']) { |
||
| 91 | case self::OPERATION_CROP: |
||
| 92 | if (!Utils::arrayContainsKeys($actionInfo, array('x', 'y', 'width', 'height'))) { |
||
| 93 | throw new InvalidRequestException(); |
||
| 94 | } |
||
| 95 | $x = $actionInfo['x']; |
||
| 96 | $y = $actionInfo['y']; |
||
| 97 | $width = $actionInfo['width']; |
||
| 98 | $height = $actionInfo['height']; |
||
| 99 | $image->crop($x, $y, $width, $height); |
||
| 100 | break; |
||
| 101 | |||
| 102 | case self::OPERATION_ROTATE: |
||
| 103 | if (!isset($actionInfo['angle'])) { |
||
| 104 | throw new InvalidRequestException(); |
||
| 105 | } |
||
| 106 | $degrees = $actionInfo['angle']; |
||
| 107 | $bgcolor = isset($actionInfo['bgcolor']) ? $actionInfo['bgcolor'] : 0; |
||
| 108 | $image->rotate($degrees, $bgcolor); |
||
| 109 | break; |
||
| 110 | |||
| 111 | case self::OPERATION_RESIZE: |
||
| 112 | if (!Utils::arrayContainsKeys($actionInfo, array('width', 'height'))) { |
||
| 113 | throw new InvalidRequestException(); |
||
| 114 | } |
||
| 115 | |||
| 116 | $imagesConfig = $config->get('images'); |
||
| 117 | |||
| 118 | $width = $imagesConfig['maxWidth'] && $actionInfo['width'] > $imagesConfig['maxWidth'] ? $imagesConfig['maxWidth'] : $actionInfo['width']; |
||
| 119 | $height = $imagesConfig['maxHeight'] && $actionInfo['height'] > $imagesConfig['maxHeight'] ? $imagesConfig['maxHeight'] : $actionInfo['height']; |
||
| 120 | $image->resize((int) $width, (int) $height, $imagesConfig['quality']); |
||
| 121 | break; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | $editFileEvent = new EditFileEvent($this->app, $editedImage); |
||
| 126 | |||
| 127 | $editedImage->setNewContents($image->getData()); |
||
| 128 | $editedImage->setNewDimensions($image->getWidth(), $image->getHeight()); |
||
| 129 | |||
| 130 | if (!$editedImage->isValid()) { |
||
| 131 | throw new InvalidUploadException('Invalid file provided'); |
||
| 132 | } |
||
| 133 | |||
| 134 | $dispatcher->dispatch(CKFinderEvent::EDIT_IMAGE, $editFileEvent); |
||
| 135 | |||
| 136 | $saved = false; |
||
| 137 | |||
| 138 | if (!$editFileEvent->isPropagationStopped()) { |
||
| 139 | $saved = $editedImage->save($editFileEvent->getNewContents()); |
||
| 140 | |||
| 141 | //Remove thumbnails and resized images in case if file is overwritten |
||
| 142 | if ($newFileName === null && $saved) { |
||
| 143 | $thumbnailRepository->deleteThumbnails($resourceType, $workingFolder->getClientCurrentFolder(), $fileName); |
||
| 144 | $resizedImageRepository->deleteResizedImages($resourceType, $workingFolder->getClientCurrentFolder(), $fileName); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | return array( |
||
| 149 | 'saved' => (int) $saved, |
||
| 150 | 'date' => Utils::formatDate(time()) |
||
| 151 | ); |
||
| 154 |