Total Complexity | 64 |
Total Lines | 494 |
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 2.0.0 |
||
204 | */ |
||
205 | public function useFieldset(): bool |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @inheritdoc |
||
212 | */ |
||
213 | public function afterElementSave(ElementInterface $asset, bool $isNew) |
||
214 | { |
||
215 | parent::afterElementSave($asset, $isNew); |
||
216 | // Update our OptimizedImages Field data now that the Asset has been saved |
||
217 | if ($asset !== null && $asset instanceof Asset && $asset->id !== null) { |
||
218 | // If this element is propagating, we don't need to redo the image saving for each site |
||
219 | if (!$asset->propagating) { |
||
220 | // If the scenario is Asset::SCENARIO_FILEOPS treat it as a new asset |
||
221 | $scenario = $asset->getScenario(); |
||
222 | if ($isNew || $scenario === Asset::SCENARIO_FILEOPS ) { |
||
223 | /** |
||
224 | * If this is a newly uploaded/created Asset, we can save the variants |
||
225 | * via a queue job to prevent it from blocking |
||
226 | */ |
||
227 | ImageOptimize::$plugin->optimizedImages->resaveAsset($asset->id); |
||
228 | } else { |
||
229 | /** |
||
230 | * If it's not a newly uploaded/created Asset, they may have edited |
||
231 | * the image with the ImageEditor, so we need to update the variants |
||
232 | * immediately, so the AssetSelectorHud displays the new images |
||
233 | */ |
||
234 | try { |
||
235 | ImageOptimize::$plugin->optimizedImages->updateOptimizedImageFieldData($this, $asset); |
||
236 | } catch (Exception $e) { |
||
237 | Craft::error($e->getMessage(), __METHOD__); |
||
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 |
||
271 | { |
||
272 | return Schema::TYPE_TEXT; |
||
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 | |||
296 | try { |
||
297 | $reflect = new \ReflectionClass($this); |
||
298 | $thisId = $reflect->getShortName(); |
||
299 | } catch (\ReflectionException $e) { |
||
300 | Craft::error($e->getMessage(), __METHOD__); |
||
301 | $thisId = 0; |
||
302 | } |
||
303 | // Get our id and namespace |
||
304 | if (ImageOptimize::$craft35) { |
||
305 | $id = Html::id($thisId); |
||
306 | } else { |
||
307 | $id = Craft::$app->getView()->formatInputId($thisId); |
||
308 | } |
||
309 | $namespacedId = Craft::$app->getView()->namespaceInputId($id); |
||
310 | $namespacePrefix = Craft::$app->getView()->namespaceInputName($thisId); |
||
311 | $sizesWrapperId = Craft::$app->getView()->namespaceInputId('sizes-wrapper'); |
||
312 | Craft::$app->getView()->registerJs('new Craft.OptimizedImagesInput('. |
||
313 | '"'.$namespacedId.'", '. |
||
314 | '"'.$namespacePrefix.'",'. |
||
315 | '"'.$sizesWrapperId.'"'. |
||
316 | ');'); |
||
317 | |||
318 | // Prep our aspect ratios |
||
319 | $aspectRatios = []; |
||
320 | $index = 1; |
||
321 | foreach ($this->aspectRatios as $aspectRatio) { |
||
322 | if ($index % 6 === 0) { |
||
323 | $aspectRatio['break'] = true; |
||
324 | } |
||
325 | $aspectRatios[] = $aspectRatio; |
||
326 | $index++; |
||
327 | } |
||
328 | $aspectRatio = ['x' => 2, 'y' => 2, 'custom' => true]; |
||
329 | $aspectRatios[] = $aspectRatio; |
||
330 | // Get only the user-editable settings |
||
331 | $settings = ImageOptimize::$plugin->getSettings(); |
||
332 | |||
333 | // Register our CSS |
||
334 | $cssModules = [ |
||
335 | 'vendors.css', |
||
336 | 'styles.css', |
||
337 | ]; |
||
338 | foreach($cssModules as $cssModule) { |
||
339 | $css = ManifestHelper::getModule(ManifestVariable::$config, $cssModule, 'legacy', true); |
||
340 | if ($css) { |
||
341 | Craft::$app->getView()->registerCssFile($css); |
||
342 | } |
||
343 | } |
||
344 | |||
345 | // Render the settings template |
||
346 | try { |
||
347 | return Craft::$app->getView()->renderTemplate( |
||
348 | 'image-optimize/_components/fields/OptimizedImages_settings', |
||
349 | [ |
||
350 | 'field' => $this, |
||
351 | 'settings' => $settings, |
||
352 | 'aspectRatios' => $aspectRatios, |
||
353 | 'id' => $id, |
||
354 | 'name' => $this->handle, |
||
355 | 'namespace' => $namespacedId, |
||
356 | 'fieldVolumes' => $this->getFieldVolumeInfo($this->handle), |
||
357 | ] |
||
358 | ); |
||
359 | } catch (\Twig\Error\LoaderError $e) { |
||
360 | Craft::error($e->getMessage(), __METHOD__); |
||
361 | } catch (\yii\base\Exception $e) { |
||
362 | Craft::error($e->getMessage(), __METHOD__); |
||
363 | } |
||
364 | |||
365 | return ''; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * @inheritdoc |
||
370 | */ |
||
371 | public function getInputHtml($value, ElementInterface $element = null): string |
||
372 | { |
||
373 | if ($element !== null && $element instanceof Asset && $this->handle !== null) { |
||
374 | /** @var Asset $element */ |
||
375 | // Register our asset bundle |
||
376 | try { |
||
377 | Craft::$app->getView()->registerAssetBundle(ImageOptimizeAsset::class); |
||
378 | } catch (InvalidConfigException $e) { |
||
379 | Craft::error($e->getMessage(), __METHOD__); |
||
380 | } |
||
381 | |||
382 | // Get our id and namespace |
||
383 | if (ImageOptimize::$craft35) { |
||
384 | $id = Html::id($this->handle); |
||
385 | } else { |
||
386 | $id = Craft::$app->getView()->formatInputId($this->handle); |
||
387 | } |
||
388 | $nameSpaceId = Craft::$app->getView()->namespaceInputId($id); |
||
389 | |||
390 | // Variables to pass down to our field JavaScript to let it namespace properly |
||
391 | $jsonVars = [ |
||
392 | 'id' => $id, |
||
393 | 'name' => $this->handle, |
||
394 | 'namespace' => $nameSpaceId, |
||
395 | 'prefix' => Craft::$app->getView()->namespaceInputId(''), |
||
396 | ]; |
||
397 | $jsonVars = Json::encode($jsonVars); |
||
398 | $view = Craft::$app->getView(); |
||
399 | $view->registerJs("$('#{$nameSpaceId}-field').ImageOptimizeOptimizedImages(".$jsonVars.");"); |
||
400 | |||
401 | $settings = ImageOptimize::$plugin->getSettings(); |
||
402 | $createVariants = ImageOptimize::$plugin->optimizedImages->shouldCreateVariants($this, $element); |
||
403 | |||
404 | // Register our CSS |
||
405 | $cssModules = [ |
||
406 | 'vendors.css', |
||
407 | 'styles.css', |
||
408 | ]; |
||
409 | foreach($cssModules as $cssModule) { |
||
410 | $css = ManifestHelper::getModule(ManifestVariable::$config, $cssModule, 'legacy', true); |
||
411 | if ($css) { |
||
412 | Craft::$app->getView()->registerCssFile($css); |
||
413 | } |
||
414 | } |
||
415 | |||
416 | // Render the input template |
||
417 | try { |
||
418 | return Craft::$app->getView()->renderTemplate( |
||
419 | 'image-optimize/_components/fields/OptimizedImages_input', |
||
420 | [ |
||
421 | 'name' => $this->handle, |
||
422 | 'value' => $value, |
||
423 | 'variants' => $this->variants, |
||
424 | 'field' => $this, |
||
425 | 'settings' => $settings, |
||
426 | 'elementId' => $element->id, |
||
427 | 'format' => $element->getExtension(), |
||
428 | 'id' => $id, |
||
429 | 'nameSpaceId' => $nameSpaceId, |
||
430 | 'createVariants' => $createVariants, |
||
431 | ] |
||
432 | ); |
||
433 | } catch (\Twig\Error\LoaderError $e) { |
||
434 | Craft::error($e->getMessage(), __METHOD__); |
||
435 | } catch (\yii\base\Exception $e) { |
||
436 | Craft::error($e->getMessage(), __METHOD__); |
||
437 | } |
||
438 | } |
||
439 | |||
440 | // Render an error template, since the field only works when attached to an Asset |
||
441 | try { |
||
442 | return Craft::$app->getView()->renderTemplate( |
||
443 | 'image-optimize/_components/fields/OptimizedImages_error', |
||
444 | [ |
||
445 | ] |
||
446 | ); |
||
447 | } catch (\Twig\Error\LoaderError $e) { |
||
448 | Craft::error($e->getMessage(), __METHOD__); |
||
449 | } catch (\yii\base\Exception $e) { |
||
450 | Craft::error($e->getMessage(), __METHOD__); |
||
451 | } |
||
452 | |||
453 | return ''; |
||
454 | } |
||
455 | |||
456 | // Protected Methods |
||
457 | // ========================================================================= |
||
458 | |||
459 | /** |
||
460 | * Returns an array of asset volumes and their sub-folders |
||
461 | * |
||
462 | * @param string|null $fieldHandle |
||
463 | * |
||
464 | * @return array |
||
465 | * @throws InvalidConfigException |
||
466 | */ |
||
467 | protected function getFieldVolumeInfo($fieldHandle): array |
||
468 | { |
||
469 | $result = []; |
||
470 | if ($fieldHandle !== null) { |
||
471 | $volumes = Craft::$app->getVolumes()->getAllVolumes(); |
||
472 | $assets = Craft::$app->getAssets(); |
||
473 | foreach ($volumes as $volume) { |
||
474 | if (is_subclass_of($volume, Volume::class)) { |
||
475 | /** @var Volume $volume */ |
||
476 | if ($this->volumeHasField($volume, $fieldHandle)) { |
||
477 | $tree = $assets->getFolderTreeByVolumeIds([$volume->id]); |
||
478 | $result[] = [ |
||
479 | 'name' => $volume->name, |
||
480 | 'handle' => $volume->handle, |
||
481 | 'subfolders' => $this->assembleSourceList($tree), |
||
482 | ]; |
||
483 | } |
||
484 | } |
||
485 | } |
||
486 | } |
||
487 | |||
488 | return $result; |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * See if the passed $volume has an OptimizedImagesField with the handle $fieldHandle |
||
493 | * |
||
494 | * @param Volume $volume |
||
495 | * |
||
496 | * @param string $fieldHandle |
||
497 | * |
||
498 | * @return bool |
||
499 | * @throws InvalidConfigException |
||
500 | */ |
||
501 | protected function volumeHasField(Volume $volume, string $fieldHandle): bool |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * Transforms an asset folder tree into a source list. |
||
521 | * |
||
522 | * @param array $folders |
||
523 | * @param bool $includeNestedFolders |
||
524 | * |
||
525 | * @return array |
||
526 | */ |
||
527 | protected function assembleSourceList(array $folders, bool $includeNestedFolders = true): array |
||
539 | } |
||
540 | } |
||
541 |