Passed
Push — v1 ( 65d641...7780c4 )
by Andrew
02:59
created

Beacons   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 67
dl 0
loc 146
rs 10
c 0
b 0
f 0

6 Methods

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