Passed
Push — v1 ( 01300c...f6b482 )
by Andrew
07:56 queued 04:40
created

Beacons::ampHtmlIframe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 14
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\helpers\PluginTemplate;
14
15
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...
16
17
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...
18
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...
19
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...
20
21
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
22
 * @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...
23
 * @package   Webperf
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
24
 * @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...
25
 */
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...
26
class Beacons extends Component
27
{
28
    // Constants
29
    // =========================================================================
30
31
    const AMP_IFRAME_SCRIPT_URL = "https://cdn.ampproject.org/v0/amp-iframe-0.1.js";
32
    const SEOMATIC_PLUGIN_HANDLE = 'seomatic';
33
34
    // Public Methods
35
    // =========================================================================
36
37
    /*
38
     * @return void
39
     */
40
    public function includeHtmlBeacon()
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
41
    {
42
        $view = Craft::$app->getView();
43
        $boomerangUrl = Craft::$app->assetManager->getPublishedUrl(
44
            '@nystudio107/webperf/assetbundles/boomerang/dist/js/boomerang-1.0.0.min.js',
45
            true
46
        );
47
        $boomerangTitle = $this->getDocumentTitle();
48
        $script = PluginTemplate::renderPluginTemplate(
49
            '_frontend/scripts/load-boomerang-iframe.twig',
50
            [
51
                'boomerangScriptUrl' => $boomerangUrl,
52
                'boomerangTitle' => $boomerangTitle,
53
            ]
54
        );
55
        $view->registerJs(
56
            $script,
57
            $view::POS_HEAD
58
        );
59
    }
60
61
    /*
62
     * @return void
63
     */
64
    public function includeAmpHtmlScript()
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
65
    {
66
        $view = Craft::$app->getView();
67
        $view->registerJsFile(
68
            self::AMP_IFRAME_SCRIPT_URL,
69
            [
70
                'position' => $view::POS_HEAD,
71
                'async' => 'async',
72
                'custom-element' => 'amp-iframe',
73
            ],
74
            'webperf-amp-iframe-script'
75
        );
76
    }
77
78
    /*
79
     * @return void
80
     */
81
    public function includeAmpHtmlBeacon()
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
82
    {
83
        $html = PluginTemplate::renderPluginTemplate(
84
            '_frontend/scripts/load-boomerang-amp-iframe.twig',
85
            [
86
                'boomerangIframeUrl' => UrlHelper::siteUrl('/webperf/render/amp-iframe'),
87
            ]
88
        );
89
        echo $html;
90
    }
91
92
    /*
93
     * @return void
94
     */
95
    public function ampHtmlIframe()
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
96
    {
97
        $boomerangUrl = Craft::$app->assetManager->getPublishedUrl(
98
            '@nystudio107/webperf/assetbundles/boomerang/dist/js/boomerang-1.0.0.min.js',
99
            true
100
        );
101
        $html = PluginTemplate::renderPluginTemplate(
102
            '_frontend/scripts/boomerang-amp-iframe-html.twig',
103
            [
104
                'boomerangScriptUrl' => $boomerangUrl,
105
            ]
106
        );
107
108
        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...
109
    }
110
111
    // Protected Methods
112
    // =========================================================================
113
114
    /**
115
     * Get the title of the currently rendering document
116
     *
117
     * @return string
118
     */
119
    protected function getDocumentTitle(): string
120
    {
121
        // Default to whatever the view title is
122
        $view = Craft::$app->getView();
123
        $docTitle = $view->title;
124
        // If SEOmatic is installed, get the title from it
125
        $seomatic = Craft::$app->getPlugins()->getPlugin(self::SEOMATIC_PLUGIN_HANDLE);
126
        if ($seomatic && Seomatic::$settings->renderEnabled) {
127
            $titleTag = Seomatic::$plugin->title->get('title');
128
            if ($titleTag) {
129
                $titleArray = $titleTag->renderAttributes();
130
                if (!empty($titleArray['title'])) {
131
                    $docTitle = $titleArray['title'];
132
                }
133
            }
134
        }
135
136
        return $docTitle;
137
    }
138
}
139