| Total Complexity | 44 |
| Total Lines | 354 |
| Duplicated Lines | 0 % |
| Changes | 12 | ||
| 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 |
||
| 39 | class OptimizedImages extends Field |
||
| 40 | { |
||
| 41 | // Constants |
||
| 42 | // ========================================================================= |
||
| 43 | |||
| 44 | const DEFAULT_ASPECT_RATIOS = [ |
||
| 45 | ['x' => 16, 'y' => 9], |
||
| 46 | ]; |
||
| 47 | const DEFAULT_IMAGE_VARIANTS = [ |
||
| 48 | [ |
||
| 49 | 'width' => 1200, |
||
| 50 | 'useAspectRatio' => true, |
||
| 51 | 'aspectRatioX' => 16.0, |
||
| 52 | 'aspectRatioY' => 9.0, |
||
| 53 | 'retinaSizes' => ['1'], |
||
| 54 | 'quality' => 82, |
||
| 55 | 'format' => 'jpg', |
||
| 56 | ], |
||
| 57 | ]; |
||
| 58 | |||
| 59 | // Public Properties |
||
| 60 | // ========================================================================= |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | public $ignoreFilesOfType = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var bool |
||
| 69 | */ |
||
| 70 | public $displayOptimizedImageVariants = true; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var bool |
||
| 74 | */ |
||
| 75 | public $displayDominantColorPalette = true; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var bool |
||
| 79 | */ |
||
| 80 | public $displayLazyLoadPlaceholderImages = true; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | public $variants = []; |
||
| 86 | |||
| 87 | // Private Properties |
||
| 88 | // ========================================================================= |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | private $aspectRatios = []; |
||
| 94 | |||
| 95 | // Static Methods |
||
| 96 | // ========================================================================= |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @inheritdoc |
||
| 100 | */ |
||
| 101 | public function __construct(array $config = []) |
||
| 102 | { |
||
| 103 | // Unset any deprecated properties |
||
| 104 | if (!empty($config)) { |
||
| 105 | unset($config['transformMethod'], $config['imgixDomain']); |
||
| 106 | } |
||
| 107 | parent::__construct($config); |
||
| 108 | } |
||
| 109 | |||
| 110 | // Public Methods |
||
| 111 | // ========================================================================= |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @inheritdoc |
||
| 115 | */ |
||
| 116 | public static function displayName(): string |
||
| 117 | { |
||
| 118 | return 'OptimizedImages'; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @inheritdoc |
||
| 123 | */ |
||
| 124 | public function init() |
||
| 125 | { |
||
| 126 | parent::init(); |
||
| 127 | |||
| 128 | // Handle cases where the plugin has been uninstalled |
||
| 129 | if (ImageOptimize::$plugin !== null) { |
||
| 130 | $settings = ImageOptimize::$plugin->getSettings(); |
||
| 131 | if ($settings) { |
||
| 132 | if (empty($this->variants)) { |
||
| 133 | $this->variants = $settings->defaultVariants; |
||
| 134 | } |
||
| 135 | $this->aspectRatios = $settings->defaultAspectRatios; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | // If the user has deleted all default aspect ratios, provide a fallback |
||
| 139 | if (empty($this->aspectRatios)) { |
||
| 140 | $this->aspectRatios = self::DEFAULT_ASPECT_RATIOS; |
||
| 141 | } |
||
| 142 | // If the user has deleted all default variants, provide a fallback |
||
| 143 | if (empty($this->variants)) { |
||
| 144 | $this->variants = self::DEFAULT_IMAGE_VARIANTS; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @inheritdoc |
||
| 150 | */ |
||
| 151 | public function rules() |
||
| 152 | { |
||
| 153 | $rules = parent::rules(); |
||
| 154 | $rules = array_merge($rules, [ |
||
| 155 | [ |
||
| 156 | [ |
||
| 157 | 'displayOptimizedImageVariants', |
||
| 158 | 'displayDominantColorPalette', |
||
| 159 | 'displayLazyLoadPlaceholderImages', |
||
| 160 | ], |
||
| 161 | 'boolean', |
||
| 162 | ], |
||
| 163 | [ |
||
| 164 | [ |
||
| 165 | 'ignoreFilesOfType', |
||
| 166 | 'variants', |
||
| 167 | ], |
||
| 168 | ArrayValidator::class |
||
| 169 | ], |
||
| 170 | ]); |
||
| 171 | |||
| 172 | return $rules; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @inheritdoc |
||
| 177 | * @since 1.6.2 |
||
| 178 | */ |
||
| 179 | public function getContentGqlType() |
||
| 187 | ]; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @inheritdoc |
||
| 192 | */ |
||
| 193 | public function afterElementSave(ElementInterface $asset, bool $isNew) |
||
| 194 | { |
||
| 195 | parent::afterElementSave($asset, $isNew); |
||
| 196 | // Update our OptimizedImages Field data now that the Asset has been saved |
||
| 197 | if ($asset !== null && $asset instanceof Asset && $asset->id !== null) { |
||
| 198 | // If the scenario is Asset::SCENARIO_FILEOPS or Asset::SCENARIO_ESSENTIALS treat it as a new asset |
||
| 199 | $scenario = $asset->getScenario(); |
||
| 200 | if ($isNew || $scenario === Asset::SCENARIO_FILEOPS || $asset->propagating) { |
||
| 201 | /** |
||
| 202 | * If this is a newly uploaded/created Asset, we can save the variants |
||
| 203 | * via a queue job to prevent it from blocking |
||
| 204 | */ |
||
| 205 | ImageOptimize::$plugin->optimizedImages->resaveAsset($asset->id); |
||
| 206 | } else { |
||
| 207 | /** |
||
| 208 | * If it's not a newly uploaded/created Asset, they may have edited |
||
| 209 | * the image with the ImageEditor, so we need to update the variants |
||
| 210 | * immediately, so the AssetSelectorHud displays the new images |
||
| 211 | */ |
||
| 212 | try { |
||
| 213 | ImageOptimize::$plugin->optimizedImages->updateOptimizedImageFieldData($this, $asset); |
||
| 214 | } catch (Exception $e) { |
||
| 215 | Craft::error($e->getMessage(), __METHOD__); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @inheritdoc |
||
| 223 | */ |
||
| 224 | public function normalizeValue($value, ElementInterface $asset = null) |
||
| 225 | { |
||
| 226 | // If we're passed in a string, assume it's JSON-encoded, and decode it |
||
| 227 | if (\is_string($value) && !empty($value)) { |
||
| 228 | $value = Json::decodeIfJson($value); |
||
| 229 | } |
||
| 230 | // If we're passed in an array, make a model from it |
||
| 231 | if (\is_array($value)) { |
||
| 232 | // Create a new OptimizedImage model and populate it |
||
| 233 | $model = new OptimizedImage($value); |
||
| 234 | } elseif ($value instanceof OptimizedImage) { |
||
| 235 | $model = $value; |
||
| 236 | } else { |
||
| 237 | // Just create a new empty model |
||
| 238 | $model = new OptimizedImage(null); |
||
| 239 | } |
||
| 240 | |||
| 241 | return $model; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @inheritdoc |
||
| 246 | */ |
||
| 247 | public function getContentColumnType(): string |
||
| 248 | { |
||
| 249 | return Schema::TYPE_TEXT; |
||
| 250 | } |
||
| 251 | |||
| 252 | // Protected Methods |
||
| 253 | // ========================================================================= |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @inheritdoc |
||
| 257 | */ |
||
| 258 | public function getSettingsHtml() |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @inheritdoc |
||
| 327 | */ |
||
| 328 | public function getInputHtml($value, ElementInterface $element = null): string |
||
| 393 | } |
||
| 394 | } |
||
| 395 |