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