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 |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
|
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 methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.