| Total Complexity | 47 |
| Total Lines | 265 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ImageManager 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.
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 ImageManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | class ImageManager { |
||
| 48 | public const SupportedImageKeys = ['background', 'logo', 'logoheader', 'favicon']; |
||
| 49 | |||
| 50 | /** @var IConfig */ |
||
| 51 | private $config; |
||
| 52 | /** @var IAppData */ |
||
| 53 | private $appData; |
||
| 54 | /** @var IURLGenerator */ |
||
| 55 | private $urlGenerator; |
||
| 56 | /** @var ICacheFactory */ |
||
| 57 | private $cacheFactory; |
||
| 58 | /** @var ILogger */ |
||
| 59 | private $logger; |
||
| 60 | /** @var ITempManager */ |
||
| 61 | private $tempManager; |
||
| 62 | |||
| 63 | public function __construct(IConfig $config, |
||
| 64 | IAppData $appData, |
||
| 65 | IURLGenerator $urlGenerator, |
||
| 66 | ICacheFactory $cacheFactory, |
||
| 67 | ILogger $logger, |
||
| 68 | ITempManager $tempManager) { |
||
| 69 | $this->config = $config; |
||
| 70 | $this->urlGenerator = $urlGenerator; |
||
| 71 | $this->cacheFactory = $cacheFactory; |
||
| 72 | $this->logger = $logger; |
||
| 73 | $this->tempManager = $tempManager; |
||
| 74 | $this->appData = $appData; |
||
| 75 | } |
||
| 76 | |||
| 77 | public function getImageUrl(string $key, bool $useSvg = true): string { |
||
|
|
|||
| 78 | $cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0'); |
||
| 79 | if ($this->hasImage($key)) { |
||
| 80 | return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => $key ]) . '?v=' . $cacheBusterCounter; |
||
| 81 | } |
||
| 82 | |||
| 83 | switch ($key) { |
||
| 84 | case 'logo': |
||
| 85 | case 'logoheader': |
||
| 86 | case 'favicon': |
||
| 87 | return $this->urlGenerator->imagePath('core', 'logo/logo.png') . '?v=' . $cacheBusterCounter; |
||
| 88 | case 'background': |
||
| 89 | return $this->urlGenerator->imagePath('core', 'background.png') . '?v=' . $cacheBusterCounter; |
||
| 90 | } |
||
| 91 | return ''; |
||
| 92 | } |
||
| 93 | |||
| 94 | public function getImageUrlAbsolute(string $key, bool $useSvg = true): string { |
||
| 95 | return $this->urlGenerator->getAbsoluteURL($this->getImageUrl($key, $useSvg)); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $key |
||
| 100 | * @param bool $useSvg |
||
| 101 | * @return ISimpleFile |
||
| 102 | * @throws NotFoundException |
||
| 103 | * @throws NotPermittedException |
||
| 104 | */ |
||
| 105 | public function getImage(string $key, bool $useSvg = true): ISimpleFile { |
||
| 106 | $logo = $this->config->getAppValue('theming', $key . 'Mime', ''); |
||
| 107 | $folder = $this->getRootFolder()->getFolder('images'); |
||
| 108 | |||
| 109 | if ($logo === '' || !$folder->fileExists($key)) { |
||
| 110 | throw new NotFoundException(); |
||
| 111 | } |
||
| 112 | |||
| 113 | if (!$useSvg && $this->shouldReplaceIcons()) { |
||
| 114 | if (!$folder->fileExists($key . '.png')) { |
||
| 115 | try { |
||
| 116 | $finalIconFile = new \Imagick(); |
||
| 117 | $finalIconFile->setBackgroundColor('none'); |
||
| 118 | $finalIconFile->readImageBlob($folder->getFile($key)->getContent()); |
||
| 119 | $finalIconFile->setImageFormat('png32'); |
||
| 120 | $pngFile = $folder->newFile($key . '.png'); |
||
| 121 | $pngFile->putContent($finalIconFile->getImageBlob()); |
||
| 122 | return $pngFile; |
||
| 123 | } catch (\ImagickException $e) { |
||
| 124 | $this->logger->info('The image was requested to be no SVG file, but converting it to PNG failed: ' . $e->getMessage()); |
||
| 125 | } |
||
| 126 | } else { |
||
| 127 | return $folder->getFile($key . '.png'); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | return $folder->getFile($key); |
||
| 132 | } |
||
| 133 | |||
| 134 | public function hasImage(string $key): bool { |
||
| 135 | $mimeSetting = $this->config->getAppValue('theming', $key . 'Mime', ''); |
||
| 136 | return $mimeSetting !== ''; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Get folder for current theming files |
||
| 141 | * |
||
| 142 | * @return ISimpleFolder |
||
| 143 | * @throws NotPermittedException |
||
| 144 | */ |
||
| 145 | public function getCacheFolder(): ISimpleFolder { |
||
| 146 | $cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0'); |
||
| 147 | try { |
||
| 148 | $folder = $this->getRootFolder()->getFolder($cacheBusterValue); |
||
| 149 | } catch (NotFoundException $e) { |
||
| 150 | $folder = $this->getRootFolder()->newFolder($cacheBusterValue); |
||
| 151 | $this->cleanup(); |
||
| 152 | } |
||
| 153 | return $folder; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Get a file from AppData |
||
| 158 | * |
||
| 159 | * @param string $filename |
||
| 160 | * @throws NotFoundException |
||
| 161 | * @return \OCP\Files\SimpleFS\ISimpleFile |
||
| 162 | * @throws NotPermittedException |
||
| 163 | */ |
||
| 164 | public function getCachedImage(string $filename): ISimpleFile { |
||
| 165 | $currentFolder = $this->getCacheFolder(); |
||
| 166 | return $currentFolder->getFile($filename); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Store a file for theming in AppData |
||
| 171 | * |
||
| 172 | * @param string $filename |
||
| 173 | * @param string $data |
||
| 174 | * @return \OCP\Files\SimpleFS\ISimpleFile |
||
| 175 | * @throws NotFoundException |
||
| 176 | * @throws NotPermittedException |
||
| 177 | */ |
||
| 178 | public function setCachedImage(string $filename, string $data): ISimpleFile { |
||
| 179 | $currentFolder = $this->getCacheFolder(); |
||
| 180 | if ($currentFolder->fileExists($filename)) { |
||
| 181 | $file = $currentFolder->getFile($filename); |
||
| 182 | } else { |
||
| 183 | $file = $currentFolder->newFile($filename); |
||
| 184 | } |
||
| 185 | $file->putContent($data); |
||
| 186 | return $file; |
||
| 187 | } |
||
| 188 | |||
| 189 | public function delete(string $key): void { |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | public function updateImage(string $key, string $tmpFile): string { |
||
| 206 | $this->delete($key); |
||
| 207 | |||
| 208 | try { |
||
| 209 | $folder = $this->getRootFolder()->getFolder('images'); |
||
| 210 | } catch (NotFoundException $e) { |
||
| 211 | $folder = $this->getRootFolder()->newFolder('images'); |
||
| 212 | } |
||
| 213 | |||
| 214 | $target = $folder->newFile($key); |
||
| 215 | $supportedFormats = $this->getSupportedUploadImageFormats($key); |
||
| 216 | $detectedMimeType = mime_content_type($tmpFile); |
||
| 217 | if (!in_array($detectedMimeType, $supportedFormats, true)) { |
||
| 218 | throw new \Exception('Unsupported image type'); |
||
| 219 | } |
||
| 220 | |||
| 221 | if ($key === 'background' && strpos($detectedMimeType, 'image/svg') === false && strpos($detectedMimeType, 'image/gif') === false) { |
||
| 222 | // Optimize the image since some people may upload images that will be |
||
| 223 | // either to big or are not progressive rendering. |
||
| 224 | $newImage = @imagecreatefromstring(file_get_contents($tmpFile)); |
||
| 225 | |||
| 226 | // Preserve transparency |
||
| 227 | imagesavealpha($newImage, true); |
||
| 228 | imagealphablending($newImage, true); |
||
| 229 | |||
| 230 | $tmpFile = $this->tempManager->getTemporaryFile(); |
||
| 231 | $newWidth = (int)(imagesx($newImage) < 4096 ? imagesx($newImage) : 4096); |
||
| 232 | $newHeight = (int)(imagesy($newImage) / (imagesx($newImage) / $newWidth)); |
||
| 233 | $outputImage = imagescale($newImage, $newWidth, $newHeight); |
||
| 234 | |||
| 235 | imageinterlace($outputImage, 1); |
||
| 236 | imagepng($outputImage, $tmpFile, 8); |
||
| 237 | imagedestroy($outputImage); |
||
| 238 | |||
| 239 | $target->putContent(file_get_contents($tmpFile)); |
||
| 240 | } else { |
||
| 241 | $target->putContent(file_get_contents($tmpFile)); |
||
| 242 | } |
||
| 243 | |||
| 244 | return $detectedMimeType; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Returns a list of supported mime types for image uploads. |
||
| 249 | * "favicon" images are only allowed to be SVG when imagemagick with SVG support is available. |
||
| 250 | * |
||
| 251 | * @param string $key The image key, e.g. "favicon" |
||
| 252 | * @return string[] |
||
| 253 | */ |
||
| 254 | private function getSupportedUploadImageFormats(string $key): array { |
||
| 255 | $supportedFormats = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']; |
||
| 256 | |||
| 257 | if ($key !== 'favicon' || $this->shouldReplaceIcons() === true) { |
||
| 258 | $supportedFormats[] = 'image/svg+xml'; |
||
| 259 | $supportedFormats[] = 'image/svg'; |
||
| 260 | } |
||
| 261 | |||
| 262 | if ($key === 'favicon') { |
||
| 263 | $supportedFormats[] = 'image/x-icon'; |
||
| 264 | $supportedFormats[] = 'image/vnd.microsoft.icon'; |
||
| 265 | } |
||
| 266 | |||
| 267 | return $supportedFormats; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * remove cached files that are not required any longer |
||
| 272 | * |
||
| 273 | * @throws NotPermittedException |
||
| 274 | * @throws NotFoundException |
||
| 275 | */ |
||
| 276 | public function cleanup() { |
||
| 277 | $currentFolder = $this->getCacheFolder(); |
||
| 278 | $folders = $this->getRootFolder()->getDirectoryListing(); |
||
| 279 | foreach ($folders as $folder) { |
||
| 280 | if ($folder->getName() !== 'images' && $folder->getName() !== $currentFolder->getName()) { |
||
| 281 | $folder->delete(); |
||
| 282 | } |
||
| 283 | } |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Check if Imagemagick is enabled and if SVG is supported |
||
| 288 | * otherwise we can't render custom icons |
||
| 289 | * |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | public function shouldReplaceIcons() { |
||
| 305 | } |
||
| 306 | |||
| 307 | private function getRootFolder(): ISimpleFolder { |
||
| 312 | } |
||
| 313 | } |
||
| 314 | } |
||
| 315 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.