| Total Complexity | 43 |
| Total Lines | 511 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 73 | class ImageOptimize extends Plugin |
||
| 74 | { |
||
| 75 | |||
| 76 | // Constants |
||
| 77 | // ========================================================================= |
||
| 78 | |||
| 79 | const CRAFTQL_PLUGIN_HANDLE = 'craftql'; |
||
| 80 | |||
| 81 | // Static Properties |
||
| 82 | // ========================================================================= |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var ImageOptimize |
||
| 86 | */ |
||
| 87 | public static $plugin; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | public static $transformParams = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var bool |
||
| 96 | */ |
||
| 97 | public static $generatePlaceholders = true; |
||
| 98 | |||
| 99 | // Public Methods |
||
| 100 | // ========================================================================= |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @inheritdoc |
||
| 104 | */ |
||
| 105 | public function init() |
||
| 106 | { |
||
| 107 | parent::init(); |
||
| 108 | self::$plugin = $this; |
||
| 109 | // Handle any console commands |
||
| 110 | $request = Craft::$app->getRequest(); |
||
| 111 | if ($request->getIsConsoleRequest()) { |
||
| 112 | $this->controllerNamespace = 'nystudio107\imageoptimize\console\controllers'; |
||
| 113 | } |
||
| 114 | // Set the image transform component |
||
| 115 | $this->setImageTransformComponent(); |
||
| 116 | // Add in our Craft components |
||
| 117 | $this->addComponents(); |
||
| 118 | // Install our global event handlers |
||
| 119 | $this->installEventHandlers(); |
||
| 120 | // Log that the plugin has loaded |
||
| 121 | Craft::info( |
||
| 122 | Craft::t( |
||
| 123 | 'image-optimize', |
||
| 124 | '{name} plugin loaded', |
||
| 125 | ['name' => $this->name] |
||
| 126 | ), |
||
| 127 | __METHOD__ |
||
| 128 | ); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @inheritdoc |
||
| 133 | */ |
||
| 134 | public function getSettingsResponse() |
||
| 135 | { |
||
| 136 | $view = Craft::$app->getView(); |
||
| 137 | $namespace = $view->getNamespace(); |
||
| 138 | $view->setNamespace('settings'); |
||
| 139 | $settingsHtml = $this->settingsHtml(); |
||
| 140 | $view->setNamespace($namespace); |
||
| 141 | /** @var Controller $controller */ |
||
| 142 | $controller = Craft::$app->controller; |
||
| 143 | |||
| 144 | return $controller->renderTemplate('image-optimize/settings/index.twig', [ |
||
| 145 | 'plugin' => $this, |
||
| 146 | 'settingsHtml' => $settingsHtml, |
||
| 147 | ]); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @inheritdoc |
||
| 152 | */ |
||
| 153 | public function settingsHtml() |
||
| 154 | { |
||
| 155 | // Get only the user-editable settings |
||
| 156 | $settings = $this->getSettings(); |
||
| 157 | |||
| 158 | // Get the image transform types |
||
| 159 | $allImageTransformTypes = ImageOptimize::$plugin->optimize->getAllImageTransformTypes(); |
||
| 160 | $imageTransformTypeOptions = []; |
||
| 161 | /** @var ImageTransformInterface $class */ |
||
| 162 | foreach ($allImageTransformTypes as $class) { |
||
| 163 | if ($class::isSelectable()) { |
||
| 164 | $imageTransformTypeOptions[] = [ |
||
| 165 | 'value' => $class, |
||
| 166 | 'label' => $class::displayName(), |
||
| 167 | ]; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | // Sort them by name |
||
| 171 | ArrayHelper::multisort($imageTransformTypeOptions, 'label'); |
||
| 172 | |||
| 173 | // Render the settings template |
||
| 174 | try { |
||
| 175 | return Craft::$app->getView()->renderTemplate( |
||
| 176 | 'image-optimize/settings/_settings.twig', |
||
| 177 | [ |
||
| 178 | 'settings' => $settings, |
||
| 179 | 'gdInstalled' => \function_exists('imagecreatefromjpeg'), |
||
| 180 | 'imageTransformTypeOptions' => $imageTransformTypeOptions, |
||
| 181 | 'allImageTransformTypes' => $allImageTransformTypes, |
||
| 182 | 'imageTransform' => ImageOptimize::$plugin->transformMethod, |
||
| 183 | ] |
||
| 184 | ); |
||
| 185 | } catch (\Twig_Error_Loader $e) { |
||
| 186 | Craft::error($e->getMessage(), __METHOD__); |
||
| 187 | } catch (Exception $e) { |
||
| 188 | Craft::error($e->getMessage(), __METHOD__); |
||
| 189 | } |
||
| 190 | |||
| 191 | return ''; |
||
| 192 | } |
||
| 193 | |||
| 194 | // Protected Methods |
||
| 195 | // ========================================================================= |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @inheritdoc |
||
| 199 | */ |
||
| 200 | protected function createSettingsModel() |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Set the transformMethod component |
||
| 207 | */ |
||
| 208 | protected function setImageTransformComponent() |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Add in our Craft components |
||
| 225 | */ |
||
| 226 | protected function addComponents() |
||
| 227 | { |
||
| 228 | // Register our variables |
||
| 229 | Event::on( |
||
| 230 | CraftVariable::class, |
||
| 231 | CraftVariable::EVENT_INIT, |
||
| 232 | function (Event $event) { |
||
| 233 | /** @var CraftVariable $variable */ |
||
| 234 | $variable = $event->sender; |
||
| 235 | $variable->set('imageOptimize', ImageOptimizeVariable::class); |
||
| 236 | } |
||
| 237 | ); |
||
| 238 | |||
| 239 | // Register our Field |
||
| 240 | Event::on( |
||
| 241 | Fields::class, |
||
| 242 | Fields::EVENT_REGISTER_FIELD_TYPES, |
||
| 243 | function (RegisterComponentTypesEvent $event) { |
||
| 244 | Craft::debug( |
||
| 245 | 'Fields::EVENT_REGISTER_FIELD_TYPES', |
||
| 246 | __METHOD__ |
||
| 247 | ); |
||
| 248 | $event->types[] = OptimizedImages::class; |
||
| 249 | } |
||
| 250 | ); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Install our event handlers |
||
| 255 | */ |
||
| 256 | protected function installEventHandlers() |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Install our Asset event handlers |
||
| 271 | */ |
||
| 272 | protected function installAssetEventHandlers() |
||
| 273 | { |
||
| 274 | // Handler: Assets::EVENT_GET_ASSET_URL |
||
| 275 | Event::on( |
||
| 276 | Assets::class, |
||
| 277 | Assets::EVENT_GET_ASSET_URL, |
||
| 278 | function (GetAssetUrlEvent $event) { |
||
| 279 | Craft::debug( |
||
| 280 | 'Assets::EVENT_GET_ASSET_URL', |
||
| 281 | __METHOD__ |
||
| 282 | ); |
||
| 283 | // Return the URL to the asset URL or null to let Craft handle it |
||
| 284 | $event->url = ImageOptimize::$plugin->optimize->handleGetAssetUrlEvent( |
||
| 285 | $event |
||
| 286 | ); |
||
| 287 | } |
||
| 288 | ); |
||
| 289 | |||
| 290 | // Handler: Assets::EVENT_GET_ASSET_THUMB_URL |
||
| 291 | Event::on( |
||
| 292 | Assets::class, |
||
| 293 | Assets::EVENT_GET_ASSET_THUMB_URL, |
||
| 294 | function (GetAssetThumbUrlEvent $event) { |
||
| 295 | Craft::debug( |
||
| 296 | 'Assets::EVENT_GET_ASSET_THUMB_URL', |
||
| 297 | __METHOD__ |
||
| 298 | ); |
||
| 299 | // Return the URL to the asset URL or null to let Craft handle it |
||
| 300 | $event->url = ImageOptimize::$plugin->optimize->handleGetAssetThumbUrlEvent( |
||
| 301 | $event |
||
| 302 | ); |
||
| 303 | } |
||
| 304 | ); |
||
| 305 | |||
| 306 | // Handler: AssetTransforms::EVENT_GENERATE_TRANSFORM |
||
| 307 | Event::on( |
||
| 308 | AssetTransforms::class, |
||
| 309 | AssetTransforms::EVENT_GENERATE_TRANSFORM, |
||
| 310 | function (GenerateTransformEvent $event) { |
||
| 311 | Craft::debug( |
||
| 312 | 'AssetTransforms::EVENT_GENERATE_TRANSFORM', |
||
| 313 | __METHOD__ |
||
| 314 | ); |
||
| 315 | // Return the path to the optimized image to _createTransformForAsset() |
||
| 316 | $event->tempPath = ImageOptimize::$plugin->optimize->handleGenerateTransformEvent( |
||
| 317 | $event |
||
| 318 | ); |
||
| 319 | } |
||
| 320 | ); |
||
| 321 | |||
| 322 | // Handler: AssetTransforms::EVENT_AFTER_DELETE_TRANSFORMS |
||
| 323 | Event::on( |
||
| 324 | AssetTransforms::class, |
||
| 325 | AssetTransforms::EVENT_AFTER_DELETE_TRANSFORMS, |
||
| 326 | function (AssetTransformImageEvent $event) { |
||
| 327 | Craft::debug( |
||
| 328 | 'AssetTransforms::EVENT_AFTER_DELETE_TRANSFORMS', |
||
| 329 | __METHOD__ |
||
| 330 | ); |
||
| 331 | // Clean up any stray variant files |
||
| 332 | ImageOptimize::$plugin->optimize->handleAfterDeleteTransformsEvent( |
||
| 333 | $event |
||
| 334 | ); |
||
| 335 | } |
||
| 336 | ); |
||
| 337 | |||
| 338 | // Handler: Assets::EVENT_BEFORE_REPLACE_ASSET |
||
| 339 | Event::on( |
||
| 340 | Assets::class, |
||
| 341 | Assets::EVENT_BEFORE_REPLACE_ASSET, |
||
| 342 | function (ReplaceAssetEvent $event) { |
||
| 343 | Craft::debug( |
||
| 344 | 'Assets::EVENT_BEFORE_REPLACE_ASSET', |
||
| 345 | __METHOD__ |
||
| 346 | ); |
||
| 347 | /** @var Asset $element */ |
||
| 348 | $element = $event->asset; |
||
| 349 | // Purge the URL |
||
| 350 | $purgeUrl = ImageOptimize::$plugin->transformMethod->getPurgeUrl( |
||
| 351 | $element, |
||
| 352 | ImageOptimize::$transformParams |
||
| 353 | ); |
||
| 354 | if ($purgeUrl) { |
||
| 355 | ImageOptimize::$plugin->transformMethod->purgeUrl($purgeUrl, ImageOptimize::$transformParams); |
||
| 356 | } |
||
| 357 | } |
||
| 358 | ); |
||
| 359 | |||
| 360 | // Handler: Assets::EVENT_AFTER_REPLACE_ASSET |
||
| 361 | Event::on( |
||
| 362 | Assets::class, |
||
| 363 | Assets::EVENT_AFTER_REPLACE_ASSET, |
||
| 364 | function (ReplaceAssetEvent $event) { |
||
| 365 | Craft::debug( |
||
| 366 | 'Assets::EVENT_AFTER_REPLACE_ASSET', |
||
| 367 | __METHOD__ |
||
| 368 | ); |
||
| 369 | /** @var Asset $element */ |
||
| 370 | $element = $event->asset; |
||
| 371 | if ($element->id !== null) { |
||
| 372 | ImageOptimize::$plugin->optimizedImages->resaveAsset($element->id); |
||
| 373 | } |
||
| 374 | } |
||
| 375 | ); |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Install our Element event handlers |
||
| 380 | */ |
||
| 381 | protected function installElementEventHandlers() |
||
| 425 | } |
||
| 426 | } |
||
| 427 | ); |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Install our miscellaneous event handlers |
||
| 432 | */ |
||
| 433 | protected function installMiscEventHandlers() |
||
| 434 | { |
||
| 435 | // Handler: Fields::EVENT_AFTER_SAVE_FIELD |
||
| 436 | Event::on( |
||
| 437 | Fields::class, |
||
| 438 | Fields::EVENT_AFTER_SAVE_FIELD, |
||
| 439 | function (FieldEvent $event) { |
||
| 440 | Craft::debug( |
||
| 441 | 'Fields::EVENT_AFTER_SAVE_FIELD', |
||
| 442 | __METHOD__ |
||
| 443 | ); |
||
| 444 | $settings = $this->getSettings(); |
||
| 445 | /** @var Field $field */ |
||
| 446 | if (!$event->isNew && $settings->automaticallyResaveImageVariants) { |
||
| 447 | $this->checkForOptimizedImagesField($event); |
||
| 448 | } |
||
| 449 | } |
||
| 450 | ); |
||
| 451 | |||
| 452 | // Handler: Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS |
||
| 453 | Event::on( |
||
| 454 | Plugins::class, |
||
| 455 | Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS, |
||
| 456 | function (PluginEvent $event) { |
||
| 457 | if ($event->plugin === $this) { |
||
| 458 | Craft::debug( |
||
| 459 | 'Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS', |
||
| 460 | __METHOD__ |
||
| 461 | ); |
||
| 462 | $settings = $this->getSettings(); |
||
| 463 | if ($settings->automaticallyResaveImageVariants) { |
||
| 464 | // After they have changed the settings, resave all of the assets |
||
| 465 | ImageOptimize::$plugin->optimizedImages->resaveAllVolumesAssets(); |
||
| 466 | } |
||
| 467 | } |
||
| 468 | } |
||
| 469 | ); |
||
| 470 | |||
| 471 | // Handler: Volumes::EVENT_AFTER_SAVE_VOLUME |
||
| 472 | Event::on( |
||
| 473 | Volumes::class, |
||
| 474 | Volumes::EVENT_AFTER_SAVE_VOLUME, |
||
| 475 | function (VolumeEvent $event) { |
||
| 476 | Craft::debug( |
||
| 477 | 'Volumes::EVENT_AFTER_SAVE_VOLUME', |
||
| 478 | __METHOD__ |
||
| 479 | ); |
||
| 480 | $settings = $this->getSettings(); |
||
| 481 | // Only worry about this volume if it's not new |
||
| 482 | if (!$event->isNew && $settings->automaticallyResaveImageVariants) { |
||
| 483 | /** @var Volume $volume */ |
||
| 484 | $volume = $event->volume; |
||
| 485 | if ($volume !== null) { |
||
| 486 | ImageOptimize::$plugin->optimizedImages->resaveVolumeAssets($volume); |
||
| 487 | } |
||
| 488 | } |
||
| 489 | } |
||
| 490 | ); |
||
| 491 | |||
| 492 | // Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN |
||
| 493 | Event::on( |
||
| 494 | Plugins::class, |
||
| 495 | Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
||
| 496 | function (PluginEvent $event) { |
||
| 497 | if ($event->plugin === $this) { |
||
| 498 | $request = Craft::$app->getRequest(); |
||
| 499 | if ($request->isCpRequest) { |
||
| 500 | Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('image-optimize/welcome'))->send(); |
||
| 501 | } |
||
| 502 | } |
||
| 503 | } |
||
| 504 | ); |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Install our CraftQL event handlers |
||
| 509 | */ |
||
| 510 | protected function installCraftQLEventHandlers() |
||
| 511 | { |
||
| 512 | if (class_exists(CraftQL::class)) { |
||
| 513 | Event::on( |
||
| 514 | OptimizedImages::class, |
||
| 515 | GetCraftQLSchema::EVENT_GET_FIELD_SCHEMA, |
||
| 516 | [new GetCraftQLSchema, 'handle'] |
||
| 517 | ); |
||
| 518 | } |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Install site event listeners for site requests only |
||
| 523 | */ |
||
| 524 | protected function installSiteEventListeners() |
||
| 525 | { |
||
| 526 | // Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES |
||
| 527 | Event::on( |
||
| 528 | UrlManager::class, |
||
| 529 | UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
||
| 530 | function (RegisterUrlRulesEvent $event) { |
||
| 531 | Craft::debug( |
||
| 532 | 'UrlManager::EVENT_REGISTER_SITE_URL_RULES', |
||
| 533 | __METHOD__ |
||
| 534 | ); |
||
| 535 | // Register our Control Panel routes |
||
| 536 | $event->rules = array_merge( |
||
| 537 | $event->rules, |
||
| 538 | $this->customFrontendRoutes() |
||
| 539 | ); |
||
| 540 | } |
||
| 541 | ); |
||
| 542 | } |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Return the custom frontend routes |
||
| 546 | * |
||
| 547 | * @return array |
||
| 548 | */ |
||
| 549 | protected function customFrontendRoutes(): array |
||
| 552 | ]; |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * If the Field being saved is an OptimizedImages field, re-save the |
||
| 557 | * responsive image variants automatically |
||
| 558 | * |
||
| 559 | * @param FieldEvent $event |
||
| 560 | * |
||
| 561 | * @throws \yii\base\InvalidConfigException |
||
| 562 | */ |
||
| 563 | protected function checkForOptimizedImagesField(FieldEvent $event) |
||
| 590 |