1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheIconic\Tracking\GoogleAnalytics; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\RequestInterface; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AnalyticsResponse |
11
|
|
|
* |
12
|
|
|
* Represents the response got from GA. |
13
|
|
|
* |
14
|
|
|
* @package TheIconic\Tracking\GoogleAnalytics |
15
|
|
|
*/ |
16
|
|
|
class AnalyticsResponse |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* HTTP status code for the response. |
20
|
|
|
* |
21
|
|
|
* @var null|int |
22
|
|
|
*/ |
23
|
|
|
protected $httpStatusCode; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Request URI that was used to send the hit. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $requestUrl; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Response body. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $responseBody; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Gets the relevant data from the Guzzle clients. |
41
|
|
|
* |
42
|
|
|
* @param RequestInterface $request |
43
|
|
|
* @param ResponseInterface|PromiseInterface $response |
44
|
|
|
*/ |
45
|
|
|
public function __construct(RequestInterface $request, $response) |
46
|
|
|
{ |
47
|
|
|
if ($response instanceof ResponseInterface) { |
48
|
|
|
$this->httpStatusCode = $response->getStatusCode(); |
49
|
|
|
$this->responseBody = $response->getBody()->getContents(); |
50
|
|
|
} elseif ($response instanceof PromiseInterface) { |
51
|
|
|
$this->httpStatusCode = null; |
52
|
|
|
$this->responseBody = null; |
53
|
|
|
} else { |
54
|
|
|
throw new \InvalidArgumentException( |
55
|
|
|
'Second constructor argument "response" must be instance of ResponseInterface or PromiseInterface' |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->requestUrl = (string)$request->getUri(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Gets the HTTP status code. |
64
|
|
|
* It return NULL if the request was asynchronous since we are not waiting for the response. |
65
|
|
|
* |
66
|
|
|
* @return null|int |
67
|
|
|
*/ |
68
|
|
|
public function getHttpStatusCode() |
69
|
|
|
{ |
70
|
|
|
return $this->httpStatusCode; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Gets the request URI used to get the response. |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function getRequestUrl() |
79
|
|
|
{ |
80
|
|
|
return $this->requestUrl; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Gets the response body. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getResponseBody() |
89
|
|
|
{ |
90
|
|
|
return $this->responseBody; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|