InstantAnalyticsTwigExtension   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 12
eloc 30
c 4
b 1
f 0
dl 0
loc 138
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getFunctions() 0 7 1
A pageViewAnalytics() 0 3 1
A getGlobals() 0 15 4
A eventAnalytics() 0 3 1
A getFilters() 0 7 1
A analytics() 0 3 1
A eventTrackingUrl() 0 14 1
A getName() 0 3 1
A pageViewTrackingUrl() 0 3 1
1
<?php
2
/**
3
 * Instant Analytics plugin for Craft CMS
4
 *
5
 * Instant Analytics brings full Google Analytics support to your Twig templates
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) 2017 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\instantanalytics\twigextensions;
12
13
use Craft;
14
use craft\helpers\Template;
15
use nystudio107\instantanalytics\helpers\IAnalytics;
16
use nystudio107\instantanalytics\InstantAnalytics;
17
use Twig\Extension\AbstractExtension;
18
use Twig\Extension\GlobalsInterface;
19
use Twig\Markup;
20
use Twig\TwigFilter;
21
use Twig\TwigFunction;
22
23
/**
24
 * Twig can be extended in many ways; you can add extra tags, filters, tests,
25
 * operators, global variables, and functions. You can even extend the parser
26
 * itself with node visitors.
27
 *
28
 * http://twig.sensiolabs.org/doc/advanced.html
0 ignored issues
show
Coding Style introduced by
Doc comment long description must start with a capital letter
Loading history...
29
 *
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 for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
31
 * @package   InstantAnalytics
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag 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 for @since tag 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 InstantAnalyticsTwigExtension extends AbstractExtension implements GlobalsInterface
35
{
36
    // Public Methods
37
    // =========================================================================
38
39
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
40
     * @inheritdoc
41
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
42
    public function getName()
43
    {
44
        return 'InstantAnalytics';
45
    }
46
47
    /** @noinspection ReturnTypeCanBeDeclaredInspection */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
48
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
49
     * @@inheritdoc
50
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
51
    public function getGlobals()
52
    {
53
        $globals = [];
54
        $view = Craft::$app->getView();
55
        if ($view->getIsRenderingPageTemplate()) {
56
            $request = Craft::$app->getRequest();
57
            if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) {
58
                // Return our Analytics object as a Twig global
59
                $globals = [
60
                    'instantAnalytics' => InstantAnalytics::$plugin->ia->getGlobals(InstantAnalytics::$currentTemplate),
61
                ];
62
            }
63
        }
64
65
        return $globals;
66
    }
67
68
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
69
     * @inheritdoc
70
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
71
    public function getFilters()
72
    {
73
        return [
74
            new TwigFilter('pageViewAnalytics', [$this, 'pageViewAnalytics']),
75
            new TwigFilter('eventAnalytics', [$this, 'eventAnalytics']),
76
            new TwigFilter('pageViewTrackingUrl', [$this, 'pageViewTrackingUrl']),
77
            new TwigFilter('eventTrackingUrl', [$this, 'eventTrackingUrl']),
78
        ];
79
    }
80
81
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
82
     * @inheritdoc
83
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
84
    public function getFunctions()
85
    {
86
        return [
87
            new TwigFunction('pageViewAnalytics', [$this, 'pageViewAnalytics']),
88
            new TwigFunction('eventAnalytics', [$this, 'eventAnalytics']),
89
            new TwigFunction('pageViewTrackingUrl', [$this, 'pageViewTrackingUrl']),
90
            new TwigFunction('eventTrackingUrl', [$this, 'eventTrackingUrl']),
91
        ];
92
    }
93
94
    /**
95
     * Get a PageView analytics object
96
     *
97
     * @param string $url
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
98
     * @param string $title
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
99
     *
100
     * @return null|IAnalytics object
101
     */
102
    public function pageViewAnalytics($url = '', $title = '')
103
    {
104
        return InstantAnalytics::$plugin->ia->pageViewAnalytics($url, $title);
105
    }
106
107
    /**
108
     * Get an Event analytics object
109
     *
110
     * @param string $eventCategory
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
111
     * @param string $eventAction
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
112
     * @param string $eventLabel
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
113
     * @param int $eventValue
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
114
     *
115
     * @return null|IAnalytics
116
     */
117
    public function eventAnalytics($eventCategory = '', $eventAction = '', $eventLabel = '', $eventValue = 0)
118
    {
119
        return InstantAnalytics::$plugin->ia->eventAnalytics($eventCategory, $eventAction, $eventLabel, $eventValue);
120
    }
121
122
    /**
123
     * Return an Analytics object
124
     *
125
     * @return null|IAnalytics
126
     */
127
    public function analytics()
128
    {
129
        return InstantAnalytics::$plugin->ia->analytics();
130
    }
131
132
    /**
133
     * Get a PageView tracking URL
134
     *
135
     * @param $url
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
136
     * @param $title
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
137
     *
138
     * @return Markup
139
     * @throws \yii\base\Exception
140
     */
141
    public function pageViewTrackingUrl($url, $title): Markup
142
    {
143
        return Template::raw(InstantAnalytics::$plugin->ia->pageViewTrackingUrl($url, $title));
144
    }
145
146
    /**
147
     * Get an Event tracking URL
148
     *
149
     * @param        $url
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 1 spaces but found 8
Loading history...
150
     * @param string $eventCategory
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
151
     * @param string $eventAction
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
152
     * @param string $eventLabel
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
153
     * @param int $eventValue
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
154
     *
155
     * @return Markup
156
     * @throws \yii\base\Exception
157
     */
158
    public function eventTrackingUrl(
159
        $url,
160
        $eventCategory = '',
161
        $eventAction = '',
162
        $eventLabel = '',
163
        $eventValue = 0
164
    ): Markup
165
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
166
        return Template::raw(InstantAnalytics::$plugin->ia->eventTrackingUrl(
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...
167
            $url,
168
            $eventCategory,
169
            $eventAction,
170
            $eventLabel,
171
            $eventValue
172
        ));
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...
173
    }
174
}
175