| Total Complexity | 61 |
| Total Lines | 476 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like OptimizedImages 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 OptimizedImages, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class OptimizedImages extends Field |
||
| 46 | { |
||
| 47 | // Constants |
||
| 48 | // ========================================================================= |
||
| 49 | |||
| 50 | const DEFAULT_ASPECT_RATIOS = [ |
||
| 51 | ['x' => 16, 'y' => 9], |
||
| 52 | ]; |
||
| 53 | const DEFAULT_IMAGE_VARIANTS = [ |
||
| 54 | [ |
||
| 55 | 'width' => 1200, |
||
| 56 | 'useAspectRatio' => true, |
||
| 57 | 'aspectRatioX' => 16.0, |
||
| 58 | 'aspectRatioY' => 9.0, |
||
| 59 | 'retinaSizes' => ['1'], |
||
| 60 | 'quality' => 82, |
||
| 61 | 'format' => 'jpg', |
||
| 62 | ], |
||
| 63 | ]; |
||
| 64 | |||
| 65 | // Public Properties |
||
| 66 | // ========================================================================= |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | public $fieldVolumeSettings = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | public $ignoreFilesOfType = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var bool |
||
| 80 | */ |
||
| 81 | public $displayOptimizedImageVariants = true; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var bool |
||
| 85 | */ |
||
| 86 | public $displayDominantColorPalette = true; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | public $displayLazyLoadPlaceholderImages = true; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | public $variants = []; |
||
| 97 | |||
| 98 | // Private Properties |
||
| 99 | // ========================================================================= |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var array |
||
| 103 | */ |
||
| 104 | private $aspectRatios = []; |
||
| 105 | |||
| 106 | // Static Methods |
||
| 107 | // ========================================================================= |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @inheritdoc |
||
| 111 | */ |
||
| 112 | public function __construct(array $config = []) |
||
| 113 | { |
||
| 114 | // Unset any deprecated properties |
||
| 115 | if (!empty($config)) { |
||
| 116 | unset($config['transformMethod'], $config['imgixDomain']); |
||
| 117 | } |
||
| 118 | parent::__construct($config); |
||
| 119 | } |
||
| 120 | |||
| 121 | // Public Methods |
||
| 122 | // ========================================================================= |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @inheritdoc |
||
| 126 | */ |
||
| 127 | public static function displayName(): string |
||
| 128 | { |
||
| 129 | return 'OptimizedImages'; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @inheritdoc |
||
| 134 | */ |
||
| 135 | public function init() |
||
| 136 | { |
||
| 137 | parent::init(); |
||
| 138 | |||
| 139 | // Handle cases where the plugin has been uninstalled |
||
| 140 | if (ImageOptimize::$plugin !== null) { |
||
| 141 | $settings = ImageOptimize::$plugin->getSettings(); |
||
| 142 | if ($settings) { |
||
| 143 | if (empty($this->variants)) { |
||
| 144 | $this->variants = $settings->defaultVariants; |
||
| 145 | } |
||
| 146 | $this->aspectRatios = $settings->defaultAspectRatios; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | // If the user has deleted all default aspect ratios, provide a fallback |
||
| 150 | if (empty($this->aspectRatios)) { |
||
| 151 | $this->aspectRatios = self::DEFAULT_ASPECT_RATIOS; |
||
| 152 | } |
||
| 153 | // If the user has deleted all default variants, provide a fallback |
||
| 154 | if (empty($this->variants)) { |
||
| 155 | $this->variants = self::DEFAULT_IMAGE_VARIANTS; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @inheritdoc |
||
| 161 | */ |
||
| 162 | public function rules() |
||
| 163 | { |
||
| 164 | $rules = parent::rules(); |
||
| 165 | $rules = array_merge($rules, [ |
||
| 166 | [ |
||
| 167 | [ |
||
| 168 | 'displayOptimizedImageVariants', |
||
| 169 | 'displayDominantColorPalette', |
||
| 170 | 'displayLazyLoadPlaceholderImages', |
||
| 171 | ], |
||
| 172 | 'boolean', |
||
| 173 | ], |
||
| 174 | [ |
||
| 175 | [ |
||
| 176 | 'ignoreFilesOfType', |
||
| 177 | 'variants', |
||
| 178 | ], |
||
| 179 | ArrayValidator::class |
||
| 180 | ], |
||
| 181 | ]); |
||
| 182 | |||
| 183 | return $rules; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @inheritdoc |
||
| 188 | * @since 1.6.2 |
||
| 189 | */ |
||
| 190 | public function getContentGqlType() |
||
| 198 | ]; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @inheritdoc |
||
| 203 | * @since 1.7.0 |
||
| 204 | */ |
||
| 205 | public function useFieldset(): bool |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @inheritdoc |
||
| 212 | */ |
||
| 213 | public function afterElementSave(ElementInterface $asset, bool $isNew) |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @inheritdoc |
||
| 246 | */ |
||
| 247 | public function normalizeValue($value, ElementInterface $asset = null) |
||
| 248 | { |
||
| 249 | // If we're passed in a string, assume it's JSON-encoded, and decode it |
||
| 250 | if (\is_string($value) && !empty($value)) { |
||
| 251 | $value = Json::decodeIfJson($value); |
||
| 252 | } |
||
| 253 | // If we're passed in an array, make a model from it |
||
| 254 | if (\is_array($value)) { |
||
| 255 | // Create a new OptimizedImage model and populate it |
||
| 256 | $model = new OptimizedImage($value); |
||
| 257 | } elseif ($value instanceof OptimizedImage) { |
||
| 258 | $model = $value; |
||
| 259 | } else { |
||
| 260 | // Just create a new empty model |
||
| 261 | $model = new OptimizedImage(null); |
||
| 262 | } |
||
| 263 | |||
| 264 | return $model; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @inheritdoc |
||
| 269 | */ |
||
| 270 | public function getContentColumnType(): string |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @inheritdoc |
||
| 277 | */ |
||
| 278 | public function getSettingsHtml() |
||
| 279 | { |
||
| 280 | $namespace = Craft::$app->getView()->getNamespace(); |
||
| 281 | if (strpos($namespace, Matrix::class) !== false || strpos($namespace, SuperTableField::class) !== false) { |
||
| 282 | // Render an error template, since the field only works when attached to an Asset |
||
| 283 | try { |
||
| 284 | return Craft::$app->getView()->renderTemplate( |
||
| 285 | 'image-optimize/_components/fields/OptimizedImages_error', |
||
| 286 | [ |
||
| 287 | ] |
||
| 288 | ); |
||
| 289 | } catch (\Twig\Error\LoaderError $e) { |
||
| 290 | Craft::error($e->getMessage(), __METHOD__); |
||
| 291 | } catch (\yii\base\Exception $e) { |
||
| 292 | Craft::error($e->getMessage(), __METHOD__); |
||
| 293 | } |
||
| 294 | } |
||
| 295 | // Register our asset bundle |
||
| 296 | try { |
||
| 297 | Craft::$app->getView()->registerAssetBundle(ImageOptimizeAsset::class); |
||
| 298 | } catch (InvalidConfigException $e) { |
||
| 299 | Craft::error($e->getMessage(), __METHOD__); |
||
| 300 | } |
||
| 301 | |||
| 302 | try { |
||
| 303 | $reflect = new \ReflectionClass($this); |
||
| 304 | $thisId = $reflect->getShortName(); |
||
| 305 | } catch (\ReflectionException $e) { |
||
| 306 | Craft::error($e->getMessage(), __METHOD__); |
||
| 307 | $thisId = 0; |
||
| 308 | } |
||
| 309 | // Get our id and namespace |
||
| 310 | if (ImageOptimize::$craft35) { |
||
| 311 | $id = Html::id($thisId); |
||
| 312 | } else { |
||
| 313 | $id = Craft::$app->getView()->formatInputId($thisId); |
||
| 314 | } |
||
| 315 | $namespacedId = Craft::$app->getView()->namespaceInputId($id); |
||
| 316 | $namespacePrefix = Craft::$app->getView()->namespaceInputName($thisId); |
||
| 317 | $sizesWrapperId = Craft::$app->getView()->namespaceInputId('sizes-wrapper'); |
||
| 318 | Craft::$app->getView()->registerJs('new Craft.OptimizedImagesInput('. |
||
| 319 | '"'.$namespacedId.'", '. |
||
| 320 | '"'.$namespacePrefix.'",'. |
||
| 321 | '"'.$sizesWrapperId.'"'. |
||
| 322 | ');'); |
||
| 323 | |||
| 324 | // Prep our aspect ratios |
||
| 325 | $aspectRatios = []; |
||
| 326 | $index = 1; |
||
| 327 | foreach ($this->aspectRatios as $aspectRatio) { |
||
| 328 | if ($index % 6 === 0) { |
||
| 329 | $aspectRatio['break'] = true; |
||
| 330 | } |
||
| 331 | $aspectRatios[] = $aspectRatio; |
||
| 332 | $index++; |
||
| 333 | } |
||
| 334 | $aspectRatio = ['x' => 2, 'y' => 2, 'custom' => true]; |
||
| 335 | $aspectRatios[] = $aspectRatio; |
||
| 336 | // Get only the user-editable settings |
||
| 337 | $settings = ImageOptimize::$plugin->getSettings(); |
||
| 338 | |||
| 339 | // Render the settings template |
||
| 340 | try { |
||
| 341 | return Craft::$app->getView()->renderTemplate( |
||
| 342 | 'image-optimize/_components/fields/OptimizedImages_settings', |
||
| 343 | [ |
||
| 344 | 'field' => $this, |
||
| 345 | 'settings' => $settings, |
||
| 346 | 'aspectRatios' => $aspectRatios, |
||
| 347 | 'id' => $id, |
||
| 348 | 'name' => $this->handle, |
||
| 349 | 'namespace' => $namespacedId, |
||
| 350 | 'fieldVolumes' => $this->getFieldVolumeInfo($this->handle), |
||
| 351 | ] |
||
| 352 | ); |
||
| 353 | } catch (\Twig\Error\LoaderError $e) { |
||
| 354 | Craft::error($e->getMessage(), __METHOD__); |
||
| 355 | } catch (\yii\base\Exception $e) { |
||
| 356 | Craft::error($e->getMessage(), __METHOD__); |
||
| 357 | } |
||
| 358 | |||
| 359 | return ''; |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @inheritdoc |
||
| 364 | */ |
||
| 365 | public function getInputHtml($value, ElementInterface $element = null): string |
||
| 366 | { |
||
| 367 | if ($element !== null && $element instanceof Asset && $this->handle !== null) { |
||
| 368 | /** @var Asset $element */ |
||
| 369 | // Register our asset bundle |
||
| 370 | try { |
||
| 371 | Craft::$app->getView()->registerAssetBundle(ImageOptimizeAsset::class); |
||
| 372 | } catch (InvalidConfigException $e) { |
||
| 373 | Craft::error($e->getMessage(), __METHOD__); |
||
| 374 | } |
||
| 375 | |||
| 376 | // Get our id and namespace |
||
| 377 | if (ImageOptimize::$craft35) { |
||
| 378 | $id = Html::id($this->handle); |
||
| 379 | } else { |
||
| 380 | $id = Craft::$app->getView()->formatInputId($this->handle); |
||
| 381 | } |
||
| 382 | $nameSpaceId = Craft::$app->getView()->namespaceInputId($id); |
||
| 383 | |||
| 384 | // Variables to pass down to our field JavaScript to let it namespace properly |
||
| 385 | $jsonVars = [ |
||
| 386 | 'id' => $id, |
||
| 387 | 'name' => $this->handle, |
||
| 388 | 'namespace' => $nameSpaceId, |
||
| 389 | 'prefix' => Craft::$app->getView()->namespaceInputId(''), |
||
| 390 | ]; |
||
| 391 | $jsonVars = Json::encode($jsonVars); |
||
| 392 | $view = Craft::$app->getView(); |
||
| 393 | $view->registerJs("$('#{$nameSpaceId}-field').ImageOptimizeOptimizedImages(".$jsonVars.");"); |
||
| 394 | |||
| 395 | $settings = ImageOptimize::$plugin->getSettings(); |
||
| 396 | $createVariants = ImageOptimize::$plugin->optimizedImages->shouldCreateVariants($this, $element); |
||
| 397 | |||
| 398 | // Render the input template |
||
| 399 | try { |
||
| 400 | return Craft::$app->getView()->renderTemplate( |
||
| 401 | 'image-optimize/_components/fields/OptimizedImages_input', |
||
| 402 | [ |
||
| 403 | 'name' => $this->handle, |
||
| 404 | 'value' => $value, |
||
| 405 | 'variants' => $this->variants, |
||
| 406 | 'field' => $this, |
||
| 407 | 'settings' => $settings, |
||
| 408 | 'elementId' => $element->id, |
||
| 409 | 'format' => $element->getExtension(), |
||
| 410 | 'id' => $id, |
||
| 411 | 'nameSpaceId' => $nameSpaceId, |
||
| 412 | 'createVariants' => $createVariants, |
||
| 413 | ] |
||
| 414 | ); |
||
| 415 | } catch (\Twig\Error\LoaderError $e) { |
||
| 416 | Craft::error($e->getMessage(), __METHOD__); |
||
| 417 | } catch (\yii\base\Exception $e) { |
||
| 418 | Craft::error($e->getMessage(), __METHOD__); |
||
| 419 | } |
||
| 420 | } |
||
| 421 | |||
| 422 | // Render an error template, since the field only works when attached to an Asset |
||
| 423 | try { |
||
| 424 | return Craft::$app->getView()->renderTemplate( |
||
| 425 | 'image-optimize/_components/fields/OptimizedImages_error', |
||
| 426 | [ |
||
| 427 | ] |
||
| 428 | ); |
||
| 429 | } catch (\Twig\Error\LoaderError $e) { |
||
| 430 | Craft::error($e->getMessage(), __METHOD__); |
||
| 431 | } catch (\yii\base\Exception $e) { |
||
| 432 | Craft::error($e->getMessage(), __METHOD__); |
||
| 433 | } |
||
| 434 | |||
| 435 | return ''; |
||
| 436 | } |
||
| 437 | |||
| 438 | // Protected Methods |
||
| 439 | // ========================================================================= |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Returns an array of asset volumes and their sub-folders |
||
| 443 | * |
||
| 444 | * @param string|null $fieldHandle |
||
| 445 | * |
||
| 446 | * @return array |
||
| 447 | * @throws InvalidConfigException |
||
| 448 | */ |
||
| 449 | protected function getFieldVolumeInfo($fieldHandle): array |
||
| 450 | { |
||
| 451 | $result = []; |
||
| 452 | if ($fieldHandle !== null) { |
||
| 453 | $volumes = Craft::$app->getVolumes()->getAllVolumes(); |
||
| 454 | $assets = Craft::$app->getAssets(); |
||
| 455 | foreach ($volumes as $volume) { |
||
| 456 | if (is_subclass_of($volume, Volume::class)) { |
||
| 457 | /** @var Volume $volume */ |
||
| 458 | if ($this->volumeHasField($volume, $fieldHandle)) { |
||
| 459 | $tree = $assets->getFolderTreeByVolumeIds([$volume->id]); |
||
| 460 | $result[] = [ |
||
| 461 | 'name' => $volume->name, |
||
| 462 | 'handle' => $volume->handle, |
||
| 463 | 'subfolders' => $this->assembleSourceList($tree), |
||
| 464 | ]; |
||
| 465 | } |
||
| 466 | } |
||
| 467 | } |
||
| 468 | } |
||
| 469 | |||
| 470 | return $result; |
||
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * See if the passed $volume has an OptimizedImagesField with the handle $fieldHandle |
||
| 475 | * |
||
| 476 | * @param Volume $volume |
||
| 477 | * |
||
| 478 | * @param string $fieldHandle |
||
| 479 | * |
||
| 480 | * @return bool |
||
| 481 | * @throws InvalidConfigException |
||
| 482 | */ |
||
| 483 | protected function volumeHasField(Volume $volume, string $fieldHandle): bool |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Transforms an asset folder tree into a source list. |
||
| 503 | * |
||
| 504 | * @param array $folders |
||
| 505 | * @param bool $includeNestedFolders |
||
| 506 | * |
||
| 507 | * @return array |
||
| 508 | */ |
||
| 509 | protected function assembleSourceList(array $folders, bool $includeNestedFolders = true): array |
||
| 523 |