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