1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheIconic\Tracking\GoogleAnalytics\Network; |
4
|
|
|
|
5
|
|
|
use TheIconic\Tracking\GoogleAnalytics\AnalyticsResponse; |
6
|
|
|
use TheIconic\Tracking\GoogleAnalytics\Parameters\SingleParameter; |
7
|
|
|
use TheIconic\Tracking\GoogleAnalytics\Parameters\CompoundParameterCollection; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use GuzzleHttp\Psr7\Request; |
10
|
|
|
use GuzzleHttp\Promise; |
11
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
12
|
|
|
use Psr\Http\Message\RequestInterface; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class HttpClient |
17
|
|
|
* |
18
|
|
|
* @package TheIconic\Tracking\GoogleAnalytics |
19
|
|
|
*/ |
20
|
|
|
class HttpClient |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* User agent for the client. |
24
|
|
|
*/ |
25
|
|
|
const PHP_GA_MEASUREMENT_PROTOCOL_USER_AGENT = |
26
|
|
|
'THE ICONIC GA Measurement Protocol PHP Client (https://github.com/theiconic/php-ga-measurement-protocol)'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Timeout in seconds for the request connection and actual request execution. |
30
|
|
|
* Using the same value you can find in Google's PHP Client. |
31
|
|
|
*/ |
32
|
|
|
const REQUEST_TIMEOUT_SECONDS = 100; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* HTTP client. |
36
|
|
|
* |
37
|
|
|
* @var Client |
38
|
|
|
*/ |
39
|
|
|
private $client; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Holds the promises (async responses). |
43
|
|
|
* |
44
|
|
|
* @var PromiseInterface[] |
45
|
|
|
*/ |
46
|
|
|
private static $promises = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* We have to unwrap and send all promises at the end before analytics objects is destroyed. |
50
|
|
|
*/ |
51
|
|
|
public function __destruct() |
52
|
|
|
{ |
53
|
|
|
Promise\unwrap(self::$promises); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Sets HTTP client. |
58
|
|
|
* |
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
|
|
|
* @param string $url |
86
|
|
|
* @param boolean $nonBlocking |
87
|
|
|
* @return AnalyticsResponse |
88
|
|
|
*/ |
89
|
|
|
public function post($url, $nonBlocking = false) |
90
|
|
|
{ |
91
|
|
|
$request = new Request( |
92
|
|
|
'GET', |
93
|
|
|
$url, |
94
|
|
|
['User-Agent' => self::PHP_GA_MEASUREMENT_PROTOCOL_USER_AGENT] |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$response = $this->getClient()->sendAsync($request, [ |
98
|
|
|
'synchronous' => !$nonBlocking, |
99
|
|
|
'timeout' => self::REQUEST_TIMEOUT_SECONDS, |
100
|
|
|
'connect_timeout' => self::REQUEST_TIMEOUT_SECONDS, |
101
|
|
|
]); |
102
|
|
|
|
103
|
|
|
if ($nonBlocking) { |
104
|
|
|
self::$promises[] = $response; |
105
|
|
|
} else { |
106
|
|
|
$response = $response->wait(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->getAnalyticsResponse($request, $response); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Creates an analytics response object. |
114
|
|
|
* |
115
|
|
|
* @param RequestInterface $request |
116
|
|
|
* @param ResponseInterface|PromiseInterface $response |
117
|
|
|
* @return AnalyticsResponse |
118
|
|
|
*/ |
119
|
|
|
protected function getAnalyticsResponse(RequestInterface $request, $response) |
120
|
|
|
{ |
121
|
|
|
return new AnalyticsResponse($request, $response); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|