1
|
|
|
<?php namespace MaartenStaa\GameAnalytics; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* A single message to be sent to GA. |
5
|
|
|
* |
6
|
|
|
* @author Maarten Staa |
7
|
|
|
*/ |
8
|
|
|
class Message |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The URI of the endpoint that this message will be sent to. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $endpoint; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The GameAnalytics client that this message belongs to. |
19
|
|
|
* |
20
|
|
|
* @var \MaartenStaa\GameAnalytics\Client |
21
|
|
|
*/ |
22
|
|
|
protected $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The parameters for this message. These will be JSON encoded into the request |
26
|
|
|
* body when sending the message. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $payload = array(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor. |
34
|
|
|
* |
35
|
|
|
* @param string $endpoint |
36
|
|
|
* @param \MaartenStaa\GameAnalytics\Client $client |
37
|
|
|
*/ |
38
|
|
|
public function __construct($endpoint, Client $client) |
39
|
|
|
{ |
40
|
|
|
$this->endpoint = $endpoint; |
41
|
|
|
$this->client = $client; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the configured endpoint for this message. |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getEndpoint() |
50
|
|
|
{ |
51
|
|
|
return $this->endpoint; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the configured GameAnalytics client. |
56
|
|
|
* |
57
|
|
|
* @return \MaartenStaa\GameAnalytics\Client |
58
|
|
|
*/ |
59
|
|
|
public function getClient() |
60
|
|
|
{ |
61
|
|
|
return $this->client; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the payload of all the parameters that are set on this message. |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
public function getPayload() |
70
|
|
|
{ |
71
|
|
|
return $this->payload; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set a value to be sent as part of this message. Use either as |
76
|
|
|
* set($myKey, $myValue) or as set(array('key1' => 'value1', 'key2' => 'value2')). |
77
|
|
|
* |
78
|
|
|
* @param string|array $key |
79
|
|
|
* @param mixed|null $value |
80
|
|
|
* @return \MaartenStaa\GameAnalytics\Message |
81
|
|
|
*/ |
82
|
|
|
public function set($key, $value = null) |
83
|
|
|
{ |
84
|
|
|
if ($value === null && is_array($key)) { |
85
|
|
|
$this->payload = array_merge($this->payload, $key); |
86
|
|
|
} else { |
87
|
|
|
$this->payload[$key] = $value; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Send this message to the configured endpoint using the HTTP adapter. |
95
|
|
|
* |
96
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
97
|
|
|
*/ |
98
|
|
|
public function send() |
99
|
|
|
{ |
100
|
|
|
$request = $this->buildRequest(); |
101
|
|
|
|
102
|
|
|
return $this->getClient()->getHttp()->sendRequest($request); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Build the request to send. |
107
|
|
|
* |
108
|
|
|
* @return \Psr\Http\Message\RequestInterface |
109
|
|
|
*/ |
110
|
|
|
protected function buildRequest() |
111
|
|
|
{ |
112
|
|
|
// Prepare body of request. |
113
|
|
|
$body = $this->getGzippedBody(); |
114
|
|
|
|
115
|
|
|
// Build the request and return it. |
116
|
|
|
return $this->getClient()->getMessageFactory() |
117
|
|
|
->createRequest('POST', $this->getEndpoint(), array(), $body) |
118
|
|
|
->withHeader('Content-Type', 'application/json') |
119
|
|
|
->withHeader('Content-Encoding', 'gzip') |
120
|
|
|
->withHeader('Authorization', $this->getAuthorization($body)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the GZipped JSON-encoded request body. |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
protected function getGzippedBody() |
129
|
|
|
{ |
130
|
|
|
$body = json_encode($this->getPayload()); |
131
|
|
|
|
132
|
|
|
return gzencode($body); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the contents of the Authorization header based on the given request |
137
|
|
|
* body. Returns the base-64 encoded HMAC SHA-256 digest. |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
protected function getAuthorization($body) |
142
|
|
|
{ |
143
|
|
|
return base64_encode(hash_hmac('sha256', $body, $this->client->getSecret(), true)); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|