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