Passed
Push — v1 ( 887199...d7f64b )
by Andrew
17:42 queued 08:53
created

MetricsController::actionBeacon()   F

Complexity

Conditions 21
Paths 6148

Size

Total Lines 73
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 73
rs 0
c 0
b 0
f 0
cc 21
nc 6148
nop 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * Webperf plugin for Craft CMS 3.x
4
 *
5
 * Monitor the performance of your webpages through real-world user timing data
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2018 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace {
12
13
    require_once __DIR__ . '/../lib/geoiploc.php';
0 ignored issues
show
Coding Style introduced by
File is being conditionally included; use "include_once" instead
Loading history...
14
}
15
16
namespace nystudio107\webperf\controllers {
17
18
    use nystudio107\webperf\helpers\MultiSite;
19
    use nystudio107\webperf\Webperf;
20
    use nystudio107\webperf\models\DataSample;
21
22
    use Jaybizzle\CrawlerDetect\CrawlerDetect;
0 ignored issues
show
Bug introduced by
The type Jaybizzle\CrawlerDetect\CrawlerDetect was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
    use WhichBrowser\Parser;
0 ignored issues
show
Bug introduced by
The type WhichBrowser\Parser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
25
    use Craft;
0 ignored issues
show
Bug introduced by
The type Craft was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
    use craft\errors\SiteNotFoundException;
0 ignored issues
show
Bug introduced by
The type craft\errors\SiteNotFoundException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
    use craft\web\Controller;
0 ignored issues
show
Bug introduced by
The type craft\web\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
29
    use yii\base\InvalidConfigException;
0 ignored issues
show
Bug introduced by
The type yii\base\InvalidConfigException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
31
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
32
     * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 4
Loading history...
33
     * @package   Webperf
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
34
     * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 3 spaces but found 5
Loading history...
35
     */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
36
    class MetricsController extends Controller
37
    {
38
        // Public Properties
39
        // =========================================================================
40
41
        public $enableCsrfValidation = false;
42
43
        // Protected Properties
44
        // =========================================================================
45
46
        /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
47
         * @var    bool|array Allows anonymous access to this controller's actions.
48
         *         The actions must be in 'kebab-case'
49
         * @access protected
50
         */
51
        protected $allowAnonymous = ['beacon'];
52
53
54
        // Public Methods
55
        // =========================================================================
56
57
        /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
58
         * @return mixed
59
         */
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);
0 ignored issues
show
Bug introduced by
The function getCountryFromIP was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
                $sample->countryCode = /** @scrutinizer ignore-call */ getCountryFromIP($ip);
Loading history...
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
        }
134
    }
135
}
136