Passed
Push — v1 ( 0f87a2...543469 )
by Andrew
08:17 queued 04:56
created

Beacons   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 21
eloc 112
dl 0
loc 225
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A ampHtmlIframe() 0 19 2
A getDocumentTitle() 0 21 6
A includeHtmlBeacon() 0 8 1
A includeAmpHtmlScript() 0 11 1
A includeAmpHtmlBeacon() 0 10 1
A htmlBeaconScript() 0 20 2
A includeCraftBeacon() 0 45 4
A includeCraftErrorsBeacon() 0 32 4
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) 2019 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 nystudio107\webperf\services;
12
13
use nystudio107\webperf\Webperf;
14
use nystudio107\webperf\base\CraftDataSample;
15
use nystudio107\webperf\helpers\MultiSite;
16
use nystudio107\webperf\helpers\PluginTemplate;
17
use nystudio107\webperf\models\CraftDbErrorSample;
18
use nystudio107\webperf\models\CraftDbDataSample;
19
20
use nystudio107\seomatic\Seomatic;
0 ignored issues
show
Bug introduced by
The type nystudio107\seomatic\Seomatic 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...
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
24
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...
25
use craft\base\Component;
0 ignored issues
show
Bug introduced by
The type craft\base\Component 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\helpers\UrlHelper;
0 ignored issues
show
Bug introduced by
The type craft\helpers\UrlHelper 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
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
30
 * @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...
31
 * @package   Webperf
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
32
 * @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...
33
 */
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...
34
class Beacons extends Component
35
{
36
    // Constants
37
    // =========================================================================
38
39
    const AMP_IFRAME_SCRIPT_URL = "https://cdn.ampproject.org/v0/amp-iframe-0.1.js";
40
    const SEOMATIC_PLUGIN_HANDLE = 'seomatic';
41
42
    // Public Methods
43
    // =========================================================================
44
45
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
46
     * @return void
47
     */
48
    public function includeHtmlBeacon()
49
    {
50
        $view = Craft::$app->getView();
51
        $script = $this->htmlBeaconScript(false);
52
        // Register the JavaScript
53
        $view->registerJs(
54
            $script,
55
            $view::POS_HEAD
56
        );
57
    }
58
59
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
60
     * @param bool        $headless
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
61
     * @param string|null $title
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
62
     *
63
     * @return string
64
     */
65
    public function htmlBeaconScript(bool $headless = false, string $title = null): string
66
    {
67
        $boomerangUrl = Craft::$app->assetManager->getPublishedUrl(
68
            '@nystudio107/webperf/assetbundles/boomerang/dist/js/boomerang-1.0.0.min.js',
69
            true
70
        );
71
        $boomerangTitle = $title ?? $this->getDocumentTitle();
72
        $boomerangRequestId = Webperf::$settings->staticCachedSite ? null : Webperf::$requestUuid;
73
        $script = PluginTemplate::renderPluginTemplate(
74
            '_frontend/scripts/load-boomerang-iframe.twig',
75
            [
76
                'headless' => $headless,
77
                'boomerangScriptUrl' => $boomerangUrl,
78
                'boomerangTitle' => $boomerangTitle,
79
                'boomerangRequestId' => $boomerangRequestId,
80
            ],
81
            'jsMin'
82
        );
83
84
        return $script;
85
    }
86
87
    /*
88
     * @return void
89
     */
90
    public function includeAmpHtmlScript()
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
91
    {
92
        $view = Craft::$app->getView();
93
        $view->registerJsFile(
94
            self::AMP_IFRAME_SCRIPT_URL,
95
            [
96
                'position' => $view::POS_HEAD,
97
                'async' => 'async',
98
                'custom-element' => 'amp-iframe',
99
            ],
100
            'webperf-amp-iframe-script'
101
        );
102
    }
103
104
    /*
105
     * @return void
106
     */
107
    public function includeAmpHtmlBeacon()
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
108
    {
109
        $html = PluginTemplate::renderPluginTemplate(
110
            '_frontend/scripts/load-boomerang-amp-iframe.twig',
111
            [
112
                'boomerangIframeUrl' => UrlHelper::siteUrl('/webperf/render/amp-iframe'),
113
            ],
114
            'minify'
115
        );
116
        echo $html;
117
    }
118
119
    /*
120
     * @return void
121
     */
122
    public function ampHtmlIframe()
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
123
    {
124
        $boomerangUrl = Craft::$app->assetManager->getPublishedUrl(
125
            '@nystudio107/webperf/assetbundles/boomerang/dist/js/boomerang-1.0.0.min.js',
126
            true
127
        );
128
        $boomerangTitle = $this->getDocumentTitle();
129
        $boomerangRequestId = Webperf::$settings->staticCachedSite ? null : Webperf::$requestUuid;
130
        $html = PluginTemplate::renderPluginTemplate(
131
            '_frontend/scripts/boomerang-amp-iframe-html.twig',
132
            [
133
                'boomerangScriptUrl' => $boomerangUrl,
134
                'boomerangTitle' => $boomerangTitle,
135
                'boomerangRequestId' => $boomerangRequestId,
136
            ],
137
            'minify'
138
        );
139
140
        return $html;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $html returns the type string which is incompatible with the documented return type void.
Loading history...
141
    }
142
143
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
144
     * @return void
145
     */
146
    public function includeCraftBeacon()
147
    {
148
        // Filter out bot/spam requests via UserAgent
149
        if (Webperf::$settings->filterBotUserAgents) {
150
            $crawlerDetect = new CrawlerDetect;
151
            // Check the user agent of the current 'visitor'
152
            if ($crawlerDetect->isCrawler()) {
153
                return;
154
            }
155
        }
156
        $url = Webperf::$requestUrl ?? CraftDataSample::PLACEHOLDER_URL;
157
        // Get the site id
158
        try {
159
            $site = MultiSite::getSiteFromUrl($url);
160
            $siteId = $site->id;
161
        } catch (SiteNotFoundException $e) {
162
            $siteId = null;
163
        }
164
        $stats = Webperf::$plugin->profileTarget->stats;
165
        $request = Craft::$app->getRequest();
166
        $pageLoad = (int)($stats['database']['duration']
167
            + $stats['twig']['duration']
168
            + $stats['other']['duration']);
169
        // Allocate a new DataSample, and fill it in
170
        $sample = new CraftDbDataSample([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
171
            'requestId' => Webperf::$requestUuid,
172
            'siteId' => $siteId,
173
            'url' => $url,
174
            'title' => $this->getDocumentTitle(),
175
            'queryString' => $request->getQueryString(),
176
            'pageLoad' => $pageLoad,
177
            'craftTotalMs' => $pageLoad,
178
            'craftDbMs' => (int)$stats['database']['duration'],
179
            'craftDbCnt' => (int)$stats['database']['count'],
180
            'craftTwigMs' => (int)$stats['twig']['duration'],
181
            'craftTwigCnt' => (int)$stats['twig']['count'],
182
            'craftOtherMs' => (int)$stats['other']['duration'],
183
            'craftOtherCnt' => (int)$stats['other']['count'],
184
            'craftTotalMemory' => (int)($stats['database']['memory']
185
                + $stats['twig']['memory']
186
                + $stats['other']['memory']),
187
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
188
        // Save the data sample
189
        Craft::debug('Saving Craft DataSample: '.print_r($sample->getAttributes(), true), __METHOD__);
190
        Webperf::$plugin->dataSamples->addDataSample($sample);
191
    }
192
193
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
194
     * @return void
195
     */
196
    public function includeCraftErrorsBeacon()
197
    {
198
        // Filter out bot/spam requests via UserAgent
199
        if (Webperf::$settings->filterBotUserAgents) {
200
            $crawlerDetect = new CrawlerDetect;
201
            // Check the user agent of the current 'visitor'
202
            if ($crawlerDetect->isCrawler()) {
203
                return;
204
            }
205
        }
206
        $url = Webperf::$requestUrl ?? CraftDataSample::PLACEHOLDER_URL;
207
        // Get the site id
208
        try {
209
            $site = MultiSite::getSiteFromUrl($url);
210
            $siteId = $site->id;
211
        } catch (SiteNotFoundException $e) {
212
            $siteId = null;
213
        }
214
        $messages = Webperf::$plugin->errorsTarget->pageErrors;
215
        $request = Craft::$app->getRequest();
216
        // Allocate a new ErrorSample, and fill it in
217
        $sample = new CraftDbErrorSample([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
218
            'requestId' => Webperf::$requestUuid,
219
            'siteId' => $siteId,
220
            'url' => $url,
221
            'queryString' => $request->getQueryString(),
222
            'title' => $this->getDocumentTitle(),
223
            'pageErrors' => $messages,
224
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
225
        // Save the error sample
226
        Craft::debug('Saving Craft ErrorSample: '.print_r($sample->getAttributes(), true), __METHOD__);
227
        Webperf::$plugin->errorSamples->addErrorSample($sample);
228
    }
229
230
    // Protected Methods
231
    // =========================================================================
232
233
    /**
234
     * Get the title of the currently rendering document
235
     *
236
     * @return string
237
     */
238
    protected function getDocumentTitle(): string
239
    {
240
        // Default to whatever the view title is
241
        $view = Craft::$app->getView();
242
        $docTitle = $view->title;
243
        if (empty($docTitle)) {
244
            $docTitle = '';
245
        }
246
        // If SEOmatic is installed, get the title from it
247
        $seomatic = Craft::$app->getPlugins()->getPlugin(self::SEOMATIC_PLUGIN_HANDLE);
248
        if ($seomatic && Seomatic::$settings->renderEnabled) {
249
            $titleTag = Seomatic::$plugin->title->get('title');
250
            if ($titleTag) {
251
                $titleArray = $titleTag->renderAttributes();
252
                if (!empty($titleArray['title'])) {
253
                    $docTitle = $titleArray['title'];
254
                }
255
            }
256
        }
257
258
        return $docTitle;
259
    }
260
}
261