Piwik::piwikUrl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr7Middlewares\Utils;
8
9
class Piwik
10
{
11
    use Utils\HtmlInjectorTrait;
12
    use Utils\AttributeTrait;
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
     * ...
78
     *
79
     * @return self
80
     */
81
    public function addOption()
82
    {
83
        $this->options[] = func_get_args();
84
85
        return $this;
86
    }
87
88
    /**
89
     * Execute the middleware.
90
     *
91
     * @param ServerRequestInterface $request
92
     * @param ResponseInterface      $response
93
     * @param callable               $next
94
     *
95
     * @return ResponseInterface
96
     */
97 View Code Duplication
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
98
    {
99
        $response = $next($request, $response);
100
101
        if (Utils\Helpers::getMimeType($response) === 'text/html' && !Utils\Helpers::isAjax($request)) {
102
            return $this->inject($response, $this->getCode());
103
        }
104
105
        return $response;
106
    }
107
108
    /**
109
     * Returns the piwik code.
110
     *
111
     * @return string
112
     */
113
    private function getCode()
114
    {
115
        $_paq = '';
116
117
        foreach ($this->options as $option) {
118
            $option[0] = "'".$option[0]."'";
119
            $_paq .= sprintf('_paq.push([%s]);', implode(',', $option));
120
        }
121
122
        return <<<PWK
123
<script>
124
    var _paq = _paq || [];
125
    {$_paq}
126
    (function() {
127
        var u="{$this->piwikUrl}";
128
        _paq.push(['setTrackerUrl', u+'piwik.php']);
129
        _paq.push(['setSiteId', {$this->siteId}]);
130
        var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
131
        g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
132
    })();
133
</script>
134
<noscript><p><img src="{$this->piwikUrl}piwik.php?idsite={$this->siteId}" style="border:0;" alt="" /></p></noscript>
135
PWK;
136
    }
137
}
138