Completed
Push — master ( 47e0e4...864791 )
by Werner
02:32
created

PiwikExtension::getPiwik()   D

Complexity

Conditions 10
Paths 7

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 34
rs 4.8196
cc 10
eloc 20
nc 7
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class PiwikExtension extends Extension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
6
    /**
7
     * @var string the url to the server, without protocol and trailing slash, e.g. //piwik.foo.com/
8
     */
9
    private static $piwik_server = '//piwik.foo.com/';
0 ignored issues
show
Unused Code introduced by
The property $piwik_server is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
10
11
    /**
12
     * @var int the piwik site id
13
     */
14
    private static $piwik_site_id = 0;
0 ignored issues
show
Unused Code introduced by
The property $piwik_site_id is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
15
16
    /**
17
     * @var bool Do you want tracking code in dev environments?
18
     */
19
    private static $show_on_dev = false;
0 ignored issues
show
Unused Code introduced by
The property $show_on_dev is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
20
21
    /**
22
     * @var bool do you want tracking code in test environments?
23
     */
24
    private static $show_on_test = false;
0 ignored issues
show
Unused Code introduced by
The property $show_on_test is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
25
26
    /**
27
     * @var bool we want tracking code in live environments
28
     */
29
    private static $show_on_live = true;
0 ignored issues
show
Unused Code introduced by
The property $show_on_live is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
31
    /**
32
     * @var bool include tracking code on contentcontrollerInit
33
     */
34
    private static $auto_include = true;
0 ignored issues
show
Unused Code introduced by
The property $auto_include is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
35
36
    /**
37
     * @var bool include tracking code automatically in backend, subclasses of LeftAndMain
38
     */
39
    private static $include_in_backend = false;
0 ignored issues
show
Unused Code introduced by
The property $include_in_backend is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
40
41
    /**
42
     * includes the piwik tracking code when ContentController initializes...
43
     * @todo: get it working ;)
44
     */
45
    public function onAfterInit(&$controller)
0 ignored issues
show
Unused Code introduced by
The parameter $controller is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
    {
47
        if ($this->autoInclude() && $js = $this->getPiwik(false)) {
48
            Requirements::customScript($js, 'piwiktrackingcode');
49
        }
50
    }
51
52
    /**
53
     * generates piwik tracking code out of config vars and Piwik.ss template
54
     * @param $wrap wrap inside <script> tags, e.g. for templates
55
     */
56
    public function getPiwik($wrap = true)
57
    {
58
        if (Director::is_cli()) {
59
            return false;
60
        }
61
        //don't include on dev/build etc...
62
        if (Controller::curr() instanceof DevelopmentAdmin) {
63
            return false;
64
        }
65
        if (Director::isDev() && !Config::inst()->get('PiwikExtension', 'show_on_dev')) {
66
            return false;
67
        }
68
        if (Director::isTest() && !Config::inst()->get('PiwikExtension', 'show_on_test')) {
69
            return false;
70
        }
71
        if (Director::isLive() && !Config::inst()->get('PiwikExtension', 'show_on_live')) {
72
            return false;
73
        }
74
75
        //used for overwriting defaults in SiteConfig, e.g. for different SiteIDs in a Subsite installation
76
        $currentSiteConfig = Controller::curr()->hasMethod('getSiteConfig')
77
            ? Controller::curr()->getSiteConfig()
0 ignored issues
show
Bug introduced by
The method getSiteConfig() does not exist on Controller. Did you maybe mean config()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
78
            : SiteConfig::current_site_config();
79
80
81
        $data = array(
82
            'WrapInJsTags' => $wrap,
83
            'URL' => Config::inst()->get('PiwikExtension', 'piwik_server'),
84
            'SiteID' => Config::inst()->get('PiwikExtension', 'piwik_site_id'),
85
            'SiteConfig' => $currentSiteConfig
86
        );
87
88
        return ArrayData::create($data)->renderWith(array('Piwik'));
89
    }
90
91
    /**
92
     * Helper function to define if tracking code should be included automatically
93
     * @return bool
94
     */
95
    public function autoInclude()
96
    {
97
        if (! Config::inst()->get('PiwikExtension', 'auto_include')) {
98
            return false;
99
        }
100
101
        if ($this->isBackend() && !Config::inst()->get('PiwikExtension', 'include_in_backend')) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !($this->isBacken...'include_in_backend'));.
Loading history...
102
            return false;
103
        }
104
105
        return true;
106
    }
107
108
109
    /**
110
     * @return bool is the extended controller an instance of LeftAndMain
111
     */
112
    public function isBackend()
113
    {
114
        return Controller::curr() instanceof LeftAndMain;
115
    }
116
}
117