| Total Complexity | 40 |
| Total Lines | 448 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Webperf 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 Webperf, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class Webperf extends Plugin |
||
| 50 | { |
||
| 51 | // Static Properties |
||
| 52 | // ========================================================================= |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var Webperf |
||
| 56 | */ |
||
| 57 | public static $plugin; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var Settings |
||
| 61 | */ |
||
| 62 | public static $settings; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var int|null |
||
| 66 | */ |
||
| 67 | public static $requestUuid; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var bool |
||
| 71 | */ |
||
| 72 | public static $beaconIncluded = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | public static $renderType = 'html'; |
||
| 78 | |||
| 79 | // Public Properties |
||
| 80 | // ========================================================================= |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | public $schemaVersion = '1.0.0'; |
||
| 86 | |||
| 87 | // Public Methods |
||
| 88 | // ========================================================================= |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @inheritdoc |
||
| 92 | */ |
||
| 93 | public function init() |
||
| 94 | { |
||
| 95 | parent::init(); |
||
| 96 | // Initialize properties |
||
| 97 | self::$plugin = $this; |
||
| 98 | self::$settings = $this->getSettings(); |
||
| 99 | try { |
||
| 100 | self::$requestUuid = random_int(0, PHP_INT_MAX); |
||
| 101 | } catch (\Exception $e) { |
||
| 102 | self::$requestUuid = null; |
||
| 103 | } |
||
| 104 | $this->name = self::$settings->pluginName; |
||
| 105 | // Add in our components |
||
| 106 | $this->addComponents(); |
||
| 107 | // Install event listeners |
||
| 108 | $this->installEventListeners(); |
||
| 109 | // Load that we've loaded |
||
| 110 | Craft::info( |
||
| 111 | Craft::t( |
||
| 112 | 'webperf', |
||
| 113 | '{name} plugin loaded', |
||
| 114 | ['name' => $this->name] |
||
| 115 | ), |
||
| 116 | __METHOD__ |
||
| 117 | ); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @inheritdoc |
||
| 122 | */ |
||
| 123 | public function getSettingsResponse() |
||
| 124 | { |
||
| 125 | // Just redirect to the plugin settings page |
||
| 126 | Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('webperf/settings')); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @inheritdoc |
||
| 131 | */ |
||
| 132 | public function getCpNavItem() |
||
| 133 | { |
||
| 134 | $subNavs = []; |
||
| 135 | $navItem = parent::getCpNavItem(); |
||
| 136 | $currentUser = Craft::$app->getUser()->getIdentity(); |
||
| 137 | // Only show sub-navs the user has permission to view |
||
| 138 | if ($currentUser->can('webperf:dashboard')) { |
||
| 139 | $subNavs['dashboard'] = [ |
||
| 140 | 'label' => 'Dashboard', |
||
| 141 | 'url' => 'webperf/dashboard', |
||
| 142 | ]; |
||
| 143 | } |
||
| 144 | if ($currentUser->can('webperf:pages')) { |
||
| 145 | $subNavs['pages'] = [ |
||
| 146 | 'label' => 'Pages', |
||
| 147 | 'url' => 'webperf/pages', |
||
| 148 | ]; |
||
| 149 | } |
||
| 150 | if ($currentUser->can('webperf:settings')) { |
||
| 151 | $subNavs['settings'] = [ |
||
| 152 | 'label' => 'Settings', |
||
| 153 | 'url' => 'webperf/settings', |
||
| 154 | ]; |
||
| 155 | } |
||
| 156 | $navItem = array_merge($navItem, [ |
||
| 157 | 'subnav' => $subNavs, |
||
| 158 | ]); |
||
| 159 | |||
| 160 | return $navItem; |
||
| 161 | } |
||
| 162 | |||
| 163 | // Protected Methods |
||
| 164 | // ========================================================================= |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Add in our components |
||
| 168 | */ |
||
| 169 | protected function addComponents() |
||
| 170 | { |
||
| 171 | // Add in the ProfileTarget component |
||
| 172 | try { |
||
| 173 | $this->set('profileTarget', [ |
||
| 174 | 'class' => ProfileTarget::class, |
||
| 175 | 'levels' => ['profile'], |
||
| 176 | 'categories' => [], |
||
| 177 | 'logVars' => [], |
||
| 178 | 'except' => [], |
||
| 179 | ]); |
||
| 180 | } catch (InvalidConfigException $e) { |
||
| 181 | Craft::error($e->getMessage(), __METHOD__); |
||
| 182 | } |
||
| 183 | // Attach our log target |
||
| 184 | Craft::$app->getLog()->targets['webperf'] = $this->profileTarget; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Install our event listeners. |
||
| 189 | */ |
||
| 190 | protected function installEventListeners() |
||
| 191 | { |
||
| 192 | $request = Craft::$app->getRequest(); |
||
| 193 | // Add in our event listeners that are needed for every request |
||
| 194 | $this->installGlobalEventListeners(); |
||
| 195 | // Install only for non-console site requests |
||
| 196 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
||
| 197 | $this->installSiteEventListeners(); |
||
| 198 | } |
||
| 199 | // Install only for non-console Control Panel requests |
||
| 200 | if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) { |
||
| 201 | $this->installCpEventListeners(); |
||
| 202 | } |
||
| 203 | // Handler: EVENT_AFTER_INSTALL_PLUGIN |
||
| 204 | Event::on( |
||
| 205 | Plugins::class, |
||
| 206 | Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
||
| 207 | function (PluginEvent $event) { |
||
| 208 | if ($event->plugin === $this) { |
||
| 209 | // Invalidate our caches after we've been installed |
||
| 210 | $this->clearAllCaches(); |
||
| 211 | // Send them to our welcome screen |
||
| 212 | $request = Craft::$app->getRequest(); |
||
| 213 | if ($request->isCpRequest) { |
||
| 214 | Craft::$app->getResponse()->redirect(UrlHelper::cpUrl( |
||
| 215 | 'webperf/dashboard', |
||
| 216 | [ |
||
| 217 | 'showWelcome' => true, |
||
| 218 | ] |
||
| 219 | ))->send(); |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } |
||
| 223 | ); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Install global event listeners for all request types |
||
| 228 | */ |
||
| 229 | protected function installGlobalEventListeners() |
||
| 230 | { |
||
| 231 | // Handler: CraftVariable::EVENT_INIT |
||
| 232 | Event::on( |
||
| 233 | CraftVariable::class, |
||
| 234 | CraftVariable::EVENT_INIT, |
||
| 235 | function (Event $event) { |
||
| 236 | /** @var CraftVariable $variable */ |
||
| 237 | $variable = $event->sender; |
||
| 238 | $variable->set('webperf', WebperfVariable::class); |
||
| 239 | } |
||
| 240 | ); |
||
| 241 | // Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS |
||
| 242 | Event::on( |
||
| 243 | Plugins::class, |
||
| 244 | Plugins::EVENT_AFTER_LOAD_PLUGINS, |
||
| 245 | function () { |
||
| 246 | // Install these only after all other plugins have loaded |
||
| 247 | $request = Craft::$app->getRequest(); |
||
| 248 | // Only respond to non-console site requests |
||
| 249 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
||
| 250 | $this->handleSiteRequest(); |
||
| 251 | } |
||
| 252 | // Respond to Control Panel requests |
||
| 253 | if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) { |
||
| 254 | $this->handleAdminCpRequest(); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | ); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Install site event listeners for site requests only |
||
| 262 | */ |
||
| 263 | protected function installSiteEventListeners() |
||
| 264 | { |
||
| 265 | // Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES |
||
| 266 | Event::on( |
||
| 267 | UrlManager::class, |
||
| 268 | UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
||
| 269 | function (RegisterUrlRulesEvent $event) { |
||
| 270 | Craft::debug( |
||
| 271 | 'UrlManager::EVENT_REGISTER_SITE_URL_RULES', |
||
| 272 | __METHOD__ |
||
| 273 | ); |
||
| 274 | // Register our Control Panel routes |
||
| 275 | $event->rules = array_merge( |
||
| 276 | $event->rules, |
||
| 277 | $this->customFrontendRoutes() |
||
| 278 | ); |
||
| 279 | } |
||
| 280 | ); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Install site event listeners for Control Panel requests only |
||
| 285 | */ |
||
| 286 | protected function installCpEventListeners() |
||
| 287 | { |
||
| 288 | // Handler: UrlManager::EVENT_REGISTER_CP_URL_RULES |
||
| 289 | Event::on( |
||
| 290 | UrlManager::class, |
||
| 291 | UrlManager::EVENT_REGISTER_CP_URL_RULES, |
||
| 292 | function (RegisterUrlRulesEvent $event) { |
||
| 293 | Craft::debug( |
||
| 294 | 'UrlManager::EVENT_REGISTER_CP_URL_RULES', |
||
| 295 | __METHOD__ |
||
| 296 | ); |
||
| 297 | // Register our Control Panel routes |
||
| 298 | $event->rules = array_merge( |
||
| 299 | $event->rules, |
||
| 300 | $this->customAdminCpRoutes() |
||
| 301 | ); |
||
| 302 | } |
||
| 303 | ); |
||
| 304 | // Handler: Dashboard::EVENT_REGISTER_WIDGET_TYPES |
||
| 305 | Event::on( |
||
| 306 | Dashboard::class, |
||
| 307 | Dashboard::EVENT_REGISTER_WIDGET_TYPES, |
||
| 308 | function (RegisterComponentTypesEvent $event) { |
||
| 309 | $event->types[] = MetricsWidget::class; |
||
| 310 | } |
||
| 311 | ); |
||
| 312 | // Handler: UserPermissions::EVENT_REGISTER_PERMISSIONS |
||
| 313 | Event::on( |
||
| 314 | UserPermissions::class, |
||
| 315 | UserPermissions::EVENT_REGISTER_PERMISSIONS, |
||
| 316 | function (RegisterUserPermissionsEvent $event) { |
||
| 317 | Craft::debug( |
||
| 318 | 'UserPermissions::EVENT_REGISTER_PERMISSIONS', |
||
| 319 | __METHOD__ |
||
| 320 | ); |
||
| 321 | // Register our custom permissions |
||
| 322 | $event->permissions[Craft::t('webperf', 'Webperf')] = $this->customAdminCpPermissions(); |
||
| 323 | } |
||
| 324 | ); |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Handle site requests. We do it only after we receive the event |
||
| 329 | * EVENT_AFTER_LOAD_PLUGINS so that any pending db migrations can be run |
||
| 330 | * before our event listeners kick in |
||
| 331 | */ |
||
| 332 | protected function handleSiteRequest() |
||
| 333 | { |
||
| 334 | // Don't include the beacon for response codes >= 400 |
||
| 335 | $response = Craft::$app->getResponse(); |
||
| 336 | if ($response->statusCode < 400) { |
||
| 337 | // Handler: View::EVENT_END_PAGE |
||
| 338 | Event::on( |
||
| 339 | View::class, |
||
| 340 | View::EVENT_END_PAGE, |
||
| 341 | function () { |
||
| 342 | Craft::debug( |
||
| 343 | 'View::EVENT_END_PAGE', |
||
| 344 | __METHOD__ |
||
| 345 | ); |
||
| 346 | $view = Craft::$app->getView(); |
||
| 347 | // The page is done rendering, include our beacon |
||
| 348 | if (Webperf::$settings->includeBeacon && $view->getIsRenderingPageTemplate()) { |
||
| 349 | switch (self::$renderType) { |
||
| 350 | case 'html': |
||
| 351 | Webperf::$plugin->beacons->includeHtmlBeacon(); |
||
| 352 | self::$beaconIncluded = true; |
||
| 353 | break; |
||
| 354 | case 'amp-html': |
||
| 355 | Webperf::$plugin->beacons->includeAmpHtmlScript(); |
||
| 356 | break; |
||
| 357 | } |
||
| 358 | } |
||
| 359 | } |
||
| 360 | ); |
||
| 361 | // Handler: View::EVENT_END_BODY |
||
| 362 | Event::on( |
||
| 363 | View::class, |
||
| 364 | View::EVENT_END_BODY, |
||
| 365 | function () { |
||
| 366 | Craft::debug( |
||
| 367 | 'View::EVENT_END_BODY', |
||
| 368 | __METHOD__ |
||
| 369 | ); |
||
| 370 | $view = Craft::$app->getView(); |
||
| 371 | // The page is done rendering, include our beacon |
||
| 372 | if (Webperf::$settings->includeBeacon && $view->getIsRenderingPageTemplate()) { |
||
| 373 | switch (self::$renderType) { |
||
| 374 | case 'html': |
||
| 375 | break; |
||
| 376 | case 'amp-html': |
||
| 377 | Webperf::$plugin->beacons->includeAmpHtmlBeacon(); |
||
| 378 | self::$beaconIncluded = true; |
||
| 379 | break; |
||
| 380 | } |
||
| 381 | } |
||
| 382 | } |
||
| 383 | ); |
||
| 384 | // Handler: Application::EVENT_AFTER_REQUEST |
||
| 385 | Event::on( |
||
| 386 | Application::class, |
||
| 387 | Application::EVENT_AFTER_REQUEST, |
||
| 388 | function () { |
||
| 389 | Craft::debug( |
||
| 390 | 'Application::EVENT_AFTER_REQUEST', |
||
| 391 | __METHOD__ |
||
| 392 | ); |
||
| 393 | } |
||
| 394 | ); |
||
| 395 | } |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Handle Control Panel requests. We do it only after we receive the event |
||
| 400 | * EVENT_AFTER_LOAD_PLUGINS so that any pending db migrations can be run |
||
| 401 | * before our event listeners kick in |
||
| 402 | */ |
||
| 403 | protected function handleAdminCpRequest() |
||
| 404 | { |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Clear all the caches! |
||
| 409 | */ |
||
| 410 | public function clearAllCaches() |
||
| 411 | { |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @inheritdoc |
||
| 416 | */ |
||
| 417 | protected function createSettingsModel() |
||
| 418 | { |
||
| 419 | return new Settings(); |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @inheritdoc |
||
| 424 | */ |
||
| 425 | protected function settingsHtml(): string |
||
| 426 | { |
||
| 427 | return Craft::$app->view->renderTemplate( |
||
| 428 | 'webperf/settings', |
||
| 429 | [ |
||
| 430 | 'settings' => $this->getSettings() |
||
| 431 | ] |
||
| 432 | ); |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Return the custom frontend routes |
||
| 437 | * |
||
| 438 | * @return array |
||
| 439 | */ |
||
| 440 | protected function customFrontendRoutes(): array |
||
| 441 | { |
||
| 442 | return [ |
||
| 443 | // Beacon |
||
| 444 | '/webperf/metrics/beacon' => 'webperf/metrics/beacon', |
||
| 445 | // Render |
||
| 446 | '/webperf/render/amp-iframe' => 'webperf/render/amp-iframe', |
||
| 447 | // Tables |
||
| 448 | '/webperf/tables/dashboard' => 'webperf/tables/dashboard', |
||
| 449 | '/webperf/tables/redirects' => 'webperf/tables/redirects', |
||
| 450 | // Charts |
||
| 451 | '/webperf/charts/dashboard-stats-average/<days:\d+>/<column:{handle}>' |
||
| 452 | => 'webperf/charts/dashboard-stats-average', |
||
| 453 | '/webperf/charts/dashboard-stats-average/<days:\d+>/<column:{handle}>/<siteId:\d+>' |
||
| 454 | => 'webperf/charts/dashboard-stats-average', |
||
| 455 | '/webperf/charts/dashboard-slowest-pages/<days:\d+>/<column:{handle}>/<limit:\d+>' |
||
| 456 | => 'webperf/charts/dashboard-slowest-pages', |
||
| 457 | '/webperf/charts/dashboard-slowest-pages/<days:\d+>/<column:{handle}>/<limit:\d+>/<siteId:\d+>' |
||
| 458 | => 'webperf/charts/dashboard-slowest-pages', |
||
| 459 | '/webperf/charts/widget/<days>' => 'webperf/charts/widget', |
||
| 460 | ]; |
||
| 461 | } |
||
| 462 | /** |
||
| 463 | * Return the custom Control Panel routes |
||
| 464 | * |
||
| 465 | * @return array |
||
| 466 | */ |
||
| 467 | protected function customAdminCpRoutes(): array |
||
| 468 | { |
||
| 469 | return [ |
||
| 470 | 'webperf' => 'webperf/sections/dashboard', |
||
| 471 | 'webperf/dashboard' => 'webperf/sections/dashboard', |
||
| 472 | 'webperf/dashboard/<siteHandle:{handle}>' => 'webperf/sections/dashboard', |
||
| 473 | |||
| 474 | 'webperf/pages' => 'webperf/sections/pages-index', |
||
| 475 | 'webperf/pages/<siteHandle:{handle}>' => 'webperf/sections/pages-index', |
||
| 476 | |||
| 477 | 'webperf/settings' => 'webperf/settings/plugin-settings', |
||
| 478 | ]; |
||
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Returns the custom Control Panel user permissions. |
||
| 483 | * |
||
| 484 | * @return array |
||
| 485 | */ |
||
| 486 | protected function customAdminCpPermissions(): array |
||
| 497 | ], |
||
| 498 | ]; |
||
| 499 | } |
||
| 500 | } |
||
| 501 |