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