Total Complexity | 42 |
Total Lines | 320 |
Duplicated Lines | 0 % |
Changes | 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 |
||
36 | class OptimizedImages extends Field |
||
37 | { |
||
38 | // Constants |
||
39 | // ========================================================================= |
||
40 | |||
41 | const DEFAULT_ASPECT_RATIOS = [ |
||
42 | ['x' => 16, 'y' => 9], |
||
43 | ]; |
||
44 | const DEFAULT_IMAGE_VARIANTS = [ |
||
45 | [ |
||
46 | 'width' => 1200, |
||
47 | 'useAspectRatio' => true, |
||
48 | 'aspectRatioX' => 16.0, |
||
49 | 'aspectRatioY' => 9.0, |
||
50 | 'retinaSizes' => ['1'], |
||
51 | 'quality' => 82, |
||
52 | 'format' => 'jpg', |
||
53 | ], |
||
54 | ]; |
||
55 | |||
56 | // Public Properties |
||
57 | // ========================================================================= |
||
58 | |||
59 | /** |
||
60 | * @var bool |
||
61 | */ |
||
62 | public $displayOptimizedImageVariants = true; |
||
63 | |||
64 | /** |
||
65 | * @var bool |
||
66 | */ |
||
67 | public $displayDominantColorPalette = true; |
||
68 | |||
69 | /** |
||
70 | * @var bool |
||
71 | */ |
||
72 | public $displayLazyLoadPlaceholderImages = true; |
||
73 | |||
74 | /** |
||
75 | * @var array |
||
76 | */ |
||
77 | public $variants = []; |
||
78 | |||
79 | // Private Properties |
||
80 | // ========================================================================= |
||
81 | |||
82 | /** |
||
83 | * @var array |
||
84 | */ |
||
85 | private $aspectRatios = []; |
||
86 | |||
87 | // Static Methods |
||
88 | // ========================================================================= |
||
89 | |||
90 | /** |
||
91 | * @inheritdoc |
||
92 | */ |
||
93 | public function __construct(array $config = []) |
||
94 | { |
||
95 | // Unset any deprecated properties |
||
96 | if (!empty($config)) { |
||
97 | unset($config['transformMethod'], $config['imgixDomain']); |
||
98 | } |
||
99 | parent::__construct($config); |
||
100 | } |
||
101 | |||
102 | // Public Methods |
||
103 | // ========================================================================= |
||
104 | |||
105 | /** |
||
106 | * @inheritdoc |
||
107 | */ |
||
108 | public static function displayName(): string |
||
109 | { |
||
110 | return 'OptimizedImages'; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @inheritdoc |
||
115 | */ |
||
116 | public function init() |
||
137 | } |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @inheritdoc |
||
142 | */ |
||
143 | public function rules() |
||
144 | { |
||
145 | $rules = parent::rules(); |
||
146 | $rules = array_merge($rules, [ |
||
147 | ['variants', ArrayValidator::class], |
||
148 | ]); |
||
149 | |||
150 | return $rules; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @inheritdoc |
||
155 | */ |
||
156 | public function afterElementSave(ElementInterface $asset, bool $isNew) |
||
157 | { |
||
158 | parent::afterElementSave($asset, $isNew); |
||
159 | // Update our OptimizedImages Field data now that the Asset has been saved |
||
160 | if ($asset !== null && $asset instanceof Asset && $asset->id !== null) { |
||
161 | // If the scenario is Asset::SCENARIO_FILEOPS or Asset::SCENARIO_ESSENTIALS treat it as a new asset |
||
162 | $scenario = $asset->getScenario(); |
||
163 | if ($isNew || $scenario === Asset::SCENARIO_FILEOPS || $asset->propagating) { |
||
164 | /** |
||
165 | * If this is a newly uploaded/created Asset, we can save the variants |
||
166 | * via a queue job to prevent it from blocking |
||
167 | */ |
||
168 | ImageOptimize::$plugin->optimizedImages->resaveAsset($asset->id); |
||
169 | } else { |
||
170 | /** |
||
171 | * If it's not a newly uploaded/created Asset, they may have edited |
||
172 | * the image with the ImageEditor, so we need to update the variants |
||
173 | * immediately, so the AssetSelectorHud displays the new images |
||
174 | */ |
||
175 | try { |
||
176 | ImageOptimize::$plugin->optimizedImages->updateOptimizedImageFieldData($this, $asset); |
||
177 | } catch (Exception $e) { |
||
178 | Craft::error($e->getMessage(), __METHOD__); |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @inheritdoc |
||
186 | */ |
||
187 | public function normalizeValue($value, ElementInterface $asset = null) |
||
188 | { |
||
189 | // If we're passed in a string, assume it's JSON-encoded, and decode it |
||
190 | if (\is_string($value) && !empty($value)) { |
||
191 | $value = Json::decodeIfJson($value); |
||
192 | } |
||
193 | // If we're passed in an array, make a model from it |
||
194 | if (\is_array($value)) { |
||
195 | // Create a new OptimizedImage model and populate it |
||
196 | $model = new OptimizedImage($value); |
||
197 | } elseif ($value instanceof OptimizedImage) { |
||
198 | $model = $value; |
||
199 | } else { |
||
200 | // Just create a new empty model |
||
201 | $model = new OptimizedImage(null); |
||
202 | } |
||
203 | |||
204 | return $model; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @inheritdoc |
||
209 | */ |
||
210 | public function getContentColumnType(): string |
||
211 | { |
||
212 | return Schema::TYPE_TEXT; |
||
213 | } |
||
214 | |||
215 | // Protected Methods |
||
216 | // ========================================================================= |
||
217 | |||
218 | /** |
||
219 | * @inheritdoc |
||
220 | */ |
||
221 | public function getSettingsHtml() |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * @inheritdoc |
||
290 | */ |
||
291 | public function getInputHtml($value, ElementInterface $element = null): string |
||
356 | } |
||
357 | } |
||
358 |