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