Passed
Push — develop ( 0deb33...ed7ad9 )
by Andrew
03:41
created

Beacons::htmlBeaconScript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 nystudio107\webperf\services;
12
13
use nystudio107\webperf\models\DataSample;
14
use nystudio107\webperf\Webperf;
15
use nystudio107\webperf\helpers\PluginTemplate;
16
17
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...
18
19
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...
20
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...
21
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...
22
23
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
24
 * @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...
25
 * @package   Webperf
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
26
 * @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...
27
 */
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...
28
class Beacons extends Component
29
{
30
    // Constants
31
    // =========================================================================
32
33
    const AMP_IFRAME_SCRIPT_URL = "https://cdn.ampproject.org/v0/amp-iframe-0.1.js";
34
    const SEOMATIC_PLUGIN_HANDLE = 'seomatic';
35
36
    // Public Methods
37
    // =========================================================================
38
39
    /*
40
     * @return void
41
     */
42
    public function includeHtmlBeacon()
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
43
    {
44
        $view = Craft::$app->getView();
45
        $script = $this->htmlBeaconScript();
46
        // Register the JavaScript
47
        $view->registerJs(
48
            $script,
49
            $view::POS_HEAD
50
        );
51
    }
52
53
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
54
     * @param string|null $title
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
55
     *
56
     * @return string
57
     */
58
    public function htmlBeaconScript(string $title = null): string
59
    {
60
        $boomerangUrl = Craft::$app->assetManager->getPublishedUrl(
61
            '@nystudio107/webperf/assetbundles/boomerang/dist/js/boomerang-1.0.0.min.js',
62
            true
63
        );
64
        $boomerangTitle = $title ?? $this->getDocumentTitle();
65
        $script = PluginTemplate::renderPluginTemplate(
66
            '_frontend/scripts/load-boomerang-iframe.twig',
67
            [
68
                'boomerangScriptUrl' => $boomerangUrl,
69
                'boomerangTitle' => $boomerangTitle,
70
                'boomerangRequestId' => Webperf::$requestUuid,
71
            ],
72
            'jsMin'
73
        );
74
75
        return $script;
76
    }
77
78
    /*
79
     * @return void
80
     */
81
    public function includeAmpHtmlScript()
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
82
    {
83
        $view = Craft::$app->getView();
84
        $view->registerJsFile(
85
            self::AMP_IFRAME_SCRIPT_URL,
86
            [
87
                'position' => $view::POS_HEAD,
88
                'async' => 'async',
89
                'custom-element' => 'amp-iframe',
90
            ],
91
            'webperf-amp-iframe-script'
92
        );
93
    }
94
95
    /*
96
     * @return void
97
     */
98
    public function includeAmpHtmlBeacon()
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
99
    {
100
        $html = PluginTemplate::renderPluginTemplate(
101
            '_frontend/scripts/load-boomerang-amp-iframe.twig',
102
            [
103
                'boomerangIframeUrl' => UrlHelper::siteUrl('/webperf/render/amp-iframe'),
104
            ],
105
            'minify'
106
        );
107
        echo $html;
108
    }
109
110
    /*
111
     * @return void
112
     */
113
    public function ampHtmlIframe()
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
114
    {
115
        $boomerangUrl = Craft::$app->assetManager->getPublishedUrl(
116
            '@nystudio107/webperf/assetbundles/boomerang/dist/js/boomerang-1.0.0.min.js',
117
            true
118
        );
119
        $boomerangTitle = $this->getDocumentTitle();
120
        $html = PluginTemplate::renderPluginTemplate(
121
            '_frontend/scripts/boomerang-amp-iframe-html.twig',
122
            [
123
                'boomerangScriptUrl' => $boomerangUrl,
124
                'boomerangTitle' => $boomerangTitle,
125
                'boomerangRequestId' => Webperf::$requestUuid,
126
            ],
127
            'minify'
128
        );
129
130
        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...
131
    }
132
133
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
134
     * @return void
135
     */
136
    public function includeCraftBeacon()
137
    {
138
        $stats = Webperf::$plugin->profileTarget->stats;
139
        // Allocate a new DataSample, and fill it in
140
        $sample = new DataSample([
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...
141
            'requestId' => Webperf::$requestUuid,
142
            'url' => Webperf::$requestUrl ?? DataSample::PLACEHOLDER_URL,
143
            'craftTotalMs' => (int)($stats['database']['duration']
144
                + $stats['twig']['duration']
145
                + $stats['other']['duration']),
146
            'craftDbMs' => (int)$stats['database']['duration'],
147
            'craftDbCnt' => (int)$stats['database']['count'],
148
            'craftTwigMs' => (int)$stats['twig']['duration'],
149
            'craftTwigCnt' => (int)$stats['twig']['count'],
150
            'craftOtherMs' => (int)$stats['other']['duration'],
151
            'craftOtherCnt' => (int)$stats['other']['count'],
152
            'craftTotalMemory' => (int)($stats['database']['memory']
153
                + $stats['twig']['memory']
154
                + $stats['other']['memory']),
155
        ]);
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...
156
        // Save the data sample
157
        $sample->setScenario(DataSample::SCENARIO_CRAFT_BEACON);
158
        Craft::debug('Saving Craft DataSample: '.print_r($sample, true), __METHOD__);
159
        Webperf::$plugin->dataSamples->addDataSample($sample);
160
    }
161
162
    // Protected Methods
163
    // =========================================================================
164
165
    /**
166
     * Get the title of the currently rendering document
167
     *
168
     * @return string
169
     */
170
    protected function getDocumentTitle(): string
171
    {
172
        // Default to whatever the view title is
173
        $view = Craft::$app->getView();
174
        $docTitle = $view->title;
175
        if (empty($docTitle)) {
176
            $docTitle = '';
177
        }
178
        // If SEOmatic is installed, get the title from it
179
        $seomatic = Craft::$app->getPlugins()->getPlugin(self::SEOMATIC_PLUGIN_HANDLE);
180
        if ($seomatic && Seomatic::$settings->renderEnabled) {
181
            $titleTag = Seomatic::$plugin->title->get('title');
182
            if ($titleTag) {
183
                $titleArray = $titleTag->renderAttributes();
184
                if (!empty($titleArray['title'])) {
185
                    $docTitle = $titleArray['title'];
186
                }
187
            }
188
        }
189
190
        return $docTitle;
191
    }
192
}
193