| Conditions | 21 |
| Paths | 6148 |
| Total Lines | 73 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 60 | public function actionBeacon() |
||
| 61 | { |
||
| 62 | // Get the incoming params from the beacon |
||
| 63 | try { |
||
| 64 | $params = Craft::$app->getRequest()->getBodyParams(); |
||
| 65 | } catch (InvalidConfigException $e) { |
||
| 66 | $params = []; |
||
| 67 | } |
||
| 68 | // Ensure the beacon has at least the URL parameter |
||
| 69 | if (empty($params) || empty($params['u'])) { |
||
| 70 | return; |
||
| 71 | } |
||
| 72 | // Filter out bot/spam requests via UserAgent |
||
| 73 | if (Webperf::$settings->filterBotUserAgents) { |
||
| 74 | $crawlerDetect = new CrawlerDetect; |
||
| 75 | // Check the user agent of the current 'visitor' |
||
| 76 | if ($crawlerDetect->isCrawler()) { |
||
| 77 | return; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | // Allocate a new DataSample, and fill it in |
||
| 81 | $sample = new DataSample(); |
||
| 82 | $sample->url = $params['u']; |
||
| 83 | // Get the site id |
||
| 84 | try { |
||
| 85 | $site = MultiSite::getSiteFromUrl($sample->url); |
||
| 86 | $sample->siteId = $site->id; |
||
| 87 | } catch (SiteNotFoundException $e) { |
||
| 88 | $sample->siteId = null; |
||
| 89 | } |
||
| 90 | // Fill in all of the timing information that's available |
||
| 91 | $sample->pageLoad = $params['t_done'] ?? null; |
||
| 92 | if (!empty($params['nt_dns_end']) && !empty($params['nt_dns_st'])) { |
||
| 93 | $sample->dns = $params['nt_dns_end'] - $params['nt_dns_st']; |
||
| 94 | } |
||
| 95 | if (!empty($params['nt_con_end']) && !empty($params['nt_con_st'])) { |
||
| 96 | $sample->connect = $params['nt_con_end'] - $params['nt_con_st']; |
||
| 97 | } |
||
| 98 | if (!empty($params['t_resp'])) { |
||
| 99 | $sample->firstByte = $params['t_resp']; |
||
| 100 | } |
||
| 101 | if (!empty($params['pt_fp'])) { |
||
| 102 | $sample->firstPaint = $params['pt_fp']; |
||
| 103 | } |
||
| 104 | if (!empty($params['pt_fcp'])) { |
||
| 105 | $sample->firstContentfulPaint = $params['pt_fcp']; |
||
| 106 | } |
||
| 107 | if (!empty($params['nt_domint']) && !empty($params['nt_nav_st'])) { |
||
| 108 | $sample->domInteractive = $params['nt_domint'] - $params['nt_nav_st']; |
||
| 109 | } |
||
| 110 | if (!empty($params['nt_domcomp']) && !empty($params['nt_nav_st'])) { |
||
| 111 | $sample->pageLoad = $params['nt_domcomp'] - $params['nt_nav_st']; |
||
| 112 | } |
||
| 113 | // Fill in information from the current request |
||
| 114 | $request = Craft::$app->getRequest(); |
||
| 115 | $ip = $request->userIP; |
||
| 116 | if ($ip) { |
||
| 117 | $sample->countryCode = getCountryFromIP($ip); |
||
| 118 | // getCountryFromIP returns 'ZZ' for unknown countries, map to '??' |
||
| 119 | if ($sample->countryCode === 'ZZ') { |
||
| 120 | $sample->countryCode = '??'; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | $userAgent = $request->userAgent; |
||
| 124 | if ($userAgent) { |
||
| 125 | $parser = new Parser($userAgent); |
||
| 126 | $sample->browser = $parser->browser->name; |
||
| 127 | $sample->os = $parser->os->name; |
||
| 128 | $sample->mobile = $parser->isMobile(); |
||
| 129 | } |
||
| 130 | // Save the data sample |
||
| 131 | Craft::debug('Saving DataSample: '.print_r($sample, true), __METHOD__); |
||
| 132 | Webperf::$plugin->dataSamples->addDataSample($sample); |
||
| 133 | } |
||
| 136 |