| Total Complexity | 45 |
| Total Lines | 362 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PreviewManager 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 PreviewManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class PreviewManager implements IPreview { |
||
| 42 | /** @var IConfig */ |
||
| 43 | protected $config; |
||
| 44 | |||
| 45 | /** @var IRootFolder */ |
||
| 46 | protected $rootFolder; |
||
| 47 | |||
| 48 | /** @var IAppData */ |
||
| 49 | protected $appData; |
||
| 50 | |||
| 51 | /** @var EventDispatcherInterface */ |
||
| 52 | protected $eventDispatcher; |
||
| 53 | |||
| 54 | /** @var Generator */ |
||
| 55 | private $generator; |
||
| 56 | |||
| 57 | /** @var GeneratorHelper */ |
||
| 58 | private $helper; |
||
| 59 | |||
| 60 | /** @var bool */ |
||
| 61 | protected $providerListDirty = false; |
||
| 62 | |||
| 63 | /** @var bool */ |
||
| 64 | protected $registeredCoreProviders = false; |
||
| 65 | |||
| 66 | /** @var array */ |
||
| 67 | protected $providers = []; |
||
| 68 | |||
| 69 | /** @var array mime type => support status */ |
||
| 70 | protected $mimeTypeSupportMap = []; |
||
| 71 | |||
| 72 | /** @var array */ |
||
| 73 | protected $defaultProviders; |
||
| 74 | |||
| 75 | /** @var string */ |
||
| 76 | protected $userId; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * PreviewManager constructor. |
||
| 80 | * |
||
| 81 | * @param IConfig $config |
||
| 82 | * @param IRootFolder $rootFolder |
||
| 83 | * @param IAppData $appData |
||
| 84 | * @param EventDispatcherInterface $eventDispatcher |
||
| 85 | * @param string $userId |
||
| 86 | */ |
||
| 87 | public function __construct(IConfig $config, |
||
| 88 | IRootFolder $rootFolder, |
||
| 89 | IAppData $appData, |
||
| 90 | EventDispatcherInterface $eventDispatcher, |
||
| 91 | GeneratorHelper $helper, |
||
| 92 | $userId) { |
||
| 93 | $this->config = $config; |
||
| 94 | $this->rootFolder = $rootFolder; |
||
| 95 | $this->appData = $appData; |
||
| 96 | $this->eventDispatcher = $eventDispatcher; |
||
| 97 | $this->helper = $helper; |
||
| 98 | $this->userId = $userId; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * In order to improve lazy loading a closure can be registered which will be |
||
| 103 | * called in case preview providers are actually requested |
||
| 104 | * |
||
| 105 | * $callable has to return an instance of \OCP\Preview\IProvider or \OCP\Preview\IProviderV2 |
||
| 106 | * |
||
| 107 | * @param string $mimeTypeRegex Regex with the mime types that are supported by this provider |
||
| 108 | * @param \Closure $callable |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | public function registerProvider($mimeTypeRegex, \Closure $callable) { |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Get all providers |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | public function getProviders() { |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Does the manager have any providers |
||
| 144 | * @return bool |
||
| 145 | */ |
||
| 146 | public function hasProviders() { |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Returns a preview of a file |
||
| 153 | * |
||
| 154 | * The cache is searched first and if nothing usable was found then a preview is |
||
| 155 | * generated by one of the providers |
||
| 156 | * |
||
| 157 | * @param File $file |
||
| 158 | * @param int $width |
||
| 159 | * @param int $height |
||
| 160 | * @param bool $crop |
||
| 161 | * @param string $mode |
||
| 162 | * @param string $mimeType |
||
| 163 | * @return ISimpleFile |
||
| 164 | * @throws NotFoundException |
||
| 165 | * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
||
| 166 | * @since 11.0.0 - \InvalidArgumentException was added in 12.0.0 |
||
| 167 | */ |
||
| 168 | public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { |
||
| 169 | if ($this->generator === null) { |
||
| 170 | $this->generator = new Generator( |
||
| 171 | $this->config, |
||
| 172 | $this, |
||
| 173 | $this->appData, |
||
| 174 | new GeneratorHelper( |
||
| 175 | $this->rootFolder, |
||
| 176 | $this->config |
||
| 177 | ), |
||
| 178 | $this->eventDispatcher |
||
| 179 | ); |
||
| 180 | } |
||
| 181 | |||
| 182 | return $this->generator->getPreview($file, $width, $height, $crop, $mode, $mimeType); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * returns true if the passed mime type is supported |
||
| 187 | * |
||
| 188 | * @param string $mimeType |
||
| 189 | * @return boolean |
||
| 190 | */ |
||
| 191 | public function isMimeSupported($mimeType = '*') { |
||
| 192 | if (!$this->config->getSystemValue('enable_previews', true)) { |
||
| 193 | return false; |
||
| 194 | } |
||
| 195 | |||
| 196 | if (isset($this->mimeTypeSupportMap[$mimeType])) { |
||
| 197 | return $this->mimeTypeSupportMap[$mimeType]; |
||
| 198 | } |
||
| 199 | |||
| 200 | $this->registerCoreProviders(); |
||
| 201 | $providerMimeTypes = array_keys($this->providers); |
||
| 202 | foreach ($providerMimeTypes as $supportedMimeType) { |
||
| 203 | if (preg_match($supportedMimeType, $mimeType)) { |
||
| 204 | $this->mimeTypeSupportMap[$mimeType] = true; |
||
| 205 | return true; |
||
| 206 | } |
||
| 207 | } |
||
| 208 | $this->mimeTypeSupportMap[$mimeType] = false; |
||
| 209 | return false; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Check if a preview can be generated for a file |
||
| 214 | * |
||
| 215 | * @param \OCP\Files\FileInfo $file |
||
| 216 | * @return bool |
||
| 217 | */ |
||
| 218 | public function isAvailable(\OCP\Files\FileInfo $file) { |
||
| 219 | if (!$this->config->getSystemValue('enable_previews', true)) { |
||
| 220 | return false; |
||
| 221 | } |
||
| 222 | |||
| 223 | $this->registerCoreProviders(); |
||
| 224 | if (!$this->isMimeSupported($file->getMimetype())) { |
||
| 225 | return false; |
||
| 226 | } |
||
| 227 | |||
| 228 | $mount = $file->getMountPoint(); |
||
| 229 | if ($mount and !$mount->getOption('previews', true)){ |
||
| 230 | return false; |
||
| 231 | } |
||
| 232 | |||
| 233 | foreach ($this->providers as $supportedMimeType => $providers) { |
||
| 234 | if (preg_match($supportedMimeType, $file->getMimetype())) { |
||
| 235 | foreach ($providers as $providerClosure) { |
||
| 236 | $provider = $this->helper->getProvider($providerClosure); |
||
| 237 | if (!($provider instanceof IProviderV2)) { |
||
| 238 | continue; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** @var $provider IProvider */ |
||
| 242 | if ($provider->isAvailable($file)) { |
||
| 243 | return true; |
||
| 244 | } |
||
| 245 | } |
||
| 246 | } |
||
| 247 | } |
||
| 248 | return false; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * List of enabled default providers |
||
| 253 | * |
||
| 254 | * The following providers are enabled by default: |
||
| 255 | * - OC\Preview\PNG |
||
| 256 | * - OC\Preview\JPEG |
||
| 257 | * - OC\Preview\GIF |
||
| 258 | * - OC\Preview\BMP |
||
| 259 | * - OC\Preview\HEIC |
||
| 260 | * - OC\Preview\XBitmap |
||
| 261 | * - OC\Preview\MarkDown |
||
| 262 | * - OC\Preview\MP3 |
||
| 263 | * - OC\Preview\TXT |
||
| 264 | * |
||
| 265 | * The following providers are disabled by default due to performance or privacy concerns: |
||
| 266 | * - OC\Preview\Font |
||
| 267 | * - OC\Preview\Illustrator |
||
| 268 | * - OC\Preview\Movie |
||
| 269 | * - OC\Preview\MSOfficeDoc |
||
| 270 | * - OC\Preview\MSOffice2003 |
||
| 271 | * - OC\Preview\MSOffice2007 |
||
| 272 | * - OC\Preview\OpenDocument |
||
| 273 | * - OC\Preview\PDF |
||
| 274 | * - OC\Preview\Photoshop |
||
| 275 | * - OC\Preview\Postscript |
||
| 276 | * - OC\Preview\StarOffice |
||
| 277 | * - OC\Preview\SVG |
||
| 278 | * - OC\Preview\TIFF |
||
| 279 | * |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | protected function getEnabledDefaultProvider() { |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Register the default providers (if enabled) |
||
| 311 | * |
||
| 312 | * @param string $class |
||
| 313 | * @param string $mimeType |
||
| 314 | */ |
||
| 315 | protected function registerCoreProvider($class, $mimeType, $options = []) { |
||
| 316 | if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
||
| 317 | $this->registerProvider($mimeType, function () use ($class, $options) { |
||
| 318 | return new $class($options); |
||
| 319 | }); |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Register the default providers (if enabled) |
||
| 325 | */ |
||
| 326 | protected function registerCoreProviders() { |
||
| 403 | } |
||
| 404 | } |
||
| 405 | } |
||
| 406 | } |
||
| 407 |