Total Complexity | 49 |
Total Lines | 584 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like ImageOptimize 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 ImageOptimize, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
68 | class ImageOptimize extends Plugin |
||
69 | { |
||
70 | // Traits |
||
71 | // ========================================================================= |
||
72 | |||
73 | use ServicesTrait; |
||
74 | |||
75 | // Constants |
||
76 | // ========================================================================= |
||
77 | |||
78 | const CRAFTQL_PLUGIN_HANDLE = 'craftql'; |
||
79 | |||
80 | // Static Properties |
||
81 | // ========================================================================= |
||
82 | |||
83 | /** |
||
84 | * @var ImageOptimize |
||
85 | */ |
||
86 | public static $plugin; |
||
87 | |||
88 | /** |
||
89 | * @var bool |
||
90 | */ |
||
91 | public static $generatePlaceholders = true; |
||
92 | |||
93 | /** |
||
94 | * @var bool |
||
95 | */ |
||
96 | public static $craft31 = false; |
||
97 | |||
98 | /** |
||
99 | * @var bool |
||
100 | */ |
||
101 | public static $craft35 = false; |
||
102 | |||
103 | /** |
||
104 | * @var bool |
||
105 | */ |
||
106 | public static $craft37 = false; |
||
107 | |||
108 | // Public Properties |
||
109 | // ========================================================================= |
||
110 | |||
111 | /** |
||
112 | * @var string |
||
113 | */ |
||
114 | public $schemaVersion = '1.0.0'; |
||
115 | |||
116 | /** |
||
117 | * @var bool |
||
118 | */ |
||
119 | public $hasCpSection = false; |
||
120 | |||
121 | /** |
||
122 | * @var bool |
||
123 | */ |
||
124 | public $hasCpSettings = true; |
||
125 | |||
126 | // Public Methods |
||
127 | // ========================================================================= |
||
128 | |||
129 | /** |
||
130 | * @inheritdoc |
||
131 | */ |
||
132 | public function init() |
||
158 | ); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @inheritdoc |
||
163 | */ |
||
164 | public function getSettingsResponse() |
||
165 | { |
||
166 | $view = Craft::$app->getView(); |
||
167 | $namespace = $view->getNamespace(); |
||
168 | $view->setNamespace('settings'); |
||
169 | $settingsHtml = $this->settingsHtml(); |
||
170 | $view->setNamespace($namespace); |
||
171 | /** @var Controller $controller */ |
||
172 | $controller = Craft::$app->controller; |
||
173 | |||
174 | return $controller->renderTemplate('image-optimize/settings/index.twig', [ |
||
175 | 'plugin' => $this, |
||
176 | 'settingsHtml' => $settingsHtml, |
||
177 | ]); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @inheritdoc |
||
182 | */ |
||
183 | public function settingsHtml() |
||
184 | { |
||
185 | // Get only the user-editable settings |
||
186 | $settings = $this->getSettings(); |
||
187 | |||
188 | // Get the image transform types |
||
189 | $allImageTransformTypes = ImageOptimize::$plugin->optimize->getAllImageTransformTypes(); |
||
190 | $imageTransformTypeOptions = []; |
||
191 | /** @var ImageTransformInterface $class */ |
||
192 | foreach ($allImageTransformTypes as $class) { |
||
193 | if ($class::isSelectable()) { |
||
194 | $imageTransformTypeOptions[] = [ |
||
195 | 'value' => $class, |
||
196 | 'label' => $class::displayName(), |
||
197 | ]; |
||
198 | } |
||
199 | } |
||
200 | // Sort them by name |
||
201 | ArrayHelper::multisort($imageTransformTypeOptions, 'label'); |
||
202 | |||
203 | // Render the settings template |
||
204 | try { |
||
205 | return Craft::$app->getView()->renderTemplate( |
||
206 | 'image-optimize/settings/_settings.twig', |
||
207 | [ |
||
208 | 'settings' => $settings, |
||
209 | 'gdInstalled' => \function_exists('imagecreatefromjpeg'), |
||
210 | 'imageTransformTypeOptions' => $imageTransformTypeOptions, |
||
211 | 'allImageTransformTypes' => $allImageTransformTypes, |
||
212 | 'imageTransform' => ImageOptimize::$plugin->transformMethod, |
||
213 | ] |
||
214 | ); |
||
215 | } catch (\Twig\Error\LoaderError $e) { |
||
216 | Craft::error($e->getMessage(), __METHOD__); |
||
217 | } catch (Exception $e) { |
||
218 | Craft::error($e->getMessage(), __METHOD__); |
||
219 | } |
||
220 | |||
221 | return ''; |
||
222 | } |
||
223 | |||
224 | // Protected Methods |
||
225 | // ========================================================================= |
||
226 | |||
227 | /** |
||
228 | * @inheritdoc |
||
229 | */ |
||
230 | protected function createSettingsModel() |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Set the transformMethod component |
||
237 | */ |
||
238 | protected function setImageTransformComponent() |
||
239 | { |
||
240 | $settings = $this->getSettings(); |
||
241 | $definition = array_merge( |
||
242 | $settings->imageTransformTypeSettings[$settings->transformClass] ?? [], |
||
243 | ['class' => $settings->transformClass] |
||
244 | ); |
||
245 | try { |
||
246 | $this->set('transformMethod', $definition); |
||
247 | } catch (InvalidConfigException $e) { |
||
248 | Craft::error($e->getMessage(), __METHOD__); |
||
249 | } |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Add in our Craft components |
||
254 | */ |
||
255 | protected function addComponents() |
||
256 | { |
||
257 | // Register our variables |
||
258 | Event::on( |
||
259 | CraftVariable::class, |
||
260 | CraftVariable::EVENT_INIT, |
||
261 | function (Event $event) { |
||
262 | /** @var CraftVariable $variable */ |
||
263 | $variable = $event->sender; |
||
264 | $variable->set('imageOptimize', [ |
||
265 | 'class' => ImageOptimizeVariable::class, |
||
266 | 'viteService' => $this->vite, |
||
267 | ]); |
||
268 | } |
||
269 | ); |
||
270 | |||
271 | // Register our Field |
||
272 | Event::on( |
||
273 | Fields::class, |
||
274 | Fields::EVENT_REGISTER_FIELD_TYPES, |
||
275 | function (RegisterComponentTypesEvent $event) { |
||
276 | Craft::debug( |
||
277 | 'Fields::EVENT_REGISTER_FIELD_TYPES', |
||
278 | __METHOD__ |
||
279 | ); |
||
280 | $event->types[] = OptimizedImages::class; |
||
281 | } |
||
282 | ); |
||
283 | |||
284 | // Register our Utility only if they are using the CraftImageTransform method |
||
285 | if (ImageOptimize::$plugin->transformMethod instanceof CraftImageTransform) { |
||
286 | Event::on( |
||
287 | Utilities::class, |
||
288 | Utilities::EVENT_REGISTER_UTILITY_TYPES, |
||
289 | function (RegisterComponentTypesEvent $event) { |
||
290 | $event->types[] = ImageOptimizeUtility::class; |
||
291 | } |
||
292 | ); |
||
293 | } |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Install our event handlers |
||
298 | */ |
||
299 | protected function installEventHandlers() |
||
300 | { |
||
301 | $this->installAssetEventHandlers(); |
||
302 | $this->installElementEventHandlers(); |
||
303 | $this->installMiscEventHandlers(); |
||
304 | $this->installCraftQLEventHandlers(); |
||
305 | $request = Craft::$app->getRequest(); |
||
306 | // Install only for non-console site requests |
||
307 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
||
308 | $this->installSiteEventListeners(); |
||
309 | } |
||
310 | // Install only for non-console cp requests |
||
311 | if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) { |
||
312 | $this->installCpEventListeners(); |
||
313 | } |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * Install our Asset event handlers |
||
318 | */ |
||
319 | protected function installAssetEventHandlers() |
||
320 | { |
||
321 | // Handler: Assets::EVENT_GET_ASSET_URL |
||
322 | Event::on( |
||
323 | Assets::class, |
||
324 | Assets::EVENT_GET_ASSET_URL, |
||
325 | function (GetAssetUrlEvent $event) { |
||
326 | Craft::debug( |
||
327 | 'Assets::EVENT_GET_ASSET_URL', |
||
328 | __METHOD__ |
||
329 | ); |
||
330 | // Return the URL to the asset URL or null to let Craft handle it |
||
331 | $event->url = ImageOptimize::$plugin->optimize->handleGetAssetUrlEvent( |
||
332 | $event |
||
333 | ); |
||
334 | } |
||
335 | ); |
||
336 | |||
337 | // Handler: Assets::EVENT_GET_ASSET_THUMB_URL |
||
338 | Event::on( |
||
339 | Assets::class, |
||
340 | Assets::EVENT_GET_ASSET_THUMB_URL, |
||
341 | function (GetAssetThumbUrlEvent $event) { |
||
342 | Craft::debug( |
||
343 | 'Assets::EVENT_GET_ASSET_THUMB_URL', |
||
344 | __METHOD__ |
||
345 | ); |
||
346 | // Return the URL to the asset URL or null to let Craft handle it |
||
347 | $event->url = ImageOptimize::$plugin->optimize->handleGetAssetThumbUrlEvent( |
||
348 | $event |
||
349 | ); |
||
350 | } |
||
351 | ); |
||
352 | |||
353 | // Handler: AssetTransforms::EVENT_GENERATE_TRANSFORM |
||
354 | Event::on( |
||
355 | AssetTransforms::class, |
||
356 | AssetTransforms::EVENT_GENERATE_TRANSFORM, |
||
357 | function (GenerateTransformEvent $event) { |
||
358 | Craft::debug( |
||
359 | 'AssetTransforms::EVENT_GENERATE_TRANSFORM', |
||
360 | __METHOD__ |
||
361 | ); |
||
362 | // Return the path to the optimized image to _createTransformForAsset() |
||
363 | $event->tempPath = ImageOptimize::$plugin->optimize->handleGenerateTransformEvent( |
||
364 | $event |
||
365 | ); |
||
366 | } |
||
367 | ); |
||
368 | |||
369 | // Handler: AssetTransforms::EVENT_AFTER_DELETE_TRANSFORMS |
||
370 | Event::on( |
||
371 | AssetTransforms::class, |
||
372 | AssetTransforms::EVENT_AFTER_DELETE_TRANSFORMS, |
||
373 | function (AssetTransformImageEvent $event) { |
||
374 | Craft::debug( |
||
375 | 'AssetTransforms::EVENT_AFTER_DELETE_TRANSFORMS', |
||
376 | __METHOD__ |
||
377 | ); |
||
378 | // Clean up any stray variant files |
||
379 | ImageOptimize::$plugin->optimize->handleAfterDeleteTransformsEvent( |
||
380 | $event |
||
381 | ); |
||
382 | } |
||
383 | ); |
||
384 | |||
385 | // Handler: Assets::EVENT_BEFORE_REPLACE_ASSET |
||
386 | Event::on( |
||
387 | Assets::class, |
||
388 | Assets::EVENT_BEFORE_REPLACE_ASSET, |
||
389 | function (ReplaceAssetEvent $event) { |
||
390 | Craft::debug( |
||
391 | 'Assets::EVENT_BEFORE_REPLACE_ASSET', |
||
392 | __METHOD__ |
||
393 | ); |
||
394 | /** @var Asset $element */ |
||
395 | $element = $event->asset; |
||
396 | // Purge the URL |
||
397 | $purgeUrl = ImageOptimize::$plugin->transformMethod->getPurgeUrl($element); |
||
398 | if ($purgeUrl) { |
||
399 | ImageOptimize::$plugin->transformMethod->purgeUrl($purgeUrl); |
||
400 | } |
||
401 | } |
||
402 | ); |
||
403 | |||
404 | // Handler: Assets::EVENT_AFTER_REPLACE_ASSET |
||
405 | Event::on( |
||
406 | Assets::class, |
||
407 | Assets::EVENT_AFTER_REPLACE_ASSET, |
||
408 | function (ReplaceAssetEvent $event) { |
||
409 | Craft::debug( |
||
410 | 'Assets::EVENT_AFTER_REPLACE_ASSET', |
||
411 | __METHOD__ |
||
412 | ); |
||
413 | /** @var Asset $element */ |
||
414 | $element = $event->asset; |
||
415 | if ($element->id !== null) { |
||
416 | ImageOptimize::$plugin->optimizedImages->resaveAsset($element->id, true); |
||
417 | } |
||
418 | } |
||
419 | ); |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Install our Element event handlers |
||
424 | */ |
||
425 | protected function installElementEventHandlers() |
||
463 | } |
||
464 | } |
||
465 | ); |
||
466 | } |
||
467 | |||
468 | /** |
||
469 | * Install our miscellaneous event handlers |
||
470 | */ |
||
471 | protected function installMiscEventHandlers() |
||
472 | { |
||
473 | // Handler: Fields::EVENT_AFTER_SAVE_FIELD |
||
474 | Event::on( |
||
475 | Fields::class, |
||
476 | Fields::EVENT_AFTER_SAVE_FIELD, |
||
477 | function (FieldEvent $event) { |
||
478 | Craft::debug( |
||
479 | 'Fields::EVENT_AFTER_SAVE_FIELD', |
||
480 | __METHOD__ |
||
481 | ); |
||
482 | $settings = $this->getSettings(); |
||
483 | /** @var Field $field */ |
||
484 | if (!$event->isNew && $settings->automaticallyResaveImageVariants) { |
||
485 | $this->checkForOptimizedImagesField($event); |
||
486 | } |
||
487 | } |
||
488 | ); |
||
489 | |||
490 | // Handler: Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS |
||
491 | Event::on( |
||
492 | Plugins::class, |
||
493 | Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS, |
||
494 | function (PluginEvent $event) { |
||
495 | if ($event->plugin === $this) { |
||
496 | Craft::debug( |
||
497 | 'Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS', |
||
498 | __METHOD__ |
||
499 | ); |
||
500 | $settings = $this->getSettings(); |
||
501 | if ($settings->automaticallyResaveImageVariants) { |
||
502 | // After they have changed the settings, resave all of the assets |
||
503 | ImageOptimize::$plugin->optimizedImages->resaveAllVolumesAssets(); |
||
504 | } |
||
505 | } |
||
506 | } |
||
507 | ); |
||
508 | |||
509 | // Handler: Volumes::EVENT_AFTER_SAVE_VOLUME |
||
510 | Event::on( |
||
511 | Volumes::class, |
||
512 | Volumes::EVENT_AFTER_SAVE_VOLUME, |
||
513 | function (VolumeEvent $event) { |
||
514 | Craft::debug( |
||
515 | 'Volumes::EVENT_AFTER_SAVE_VOLUME', |
||
516 | __METHOD__ |
||
517 | ); |
||
518 | $settings = $this->getSettings(); |
||
519 | // Only worry about this volume if it's not new |
||
520 | if (!$event->isNew && $settings->automaticallyResaveImageVariants) { |
||
521 | /** @var Volume $volume */ |
||
522 | $volume = $event->volume; |
||
523 | if ($volume !== null) { |
||
524 | ImageOptimize::$plugin->optimizedImages->resaveVolumeAssets($volume); |
||
525 | } |
||
526 | } |
||
527 | } |
||
528 | ); |
||
529 | |||
530 | // Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN |
||
531 | Event::on( |
||
532 | Plugins::class, |
||
533 | Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
||
534 | function (PluginEvent $event) { |
||
535 | if ($event->plugin === $this) { |
||
536 | $request = Craft::$app->getRequest(); |
||
537 | if ($request->isCpRequest) { |
||
538 | Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('image-optimize/welcome'))->send(); |
||
539 | } |
||
540 | } |
||
541 | } |
||
542 | ); |
||
543 | } |
||
544 | |||
545 | /** |
||
546 | * Install our CraftQL event handlers |
||
547 | */ |
||
548 | protected function installCraftQLEventHandlers() |
||
549 | { |
||
550 | if (class_exists(CraftQL::class)) { |
||
551 | Event::on( |
||
552 | OptimizedImages::class, |
||
553 | GetCraftQLSchema::EVENT_GET_FIELD_SCHEMA, |
||
554 | [new GetCraftQLSchema, 'handle'] |
||
555 | ); |
||
556 | } |
||
557 | } |
||
558 | |||
559 | /** |
||
560 | * Install site event listeners for site requests only |
||
561 | */ |
||
562 | protected function installSiteEventListeners() |
||
563 | { |
||
564 | // Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES |
||
565 | Event::on( |
||
566 | UrlManager::class, |
||
567 | UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
||
568 | function (RegisterUrlRulesEvent $event) { |
||
569 | Craft::debug( |
||
570 | 'UrlManager::EVENT_REGISTER_SITE_URL_RULES', |
||
571 | __METHOD__ |
||
572 | ); |
||
573 | // Register our Control Panel routes |
||
574 | $event->rules = array_merge( |
||
575 | $event->rules, |
||
576 | $this->customFrontendRoutes() |
||
577 | ); |
||
578 | } |
||
579 | ); |
||
580 | } |
||
581 | |||
582 | /** |
||
583 | * Install site event listeners for cp requests only |
||
584 | */ |
||
585 | protected function installCpEventListeners() |
||
586 | { |
||
587 | // Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS |
||
588 | Event::on( |
||
589 | Plugins::class, |
||
590 | Plugins::EVENT_AFTER_LOAD_PLUGINS, |
||
591 | function () { |
||
592 | // Install these only after all other plugins have loaded |
||
593 | Event::on( |
||
594 | View::class, |
||
595 | View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, |
||
596 | function (RegisterTemplateRootsEvent $e) { |
||
597 | // Register the root directodies |
||
598 | $allImageTransformTypes = ImageOptimize::$plugin->optimize->getAllImageTransformTypes(); |
||
599 | /** @var ImageTransformInterface $imageTransformType */ |
||
600 | foreach ($allImageTransformTypes as $imageTransformType) { |
||
601 | list($id, $baseDir) = $imageTransformType::getTemplatesRoot(); |
||
602 | if (is_dir($baseDir)) { |
||
603 | $e->roots[$id] = $baseDir; |
||
604 | } |
||
605 | } |
||
606 | } |
||
607 | ); |
||
608 | } |
||
609 | ); |
||
610 | } |
||
611 | |||
612 | /** |
||
613 | * Return the custom frontend routes |
||
614 | * |
||
615 | * @return array |
||
616 | */ |
||
617 | protected function customFrontendRoutes(): array |
||
620 | ]; |
||
621 | } |
||
622 | |||
623 | /** |
||
624 | * If the Field being saved is an OptimizedImages field, re-save the |
||
625 | * responsive image variants automatically |
||
626 | * |
||
627 | * @param FieldEvent $event |
||
628 | * |
||
629 | * @throws \yii\base\InvalidConfigException |
||
630 | */ |
||
631 | protected function checkForOptimizedImagesField(FieldEvent $event) |
||
658 |