1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheIconic\Tracking\GoogleAnalytics\Network; |
4
|
|
|
|
5
|
|
|
use TheIconic\Tracking\GoogleAnalytics\AnalyticsResponse; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use GuzzleHttp\Psr7\Request; |
8
|
|
|
use GuzzleHttp\Promise; |
9
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
10
|
|
|
use Psr\Http\Message\RequestInterface; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class HttpClient |
15
|
|
|
* |
16
|
|
|
* @package TheIconic\Tracking\GoogleAnalytics |
17
|
|
|
*/ |
18
|
|
|
class HttpClient |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* User agent for the client. |
22
|
|
|
*/ |
23
|
|
|
const PHP_GA_MEASUREMENT_PROTOCOL_USER_AGENT = |
24
|
|
|
'THE ICONIC GA Measurement Protocol PHP Client (https://github.com/theiconic/php-ga-measurement-protocol)'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Timeout in seconds for the request connection and actual request execution. |
28
|
|
|
* Using the same value you can find in Google's PHP Client. |
29
|
|
|
*/ |
30
|
|
|
const REQUEST_TIMEOUT_SECONDS = 100; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* HTTP client. |
34
|
|
|
* |
35
|
|
|
* @var Client |
36
|
|
|
*/ |
37
|
|
|
private $client; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Holds the promises (async responses). |
41
|
|
|
* |
42
|
|
|
* @var PromiseInterface[] |
43
|
|
|
*/ |
44
|
|
|
private static $promises = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* We have to unwrap and send all promises at the end before analytics objects is destroyed. |
48
|
|
|
*/ |
49
|
|
|
public function __destruct() |
50
|
|
|
{ |
51
|
|
|
Promise\unwrap(self::$promises); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Sets HTTP client. |
56
|
|
|
* |
57
|
|
|
* @internal |
58
|
|
|
* @param Client $client |
59
|
|
|
*/ |
60
|
|
|
public function setClient(Client $client) |
61
|
|
|
{ |
62
|
|
|
$this->client = $client; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Gets HTTP client for internal class use. |
67
|
|
|
* |
68
|
|
|
* @return Client |
69
|
|
|
*/ |
70
|
|
|
private function getClient() |
71
|
|
|
{ |
72
|
|
|
if ($this->client === null) { |
73
|
|
|
// @codeCoverageIgnoreStart |
74
|
|
|
$this->setClient(new Client()); |
75
|
|
|
} |
76
|
|
|
// @codeCoverageIgnoreEnd |
77
|
|
|
|
78
|
|
|
return $this->client; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Sends request to Google Analytics. |
83
|
|
|
* |
84
|
|
|
* @internal |
85
|
|
|
* @param string $url |
86
|
|
|
* @param array $options |
87
|
|
|
* @return AnalyticsResponse |
88
|
|
|
*/ |
89
|
|
|
public function post($url, array $options = []) |
90
|
|
|
{ |
91
|
|
|
$request = new Request( |
92
|
|
|
'GET', |
93
|
|
|
$url, |
94
|
|
|
['User-Agent' => self::PHP_GA_MEASUREMENT_PROTOCOL_USER_AGENT] |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$opts = $this->parseOptions($options); |
98
|
|
|
$response = $this->getClient()->sendAsync($request, [ |
99
|
|
|
'synchronous' => !$opts['async'], |
100
|
|
|
'timeout' => $opts['timeout'], |
101
|
|
|
'connect_timeout' => $opts['timeout'], |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
if ($opts['async']) { |
105
|
|
|
self::$promises[] = $response; |
106
|
|
|
} else { |
107
|
|
|
$response = $response->wait(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->getAnalyticsResponse($request, $response); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Parse the given options and fill missing fields with default values. |
115
|
|
|
* |
116
|
|
|
* @param array $options |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
private function parseOptions(array $options) |
120
|
|
|
{ |
121
|
|
|
$defaultOptions = [ |
122
|
|
|
'timeout' => static::REQUEST_TIMEOUT_SECONDS, |
123
|
|
|
'async' => false, |
124
|
|
|
]; |
125
|
|
|
|
126
|
|
|
$opts = []; |
127
|
|
|
foreach ($defaultOptions as $option => $value) { |
128
|
|
|
$opts[$option] = isset($options[$option]) ? $options[$option] : $defaultOptions[$option]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (!is_int($opts['timeout']) || $opts['timeout'] <= 0) { |
132
|
|
|
throw new \UnexpectedValueException('The timeout must be an integer with a value greater than 0'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (!is_bool($opts['async'])) { |
136
|
|
|
throw new \UnexpectedValueException('The async option must be boolean'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $opts; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Creates an analytics response object. |
144
|
|
|
* |
145
|
|
|
* @param RequestInterface $request |
146
|
|
|
* @param ResponseInterface|PromiseInterface $response |
147
|
|
|
* @return AnalyticsResponse |
148
|
|
|
*/ |
149
|
|
|
protected function getAnalyticsResponse(RequestInterface $request, $response) |
150
|
|
|
{ |
151
|
|
|
return new AnalyticsResponse($request, $response); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|