Completed
Push — master ( b2e200...1332b8 )
by Oscar
04:48
created

Piwik   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 129
Duplicated Lines 7.75 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 10
c 3
b 2
f 0
lcom 1
cbo 1
dl 10
loc 129
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A siteId() 0 6 1
A piwikUrl() 0 11 2
A addOption() 0 6 1
A __invoke() 10 10 2
B getCode() 0 24 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Middleware;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr7Middlewares\Utils;
9
10
class Piwik
11
{
12
    use Utils\HtmlInjectorTrait;
13
14
    private $options = [
15
        ['trackPageView'],
16
        ['enableLinkTracking'],
17
    ];
18
19
    /**
20
     * @var int|null The site's ID
21
     */
22
    private $siteId = 1;
23
24
    /**
25
     * @var string|null The Piwik url
26
     */
27
    private $piwikUrl;
28
29
    /**
30
     * Constructor.Set the Piwik's url.
31
     *
32
     * @param string $piwikUrl
33
     */
34
    public function __construct($piwikUrl = null)
35
    {
36
        if ($piwikUrl !== null) {
37
            $this->piwikUrl($piwikUrl);
38
        }
39
    }
40
41
    /**
42
     * Set the site's id.
43
     *
44
     * @param int $siteId
45
     *
46
     * @return self
47
     */
48
    public function siteId($siteId)
49
    {
50
        $this->siteId = $siteId;
51
52
        return $this;
53
    }
54
55
    /**
56
     * Set the piwik url.
57
     *
58
     * @param string $url
59
     *
60
     * @return self
61
     */
62
    public function piwikUrl($url)
63
    {
64
        $this->piwikUrl = (string) $url;
65
66
        //ensure the url is ending by "/"
67
        if (substr($this->piwikUrl, -1) !== '/') {
68
            $this->piwikUrl .= '/';
69
        }
70
71
        return $this;
72
    }
73
74
    /**
75
     * Add an option.
76
     *
77
     * @param string $name
78
     *                     ...
79
     *
80
     * @return self
81
     */
82
    public function addOption($name)
0 ignored issues
show
Unused Code introduced by
The parameter $name 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...
83
    {
84
        $this->options[] = func_get_args();
85
86
        return $this;
87
    }
88
89
    /**
90
     * Execute the middleware.
91
     *
92
     * @param ServerRequestInterface $request
93
     * @param ResponseInterface      $response
94
     * @param callable               $next
95
     *
96
     * @return ResponseInterface
97
     */
98 View Code Duplication
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100
        $response = $next($request, $response);
101
102
        if ($this->isInjectable($request)) {
103
            return $this->inject($response, $this->getCode());
104
        }
105
106
        return $response;
107
    }
108
109
    /**
110
     * Returns the piwik code.
111
     * 
112
     * @return string
113
     */
114
    private function getCode()
115
    {
116
        $_paq = '';
117
118
        foreach ($this->options as $option) {
119
            $option[0] = "'".$option[0]."'";
120
            $_paq .= sprintf('_paq.push([%s]);', implode(',', $option));
121
        }
122
123
        return <<<PWK
124
<script>
125
    var _paq = _paq || [];
126
    {$_paq}
127
    (function() {
128
        var u="{$this->piwikUrl}";
129
        _paq.push(['setTrackerUrl', u+'piwik.php']);
130
        _paq.push(['setSiteId', {$this->siteId}]);
131
        var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
132
        g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
133
    })();
134
</script>
135
<noscript><p><img src="{$this->piwikUrl}piwik.php?idsite={$this->siteId}" style="border:0;" alt="" /></p></noscript>
136
PWK;
137
    }
138
}
139