Passed
Push — v1 ( 8c4450...e13d73 )
by Andrew
14:50 queued 11:24
created

Beacons::includeAmpHtmlBeacon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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