Total Complexity | 50 |
Total Lines | 598 |
Duplicated Lines | 0 % |
Changes | 8 | ||
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 |
||
82 | class ImageOptimize extends Plugin |
||
83 | { |
||
84 | |||
85 | // Constants |
||
86 | // ========================================================================= |
||
87 | |||
88 | const CRAFTQL_PLUGIN_HANDLE = 'craftql'; |
||
89 | |||
90 | // Static Properties |
||
91 | // ========================================================================= |
||
92 | |||
93 | /** |
||
94 | * @var ImageOptimize |
||
95 | */ |
||
96 | public static $plugin; |
||
97 | |||
98 | /** |
||
99 | * @var bool |
||
100 | */ |
||
101 | public static $generatePlaceholders = true; |
||
102 | |||
103 | /** |
||
104 | * @var bool |
||
105 | */ |
||
106 | public static $craft31 = false; |
||
107 | |||
108 | /** |
||
109 | * @var bool |
||
110 | */ |
||
111 | public static $craft35 = false; |
||
112 | |||
113 | // Static Methods |
||
114 | // ========================================================================= |
||
115 | |||
116 | /** |
||
117 | * @inheritdoc |
||
118 | */ |
||
119 | public function __construct($id, $parent = null, array $config = []) |
||
120 | { |
||
121 | $config['components'] = [ |
||
122 | 'optimize' => OptimizeService::class, |
||
123 | 'optimizedImages' => OptimizedImagesService::class, |
||
124 | 'placeholder' => PlaceholderService::class, |
||
125 | // Register the manifest service |
||
126 | 'manifest' => [ |
||
127 | 'class' => ManifestService::class, |
||
128 | 'assetClass' => ImageOptimizeAsset::class, |
||
129 | 'devServerManifestPath' => 'http://craft-imageoptimize-buildchain:8080/', |
||
130 | 'devServerPublicPath' => 'http://craft-imageoptimize-buildchain:8080/', |
||
131 | ], |
||
132 | ]; |
||
133 | |||
134 | parent::__construct($id, $parent, $config); |
||
135 | } |
||
136 | |||
137 | // Public Properties |
||
138 | // ========================================================================= |
||
139 | |||
140 | /** |
||
141 | * @var string |
||
142 | */ |
||
143 | public $schemaVersion = '1.0.0'; |
||
144 | |||
145 | /** |
||
146 | * @var bool |
||
147 | */ |
||
148 | public $hasCpSection = false; |
||
149 | |||
150 | /** |
||
151 | * @var bool |
||
152 | */ |
||
153 | public $hasCpSettings = true; |
||
154 | |||
155 | // Public Methods |
||
156 | // ========================================================================= |
||
157 | |||
158 | /** |
||
159 | * @inheritdoc |
||
160 | */ |
||
161 | public function init() |
||
186 | ); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @inheritdoc |
||
191 | */ |
||
192 | public function getSettingsResponse() |
||
193 | { |
||
194 | $view = Craft::$app->getView(); |
||
195 | $namespace = $view->getNamespace(); |
||
196 | $view->setNamespace('settings'); |
||
197 | $settingsHtml = $this->settingsHtml(); |
||
198 | $view->setNamespace($namespace); |
||
199 | /** @var Controller $controller */ |
||
200 | $controller = Craft::$app->controller; |
||
201 | |||
202 | return $controller->renderTemplate('image-optimize/settings/index.twig', [ |
||
203 | 'plugin' => $this, |
||
204 | 'settingsHtml' => $settingsHtml, |
||
205 | ]); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @inheritdoc |
||
210 | */ |
||
211 | public function settingsHtml() |
||
212 | { |
||
213 | // Get only the user-editable settings |
||
214 | $settings = $this->getSettings(); |
||
215 | |||
216 | // Get the image transform types |
||
217 | $allImageTransformTypes = ImageOptimize::$plugin->optimize->getAllImageTransformTypes(); |
||
218 | $imageTransformTypeOptions = []; |
||
219 | /** @var ImageTransformInterface $class */ |
||
220 | foreach ($allImageTransformTypes as $class) { |
||
221 | if ($class::isSelectable()) { |
||
222 | $imageTransformTypeOptions[] = [ |
||
223 | 'value' => $class, |
||
224 | 'label' => $class::displayName(), |
||
225 | ]; |
||
226 | } |
||
227 | } |
||
228 | // Sort them by name |
||
229 | ArrayHelper::multisort($imageTransformTypeOptions, 'label'); |
||
230 | |||
231 | // Render the settings template |
||
232 | try { |
||
233 | return Craft::$app->getView()->renderTemplate( |
||
234 | 'image-optimize/settings/_settings.twig', |
||
235 | [ |
||
236 | 'settings' => $settings, |
||
237 | 'gdInstalled' => \function_exists('imagecreatefromjpeg'), |
||
238 | 'imageTransformTypeOptions' => $imageTransformTypeOptions, |
||
239 | 'allImageTransformTypes' => $allImageTransformTypes, |
||
240 | 'imageTransform' => ImageOptimize::$plugin->transformMethod, |
||
241 | ] |
||
242 | ); |
||
243 | } catch (\Twig\Error\LoaderError $e) { |
||
244 | Craft::error($e->getMessage(), __METHOD__); |
||
245 | } catch (Exception $e) { |
||
246 | Craft::error($e->getMessage(), __METHOD__); |
||
247 | } |
||
248 | |||
249 | return ''; |
||
250 | } |
||
251 | |||
252 | // Protected Methods |
||
253 | // ========================================================================= |
||
254 | |||
255 | /** |
||
256 | * @inheritdoc |
||
257 | */ |
||
258 | protected function createSettingsModel() |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Set the transformMethod component |
||
265 | */ |
||
266 | protected function setImageTransformComponent() |
||
267 | { |
||
268 | $settings = $this->getSettings(); |
||
269 | $definition = array_merge( |
||
270 | $settings->imageTransformTypeSettings[$settings->transformClass] ?? [], |
||
271 | ['class' => $settings->transformClass] |
||
272 | ); |
||
273 | try { |
||
274 | $this->set('transformMethod', $definition); |
||
275 | } catch (InvalidConfigException $e) { |
||
276 | Craft::error($e->getMessage(), __METHOD__); |
||
277 | } |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Add in our Craft components |
||
282 | */ |
||
283 | protected function addComponents() |
||
284 | { |
||
285 | // Register our variables |
||
286 | Event::on( |
||
287 | CraftVariable::class, |
||
288 | CraftVariable::EVENT_INIT, |
||
289 | function (Event $event) { |
||
290 | /** @var CraftVariable $variable */ |
||
291 | $variable = $event->sender; |
||
292 | $variable->set('imageOptimize', [ |
||
293 | 'class' => ImageOptimizeVariable::class, |
||
294 | 'manifestService' => $this->manifest, |
||
295 | ]); |
||
296 | } |
||
297 | ); |
||
298 | |||
299 | // Register our Field |
||
300 | Event::on( |
||
301 | Fields::class, |
||
302 | Fields::EVENT_REGISTER_FIELD_TYPES, |
||
303 | function (RegisterComponentTypesEvent $event) { |
||
304 | Craft::debug( |
||
305 | 'Fields::EVENT_REGISTER_FIELD_TYPES', |
||
306 | __METHOD__ |
||
307 | ); |
||
308 | $event->types[] = OptimizedImages::class; |
||
309 | } |
||
310 | ); |
||
311 | |||
312 | // Register our Utility only if they are using the CraftImageTransform method |
||
313 | if (ImageOptimize::$plugin->transformMethod instanceof CraftImageTransform) { |
||
314 | Event::on( |
||
315 | Utilities::class, |
||
316 | Utilities::EVENT_REGISTER_UTILITY_TYPES, |
||
317 | function (RegisterComponentTypesEvent $event) { |
||
318 | $event->types[] = ImageOptimizeUtility::class; |
||
319 | } |
||
320 | ); |
||
321 | } |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Install our event handlers |
||
326 | */ |
||
327 | protected function installEventHandlers() |
||
328 | { |
||
329 | $this->installAssetEventHandlers(); |
||
330 | $this->installElementEventHandlers(); |
||
331 | $this->installMiscEventHandlers(); |
||
332 | $this->installCraftQLEventHandlers(); |
||
333 | $request = Craft::$app->getRequest(); |
||
334 | // Install only for non-console site requests |
||
335 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
||
336 | $this->installSiteEventListeners(); |
||
337 | } |
||
338 | // Install only for non-console cp requests |
||
339 | if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) { |
||
340 | $this->installCpEventListeners(); |
||
341 | } |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Install our Asset event handlers |
||
346 | */ |
||
347 | protected function installAssetEventHandlers() |
||
348 | { |
||
349 | // Handler: Assets::EVENT_GET_ASSET_URL |
||
350 | Event::on( |
||
351 | Assets::class, |
||
352 | Assets::EVENT_GET_ASSET_URL, |
||
353 | function (GetAssetUrlEvent $event) { |
||
354 | Craft::debug( |
||
355 | 'Assets::EVENT_GET_ASSET_URL', |
||
356 | __METHOD__ |
||
357 | ); |
||
358 | // Return the URL to the asset URL or null to let Craft handle it |
||
359 | $event->url = ImageOptimize::$plugin->optimize->handleGetAssetUrlEvent( |
||
360 | $event |
||
361 | ); |
||
362 | } |
||
363 | ); |
||
364 | |||
365 | // Handler: Assets::EVENT_GET_ASSET_THUMB_URL |
||
366 | Event::on( |
||
367 | Assets::class, |
||
368 | Assets::EVENT_GET_ASSET_THUMB_URL, |
||
369 | function (GetAssetThumbUrlEvent $event) { |
||
370 | Craft::debug( |
||
371 | 'Assets::EVENT_GET_ASSET_THUMB_URL', |
||
372 | __METHOD__ |
||
373 | ); |
||
374 | // Return the URL to the asset URL or null to let Craft handle it |
||
375 | $event->url = ImageOptimize::$plugin->optimize->handleGetAssetThumbUrlEvent( |
||
376 | $event |
||
377 | ); |
||
378 | } |
||
379 | ); |
||
380 | |||
381 | // Handler: AssetTransforms::EVENT_GENERATE_TRANSFORM |
||
382 | Event::on( |
||
383 | AssetTransforms::class, |
||
384 | AssetTransforms::EVENT_GENERATE_TRANSFORM, |
||
385 | function (GenerateTransformEvent $event) { |
||
386 | Craft::debug( |
||
387 | 'AssetTransforms::EVENT_GENERATE_TRANSFORM', |
||
388 | __METHOD__ |
||
389 | ); |
||
390 | // Return the path to the optimized image to _createTransformForAsset() |
||
391 | $event->tempPath = ImageOptimize::$plugin->optimize->handleGenerateTransformEvent( |
||
392 | $event |
||
393 | ); |
||
394 | } |
||
395 | ); |
||
396 | |||
397 | // Handler: AssetTransforms::EVENT_AFTER_DELETE_TRANSFORMS |
||
398 | Event::on( |
||
399 | AssetTransforms::class, |
||
400 | AssetTransforms::EVENT_AFTER_DELETE_TRANSFORMS, |
||
401 | function (AssetTransformImageEvent $event) { |
||
402 | Craft::debug( |
||
403 | 'AssetTransforms::EVENT_AFTER_DELETE_TRANSFORMS', |
||
404 | __METHOD__ |
||
405 | ); |
||
406 | // Clean up any stray variant files |
||
407 | ImageOptimize::$plugin->optimize->handleAfterDeleteTransformsEvent( |
||
408 | $event |
||
409 | ); |
||
410 | } |
||
411 | ); |
||
412 | |||
413 | // Handler: Assets::EVENT_BEFORE_REPLACE_ASSET |
||
414 | Event::on( |
||
415 | Assets::class, |
||
416 | Assets::EVENT_BEFORE_REPLACE_ASSET, |
||
417 | function (ReplaceAssetEvent $event) { |
||
418 | Craft::debug( |
||
419 | 'Assets::EVENT_BEFORE_REPLACE_ASSET', |
||
420 | __METHOD__ |
||
421 | ); |
||
422 | /** @var Asset $element */ |
||
423 | $element = $event->asset; |
||
424 | // Purge the URL |
||
425 | $purgeUrl = ImageOptimize::$plugin->transformMethod->getPurgeUrl($element); |
||
426 | if ($purgeUrl) { |
||
427 | ImageOptimize::$plugin->transformMethod->purgeUrl($purgeUrl); |
||
428 | } |
||
429 | } |
||
430 | ); |
||
431 | |||
432 | // Handler: Assets::EVENT_AFTER_REPLACE_ASSET |
||
433 | Event::on( |
||
434 | Assets::class, |
||
435 | Assets::EVENT_AFTER_REPLACE_ASSET, |
||
436 | function (ReplaceAssetEvent $event) { |
||
437 | Craft::debug( |
||
438 | 'Assets::EVENT_AFTER_REPLACE_ASSET', |
||
439 | __METHOD__ |
||
440 | ); |
||
441 | /** @var Asset $element */ |
||
442 | $element = $event->asset; |
||
443 | if ($element->id !== null) { |
||
444 | ImageOptimize::$plugin->optimizedImages->resaveAsset($element->id); |
||
445 | } |
||
446 | } |
||
447 | ); |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * Install our Element event handlers |
||
452 | */ |
||
453 | protected function installElementEventHandlers() |
||
491 | } |
||
492 | } |
||
493 | ); |
||
494 | } |
||
495 | |||
496 | /** |
||
497 | * Install our miscellaneous event handlers |
||
498 | */ |
||
499 | protected function installMiscEventHandlers() |
||
500 | { |
||
501 | // Handler: Fields::EVENT_AFTER_SAVE_FIELD |
||
502 | Event::on( |
||
503 | Fields::class, |
||
504 | Fields::EVENT_AFTER_SAVE_FIELD, |
||
505 | function (FieldEvent $event) { |
||
506 | Craft::debug( |
||
507 | 'Fields::EVENT_AFTER_SAVE_FIELD', |
||
508 | __METHOD__ |
||
509 | ); |
||
510 | $settings = $this->getSettings(); |
||
511 | /** @var Field $field */ |
||
512 | if (!$event->isNew && $settings->automaticallyResaveImageVariants) { |
||
513 | $this->checkForOptimizedImagesField($event); |
||
514 | } |
||
515 | } |
||
516 | ); |
||
517 | |||
518 | // Handler: Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS |
||
519 | Event::on( |
||
520 | Plugins::class, |
||
521 | Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS, |
||
522 | function (PluginEvent $event) { |
||
523 | if ($event->plugin === $this) { |
||
524 | Craft::debug( |
||
525 | 'Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS', |
||
526 | __METHOD__ |
||
527 | ); |
||
528 | $settings = $this->getSettings(); |
||
529 | if ($settings->automaticallyResaveImageVariants) { |
||
530 | // After they have changed the settings, resave all of the assets |
||
531 | ImageOptimize::$plugin->optimizedImages->resaveAllVolumesAssets(); |
||
532 | } |
||
533 | } |
||
534 | } |
||
535 | ); |
||
536 | |||
537 | // Handler: Volumes::EVENT_AFTER_SAVE_VOLUME |
||
538 | Event::on( |
||
539 | Volumes::class, |
||
540 | Volumes::EVENT_AFTER_SAVE_VOLUME, |
||
541 | function (VolumeEvent $event) { |
||
542 | Craft::debug( |
||
543 | 'Volumes::EVENT_AFTER_SAVE_VOLUME', |
||
544 | __METHOD__ |
||
545 | ); |
||
546 | $settings = $this->getSettings(); |
||
547 | // Only worry about this volume if it's not new |
||
548 | if (!$event->isNew && $settings->automaticallyResaveImageVariants) { |
||
549 | /** @var Volume $volume */ |
||
550 | $volume = $event->volume; |
||
551 | if ($volume !== null) { |
||
552 | ImageOptimize::$plugin->optimizedImages->resaveVolumeAssets($volume); |
||
553 | } |
||
554 | } |
||
555 | } |
||
556 | ); |
||
557 | |||
558 | // Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN |
||
559 | Event::on( |
||
560 | Plugins::class, |
||
561 | Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
||
562 | function (PluginEvent $event) { |
||
563 | if ($event->plugin === $this) { |
||
564 | $request = Craft::$app->getRequest(); |
||
565 | if ($request->isCpRequest) { |
||
566 | Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('image-optimize/welcome'))->send(); |
||
567 | } |
||
568 | } |
||
569 | } |
||
570 | ); |
||
571 | } |
||
572 | |||
573 | /** |
||
574 | * Install our CraftQL event handlers |
||
575 | */ |
||
576 | protected function installCraftQLEventHandlers() |
||
577 | { |
||
578 | if (class_exists(CraftQL::class)) { |
||
579 | Event::on( |
||
580 | OptimizedImages::class, |
||
581 | GetCraftQLSchema::EVENT_GET_FIELD_SCHEMA, |
||
582 | [new GetCraftQLSchema, 'handle'] |
||
583 | ); |
||
584 | } |
||
585 | } |
||
586 | |||
587 | /** |
||
588 | * Install site event listeners for site requests only |
||
589 | */ |
||
590 | protected function installSiteEventListeners() |
||
591 | { |
||
592 | // Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES |
||
593 | Event::on( |
||
594 | UrlManager::class, |
||
595 | UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
||
596 | function (RegisterUrlRulesEvent $event) { |
||
597 | Craft::debug( |
||
598 | 'UrlManager::EVENT_REGISTER_SITE_URL_RULES', |
||
599 | __METHOD__ |
||
600 | ); |
||
601 | // Register our Control Panel routes |
||
602 | $event->rules = array_merge( |
||
603 | $event->rules, |
||
604 | $this->customFrontendRoutes() |
||
605 | ); |
||
606 | } |
||
607 | ); |
||
608 | } |
||
609 | |||
610 | /** |
||
611 | * Install site event listeners for cp requests only |
||
612 | */ |
||
613 | protected function installCpEventListeners() |
||
614 | { |
||
615 | // Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS |
||
616 | Event::on( |
||
617 | Plugins::class, |
||
618 | Plugins::EVENT_AFTER_LOAD_PLUGINS, |
||
619 | function () { |
||
620 | // Install these only after all other plugins have loaded |
||
621 | Event::on( |
||
622 | View::class, |
||
623 | View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, |
||
624 | function (RegisterTemplateRootsEvent $e) { |
||
625 | // Register the root directodies |
||
626 | $allImageTransformTypes = ImageOptimize::$plugin->optimize->getAllImageTransformTypes(); |
||
627 | /** @var ImageTransformInterface $imageTransformType */ |
||
628 | foreach ($allImageTransformTypes as $imageTransformType) { |
||
629 | list($id, $baseDir) = $imageTransformType::getTemplatesRoot(); |
||
630 | if (is_dir($baseDir)) { |
||
631 | $e->roots[$id] = $baseDir; |
||
632 | } |
||
633 | } |
||
634 | } |
||
635 | ); |
||
636 | } |
||
637 | ); |
||
638 | } |
||
639 | |||
640 | /** |
||
641 | * Return the custom frontend routes |
||
642 | * |
||
643 | * @return array |
||
644 | */ |
||
645 | protected function customFrontendRoutes(): array |
||
648 | ]; |
||
649 | } |
||
650 | |||
651 | /** |
||
652 | * If the Field being saved is an OptimizedImages field, re-save the |
||
653 | * responsive image variants automatically |
||
654 | * |
||
655 | * @param FieldEvent $event |
||
656 | * |
||
657 | * @throws \yii\base\InvalidConfigException |
||
658 | */ |
||
659 | protected function checkForOptimizedImagesField(FieldEvent $event) |
||
686 |