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

GoogleAnalytics::getCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
rs 9.4286
cc 1
eloc 5
nc 1
nop 0
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 GoogleAnalytics
11
{
12
    use Utils\HtmlInjectorTrait;
13
14
    /**
15
     * @var string|null The site's ID
16
     */
17
    private $siteId;
18
19
    /**
20
     * Constructor.Set the site's ID.
21
     *
22
     * @param string $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
23
     */
24
    public function __construct($siteId = null)
25
    {
26
        if ($siteId !== null) {
27
            $this->siteId($siteId);
28
        }
29
    }
30
31
    /**
32
     * Set the site's id.
33
     *
34
     * @param string $siteId
35
     *
36
     * @return self
37
     */
38
    public function siteId($siteId)
39
    {
40
        $this->siteId = (string) $siteId;
41
42
        return $this;
43
    }
44
45
    /**
46
     * Execute the middleware.
47
     *
48
     * @param ServerRequestInterface $request
49
     * @param ResponseInterface      $response
50
     * @param callable               $next
51
     *
52
     * @return ResponseInterface
53
     */
54 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...
55
    {
56
        $response = $next($request, $response);
57
58
        if ($this->isInjectable($request)) {
59
            return $this->inject($response, $this->getCode());
60
        }
61
62
        return $response;
63
    }
64
65
    /**
66
     * Returns the google code.
67
     * https://github.com/h5bp/html5-boilerplate/blob/master/src/index.html.
68
     * 
69
     * @return string
70
     */
71
    private function getCode()
72
    {
73
        return <<<GA
74
<script>
75
    (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
76
    function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
77
    e=o.createElement(i);r=o.getElementsByTagName(i)[0];
78
    e.src='https://www.google-analytics.com/analytics.js';
79
    r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
80
    ga('create','{$this->siteId}','auto');ga('send','pageview');
81
</script>
82
GA;
83
    }
84
}
85