Total Complexity | 75 |
Total Lines | 749 |
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 |
||
60 | class Webperf extends Plugin |
||
61 | { |
||
62 | // Constants |
||
63 | // ========================================================================= |
||
64 | |||
65 | const RECOMMENDATIONS_CACHE_KEY = 'webperf-recommendations'; |
||
66 | const RECOMMENDATIONS_CACHE_DURATION = 60; |
||
67 | |||
68 | const ERRORS_CACHE_KEY = 'webperf-errors'; |
||
69 | const ERRORS_CACHE_DURATION = 60; |
||
70 | |||
71 | // Static Properties |
||
72 | // ========================================================================= |
||
73 | |||
74 | /** |
||
75 | * @var Webperf |
||
76 | */ |
||
77 | public static $plugin; |
||
78 | |||
79 | /** |
||
80 | * @var Settings |
||
81 | */ |
||
82 | public static $settings; |
||
83 | |||
84 | /** |
||
85 | * @var int|null |
||
86 | */ |
||
87 | public static $requestUuid; |
||
88 | |||
89 | /** |
||
90 | * @var int|null |
||
91 | */ |
||
92 | public static $requestUrl; |
||
93 | |||
94 | /** |
||
95 | * @var bool |
||
96 | */ |
||
97 | public static $beaconIncluded = false; |
||
98 | |||
99 | /** |
||
100 | * @var string |
||
101 | */ |
||
102 | public static $renderType = 'html'; |
||
103 | |||
104 | /** |
||
105 | * @var bool |
||
106 | */ |
||
107 | public static $craft31 = false; |
||
108 | |||
109 | // Public Properties |
||
110 | // ========================================================================= |
||
111 | |||
112 | /** |
||
113 | * @var string |
||
114 | */ |
||
115 | public $schemaVersion = '1.0.0'; |
||
116 | |||
117 | // Public Methods |
||
118 | // ========================================================================= |
||
119 | |||
120 | /** |
||
121 | * @inheritdoc |
||
122 | */ |
||
123 | public function init() |
||
124 | { |
||
125 | parent::init(); |
||
126 | // Initialize properties |
||
127 | self::$plugin = $this; |
||
128 | self::$settings = $this->getSettings(); |
||
129 | try { |
||
130 | self::$requestUuid = random_int(0, PHP_INT_MAX); |
||
131 | } catch (\Exception $e) { |
||
132 | self::$requestUuid = null; |
||
133 | } |
||
134 | self::$craft31 = version_compare(Craft::$app->getVersion(), '3.1', '>='); |
||
135 | $this->name = self::$settings->pluginName; |
||
136 | // Handle any console commands |
||
137 | $request = Craft::$app->getRequest(); |
||
138 | if ($request->getIsConsoleRequest()) { |
||
139 | $this->controllerNamespace = 'nystudio107\webperf\console\controllers'; |
||
140 | } |
||
141 | // Add in our components |
||
142 | $this->addComponents(); |
||
143 | // Install event listeners |
||
144 | $this->installEventListeners(); |
||
145 | // Load that we've loaded |
||
146 | Craft::info( |
||
147 | Craft::t( |
||
148 | 'webperf', |
||
149 | '{name} plugin loaded', |
||
150 | ['name' => $this->name] |
||
151 | ), |
||
152 | __METHOD__ |
||
153 | ); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @inheritdoc |
||
158 | */ |
||
159 | public function getSettingsResponse() |
||
160 | { |
||
161 | // Just redirect to the plugin settings page |
||
162 | Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('webperf/settings')); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @inheritdoc |
||
167 | */ |
||
168 | public function getCpNavItem() |
||
169 | { |
||
170 | $subNavs = []; |
||
171 | $navItem = parent::getCpNavItem(); |
||
172 | $recommendations = $this->getRecommendationsCount(); |
||
173 | $errors = $this->getErrorsCount(); |
||
174 | $navItem['badgeCount'] = $errors.$recommendations; |
||
175 | $currentUser = Craft::$app->getUser()->getIdentity(); |
||
176 | if ($currentUser) { |
||
177 | // Only show sub-navs the user has permission to view |
||
178 | if ($currentUser->can('webperf:dashboard')) { |
||
179 | $subNavs['dashboard'] = [ |
||
180 | 'label' => Craft::t('webperf', 'Dashboard'), |
||
181 | 'url' => 'webperf/dashboard', |
||
182 | ]; |
||
183 | } |
||
184 | if ($currentUser->can('webperf:performance')) { |
||
185 | $subNavs['performance'] = [ |
||
186 | 'label' => Craft::t('webperf', 'Performance'), |
||
187 | 'url' => 'webperf/performance', |
||
188 | ]; |
||
189 | } |
||
190 | if ($currentUser->can('webperf:errors')) { |
||
191 | $subNavs['errors'] = [ |
||
192 | 'label' => Craft::t('webperf', 'Errors').' '.$errors, |
||
193 | 'url' => 'webperf/errors', |
||
194 | 'badge' => $errors, |
||
195 | ]; |
||
196 | } |
||
197 | if ($currentUser->can('webperf:alerts')) { |
||
198 | $subNavs['alerts'] = [ |
||
199 | 'label' => 'Alerts', |
||
200 | 'url' => 'webperf/alerts', |
||
201 | ]; |
||
202 | } |
||
203 | if ($currentUser->can('webperf:settings')) { |
||
204 | $subNavs['settings'] = [ |
||
205 | 'label' => Craft::t('webperf', 'Settings'), |
||
206 | 'url' => 'webperf/settings', |
||
207 | ]; |
||
208 | } |
||
209 | } |
||
210 | $navItem = array_merge($navItem, [ |
||
211 | 'subnav' => $subNavs, |
||
212 | ]); |
||
213 | |||
214 | return $navItem; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Clear all the caches! |
||
219 | */ |
||
220 | public function clearAllCaches() |
||
221 | { |
||
222 | } |
||
223 | |||
224 | // Protected Methods |
||
225 | // ========================================================================= |
||
226 | |||
227 | /** |
||
228 | * Add in our components |
||
229 | */ |
||
230 | protected function addComponents() |
||
231 | { |
||
232 | $request = Craft::$app->getRequest(); |
||
233 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
||
234 | $this->setRequestUrl(); |
||
235 | try { |
||
236 | $uri = $request->getPathInfo(); |
||
237 | } catch (InvalidConfigException $e) { |
||
238 | $uri = ''; |
||
239 | } |
||
240 | // Ignore our own controllers |
||
241 | if (self::$settings->includeCraftProfiling && !$this->excludeUri($uri)) { |
||
242 | // Add in the ProfileTarget component |
||
243 | try { |
||
244 | $this->set('profileTarget', [ |
||
245 | 'class' => ProfileTarget::class, |
||
246 | 'levels' => ['profile'], |
||
247 | 'categories' => [], |
||
248 | 'logVars' => [], |
||
249 | 'except' => [], |
||
250 | ]); |
||
251 | } catch (InvalidConfigException $e) { |
||
252 | Craft::error($e->getMessage(), __METHOD__); |
||
253 | } |
||
254 | // Attach our log target |
||
255 | Craft::$app->getLog()->targets['webperf-profile'] = $this->profileTarget; |
||
256 | // Add in the ErrorsTarget component |
||
257 | $except = []; |
||
258 | // If devMode is on, exclude errors/warnings from `seomatic` |
||
259 | if (Craft::$app->getConfig()->getGeneral()->devMode) { |
||
260 | $except = ['nystudio107\seomatic\*']; |
||
261 | } |
||
262 | $levels = ['error']; |
||
263 | if (self::$settings->includeCraftWarnings) { |
||
264 | $levels[] = 'warning'; |
||
265 | } |
||
266 | try { |
||
267 | $this->set('errorsTarget', [ |
||
268 | 'class' => ErrorsTarget::class, |
||
269 | 'levels' => $levels, |
||
270 | 'categories' => [], |
||
271 | 'logVars' => [], |
||
272 | 'except' => $except, |
||
273 | ]); |
||
274 | } catch (InvalidConfigException $e) { |
||
275 | Craft::error($e->getMessage(), __METHOD__); |
||
276 | } |
||
277 | // Attach our log target |
||
278 | Craft::$app->getLog()->targets['webperf-errors'] = $this->errorsTarget; |
||
279 | } |
||
280 | } |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Set the request URL |
||
285 | * |
||
286 | * @param bool $force |
||
287 | */ |
||
288 | protected function setRequestUrl(bool $force = false) |
||
289 | { |
||
290 | self::$requestUrl = CraftDataSample::PLACEHOLDER_URL; |
||
291 | if (!self::$settings->includeBeacon || $force || self::$settings->staticCachedSite) { |
||
292 | $request = Craft::$app->getRequest(); |
||
293 | self::$requestUrl = UrlHelper::stripQueryString( |
||
294 | urldecode($request->getAbsoluteUrl()) |
||
295 | ); |
||
296 | } |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Install our event listeners. |
||
301 | */ |
||
302 | protected function installEventListeners() |
||
303 | { |
||
304 | $request = Craft::$app->getRequest(); |
||
305 | // Add in our event listeners that are needed for every request |
||
306 | $this->installGlobalEventListeners(); |
||
307 | // Install only for non-console site requests |
||
308 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
||
309 | $this->installSiteEventListeners(); |
||
310 | } |
||
311 | // Install only for non-console Control Panel requests |
||
312 | if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) { |
||
313 | $this->installCpEventListeners(); |
||
314 | } |
||
315 | // Handler: EVENT_AFTER_INSTALL_PLUGIN |
||
316 | Event::on( |
||
317 | Plugins::class, |
||
318 | Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
||
319 | function (PluginEvent $event) { |
||
320 | if ($event->plugin === $this) { |
||
321 | // Invalidate our caches after we've been installed |
||
322 | $this->clearAllCaches(); |
||
323 | // Send them to our welcome screen |
||
324 | $request = Craft::$app->getRequest(); |
||
325 | if ($request->isCpRequest) { |
||
326 | Craft::$app->getResponse()->redirect(UrlHelper::cpUrl( |
||
327 | 'webperf/dashboard', |
||
328 | [ |
||
329 | 'showWelcome' => true, |
||
330 | ] |
||
331 | ))->send(); |
||
332 | } |
||
333 | } |
||
334 | } |
||
335 | ); |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Install global event listeners for all request types |
||
340 | */ |
||
341 | protected function installGlobalEventListeners() |
||
342 | { |
||
343 | // Handler: CraftVariable::EVENT_INIT |
||
344 | Event::on( |
||
345 | CraftVariable::class, |
||
346 | CraftVariable::EVENT_INIT, |
||
347 | function (Event $event) { |
||
348 | /** @var CraftVariable $variable */ |
||
349 | $variable = $event->sender; |
||
350 | $variable->set('webperf', WebperfVariable::class); |
||
351 | } |
||
352 | ); |
||
353 | // Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS |
||
354 | Event::on( |
||
355 | Plugins::class, |
||
356 | Plugins::EVENT_AFTER_LOAD_PLUGINS, |
||
357 | function () { |
||
358 | // Install these only after all other plugins have loaded |
||
359 | $request = Craft::$app->getRequest(); |
||
360 | // Only respond to non-console site requests |
||
361 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
||
362 | $this->handleSiteRequest(); |
||
363 | } |
||
364 | // Respond to Control Panel requests |
||
365 | if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) { |
||
366 | $this->handleAdminCpRequest(); |
||
367 | } |
||
368 | } |
||
369 | ); |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * Install site event listeners for site requests only |
||
374 | */ |
||
375 | protected function installSiteEventListeners() |
||
376 | { |
||
377 | // Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES |
||
378 | Event::on( |
||
379 | UrlManager::class, |
||
380 | UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
||
381 | function (RegisterUrlRulesEvent $event) { |
||
382 | Craft::debug( |
||
383 | 'UrlManager::EVENT_REGISTER_SITE_URL_RULES', |
||
384 | __METHOD__ |
||
385 | ); |
||
386 | // Register our Control Panel routes |
||
387 | $event->rules = array_merge( |
||
388 | $event->rules, |
||
389 | $this->customFrontendRoutes() |
||
390 | ); |
||
391 | } |
||
392 | ); |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Install site event listeners for Control Panel requests only |
||
397 | */ |
||
398 | protected function installCpEventListeners() |
||
435 | } |
||
436 | ); |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * Handle site requests. We do it only after we receive the event |
||
441 | * EVENT_AFTER_LOAD_PLUGINS so that any pending db migrations can be run |
||
442 | * before our event listeners kick in |
||
443 | */ |
||
444 | protected function handleSiteRequest() |
||
445 | { |
||
446 | $request = Craft::$app->getRequest(); |
||
447 | try { |
||
448 | $uri = $request->getPathInfo(); |
||
449 | } catch (InvalidConfigException $e) { |
||
450 | $uri = ''; |
||
451 | } |
||
452 | // Don't include the beacon for response codes >= 400 |
||
453 | $response = Craft::$app->getResponse(); |
||
454 | if ($response->statusCode < 400 && !$this->excludeUri($uri)) { |
||
455 | // Handler: View::EVENT_END_PAGE |
||
456 | Event::on( |
||
457 | View::class, |
||
458 | View::EVENT_END_PAGE, |
||
459 | function () { |
||
460 | Craft::debug( |
||
461 | 'View::EVENT_END_PAGE', |
||
462 | __METHOD__ |
||
463 | ); |
||
464 | $view = Craft::$app->getView(); |
||
465 | // The page is done rendering, include our beacon |
||
466 | if (Webperf::$settings->includeBeacon && $view->getIsRenderingPageTemplate()) { |
||
467 | switch (self::$renderType) { |
||
468 | case 'html': |
||
469 | Webperf::$plugin->beacons->includeHtmlBeacon(); |
||
470 | self::$beaconIncluded = true; |
||
471 | break; |
||
472 | case 'amp-html': |
||
473 | Webperf::$plugin->beacons->includeAmpHtmlScript(); |
||
474 | break; |
||
475 | } |
||
476 | } |
||
477 | } |
||
478 | ); |
||
479 | // Handler: View::EVENT_END_BODY |
||
480 | Event::on( |
||
481 | View::class, |
||
482 | View::EVENT_END_BODY, |
||
483 | function () { |
||
484 | Craft::debug( |
||
485 | 'View::EVENT_END_BODY', |
||
486 | __METHOD__ |
||
487 | ); |
||
488 | $view = Craft::$app->getView(); |
||
489 | // The page is done rendering, include our beacon |
||
490 | if (Webperf::$settings->includeBeacon && $view->getIsRenderingPageTemplate()) { |
||
491 | switch (self::$renderType) { |
||
492 | case 'html': |
||
493 | break; |
||
494 | case 'amp-html': |
||
495 | Webperf::$plugin->beacons->includeAmpHtmlBeacon(); |
||
496 | self::$beaconIncluded = true; |
||
497 | break; |
||
498 | } |
||
499 | } |
||
500 | } |
||
501 | ); |
||
502 | // Handler: Application::EVENT_AFTER_REQUEST |
||
503 | Event::on( |
||
504 | Application::class, |
||
505 | Application::EVENT_AFTER_REQUEST, |
||
506 | function () { |
||
507 | Craft::debug( |
||
508 | 'Application::EVENT_AFTER_REQUEST', |
||
509 | __METHOD__ |
||
510 | ); |
||
511 | // If the beacon wasn't included, allow for the Craft timings |
||
512 | if (!self::$beaconIncluded) { |
||
513 | $this->setRequestUrl(true); |
||
514 | } |
||
515 | } |
||
516 | ); |
||
517 | } |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * Handle Control Panel requests. We do it only after we receive the event |
||
522 | * EVENT_AFTER_LOAD_PLUGINS so that any pending db migrations can be run |
||
523 | * before our event listeners kick in |
||
524 | */ |
||
525 | protected function handleAdminCpRequest() |
||
551 | }); |
||
552 | } |
||
553 | } |
||
554 | |||
555 | /** |
||
556 | * @param Element $element |
||
557 | * |
||
558 | * @return string |
||
559 | */ |
||
560 | protected function renderSidebar(Element $element): string |
||
589 | } |
||
590 | |||
591 | /** |
||
592 | * @param $uri |
||
593 | * |
||
594 | * @return bool |
||
595 | */ |
||
596 | protected function excludeUri($uri): bool |
||
597 | { |
||
598 | $uri = '/'.ltrim($uri, '/'); |
||
599 | foreach (self::$settings->excludePatterns as $excludePattern) { |
||
600 | $pattern = '`'.$excludePattern['pattern'].'`i'; |
||
601 | if (preg_match($pattern, $uri) === 1) { |
||
602 | return true; |
||
603 | } |
||
604 | } |
||
605 | |||
606 | return false; |
||
607 | } |
||
608 | |||
609 | /** |
||
610 | * @inheritdoc |
||
611 | */ |
||
612 | protected function createSettingsModel() |
||
613 | { |
||
614 | return new Settings(); |
||
615 | } |
||
616 | |||
617 | /** |
||
618 | * @inheritdoc |
||
619 | */ |
||
620 | protected function settingsHtml(): string |
||
621 | { |
||
622 | return Craft::$app->view->renderTemplate( |
||
623 | 'webperf/settings', |
||
624 | [ |
||
625 | 'settings' => $this->getSettings() |
||
626 | ] |
||
627 | ); |
||
628 | } |
||
629 | |||
630 | /** |
||
631 | * Return the custom frontend routes |
||
632 | * |
||
633 | * @return array |
||
634 | */ |
||
635 | protected function customFrontendRoutes(): array |
||
636 | { |
||
637 | return [ |
||
638 | // Beacon |
||
639 | '/webperf/metrics/beacon' => 'webperf/metrics/beacon', |
||
640 | // Render |
||
641 | '/webperf/render/amp-iframe' => 'webperf/render/amp-iframe', |
||
642 | // Tables |
||
643 | '/webperf/tables/pages-index' => 'webperf/tables/pages-index', |
||
644 | '/webperf/tables/page-detail' => 'webperf/tables/page-detail', |
||
645 | '/webperf/tables/errors-index' => 'webperf/tables/errors-index', |
||
646 | '/webperf/tables/errors-detail' => 'webperf/tables/errors-detail', |
||
647 | // Charts |
||
648 | '/webperf/charts/dashboard-stats-average/<column:{handle}>' |
||
649 | => 'webperf/charts/dashboard-stats-average', |
||
650 | '/webperf/charts/dashboard-stats-average/<column:{handle}>/<siteId:\d+>' |
||
651 | => 'webperf/charts/dashboard-stats-average', |
||
652 | |||
653 | '/webperf/charts/dashboard-slowest-pages/<column:{handle}>/<limit:\d+>' |
||
654 | => 'webperf/charts/dashboard-slowest-pages', |
||
655 | '/webperf/charts/dashboard-slowest-pages/<column:{handle}>/<limit:\d+>/<siteId:\d+>' |
||
656 | => 'webperf/charts/dashboard-slowest-pages', |
||
657 | |||
658 | '/webperf/charts/pages-area-chart' |
||
659 | => 'webperf/charts/pages-area-chart', |
||
660 | '/webperf/charts/pages-area-chart/<siteId:\d+>' |
||
661 | => 'webperf/charts/pages-area-chart', |
||
662 | |||
663 | '/webperf/charts/errors-area-chart' |
||
664 | => 'webperf/charts/errors-area-chart', |
||
665 | '/webperf/charts/errors-area-chart/<siteId:\d+>' |
||
666 | => 'webperf/charts/errors-area-chart', |
||
667 | |||
668 | '/webperf/recommendations/list' |
||
669 | => 'webperf/recommendations/list', |
||
670 | '/webperf/recommendations/list/<siteId:\d+>' |
||
671 | => 'webperf/recommendations/list', |
||
672 | |||
673 | '/webperf/charts/widget/<days>' => 'webperf/charts/widget', |
||
674 | ]; |
||
675 | } |
||
676 | /** |
||
677 | * Return the custom Control Panel routes |
||
678 | * |
||
679 | * @return array |
||
680 | */ |
||
681 | protected function customAdminCpRoutes(): array |
||
682 | { |
||
683 | return [ |
||
684 | 'webperf' => 'webperf/sections/dashboard', |
||
685 | 'webperf/dashboard' => 'webperf/sections/dashboard', |
||
686 | 'webperf/dashboard/<siteHandle:{handle}>' => 'webperf/sections/dashboard', |
||
687 | |||
688 | 'webperf/performance' => 'webperf/sections/pages-index', |
||
689 | 'webperf/performance/<siteHandle:{handle}>' => 'webperf/sections/pages-index', |
||
690 | |||
691 | 'webperf/performance/page-detail' => 'webperf/sections/page-detail', |
||
692 | 'webperf/performance/page-detail/<siteHandle:{handle}>' => 'webperf/sections/page-detail', |
||
693 | |||
694 | 'webperf/errors' => 'webperf/sections/errors-index', |
||
695 | 'webperf/errors/<siteHandle:{handle}>' => 'webperf/sections/errors-index', |
||
696 | |||
697 | 'webperf/errors/page-detail' => 'webperf/sections/errors-detail', |
||
698 | 'webperf/errors/page-detail/<siteHandle:{handle}>' => 'webperf/errors/page-detail', |
||
699 | |||
700 | 'webperf/alerts' => 'webperf/sections/alerts', |
||
701 | 'webperf/alerts/<siteHandle:{handle}>' => 'webperf/sections/alerts', |
||
702 | |||
703 | 'webperf/settings' => 'webperf/settings/plugin-settings', |
||
704 | ]; |
||
705 | } |
||
706 | |||
707 | /** |
||
708 | * Returns the custom Control Panel user permissions. |
||
709 | * |
||
710 | * @return array |
||
711 | */ |
||
712 | protected function customAdminCpPermissions(): array |
||
713 | { |
||
714 | return [ |
||
715 | 'webperf:dashboard' => [ |
||
716 | 'label' => Craft::t('webperf', 'Dashboard'), |
||
717 | ], |
||
718 | 'webperf:performance' => [ |
||
719 | 'label' => Craft::t('webperf', 'Performance'), |
||
720 | 'nested' => [ |
||
721 | 'webperf:performance-detail' => [ |
||
722 | 'label' => Craft::t('webperf', 'Performance Detail'), |
||
723 | ], |
||
724 | 'webperf:delete-data-samples' => [ |
||
725 | 'label' => Craft::t('webperf', 'Delete Data Samples'), |
||
726 | ], |
||
727 | ], |
||
728 | ], |
||
729 | 'webperf:errors' => [ |
||
730 | 'label' => Craft::t('webperf', 'Errors'), |
||
731 | 'nested' => [ |
||
732 | 'webperf:errors-detail' => [ |
||
733 | 'label' => Craft::t('webperf', 'Errors Detail'), |
||
734 | ], |
||
735 | 'webperf:delete-error-samples' => [ |
||
736 | 'label' => Craft::t('webperf', 'Delete Error Samples'), |
||
737 | ], |
||
738 | ], |
||
739 | ], |
||
740 | 'webperf:alerts' => [ |
||
741 | 'label' => Craft::t('webperf', 'Alerts'), |
||
742 | ], |
||
743 | 'webperf:recommendations' => [ |
||
744 | 'label' => Craft::t('webperf', 'Recommendations'), |
||
745 | ], |
||
746 | 'webperf:sidebar' => [ |
||
747 | 'label' => Craft::t('webperf', 'Performance Sidebar'), |
||
748 | ], |
||
749 | 'webperf:settings' => [ |
||
750 | 'label' => Craft::t('webperf', 'Settings'), |
||
751 | ], |
||
752 | ]; |
||
753 | } |
||
754 | |||
755 | /** |
||
756 | * Get a string value with the number of recommendations |
||
757 | * |
||
758 | * @return string |
||
759 | */ |
||
760 | protected function getRecommendationsCount(): string |
||
761 | { |
||
762 | $cache = Craft::$app->getCache(); |
||
763 | // See if there are any recommendations to add as a badge |
||
764 | $recommendations = $cache->getOrSet(self::RECOMMENDATIONS_CACHE_KEY, function () { |
||
765 | $data = []; |
||
766 | $now = new \DateTime(); |
||
767 | $end = $now->format('Y-m-d'); |
||
768 | $start = $now->modify('-30 days')->format('Y-m-d'); |
||
769 | $stats = Webperf::$plugin->recommendations->data('', $start, $end); |
||
770 | if (!empty($stats)) { |
||
771 | $recSample = new RecommendationDataSample($stats); |
||
772 | $data = Webperf::$plugin->recommendations->list($recSample); |
||
773 | } |
||
774 | |||
775 | return count($data); |
||
776 | }, self::RECOMMENDATIONS_CACHE_DURATION); |
||
777 | |||
778 | if (!$recommendations) { |
||
779 | $recommendations = ''; |
||
780 | } |
||
781 | |||
782 | return (string)$recommendations; |
||
783 | } |
||
784 | |||
785 | /** |
||
786 | * Get a string value with the number of errors |
||
787 | * |
||
788 | * @return string |
||
789 | */ |
||
790 | protected function getErrorsCount(): string |
||
809 | } |
||
810 | } |
||
811 |