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