1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BlizzardApi; |
4
|
|
|
|
5
|
|
|
use BlizzardApi\Tokens\Access; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
8
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Blizzard Client |
12
|
|
|
* |
13
|
|
|
* @author Oleg Kachinsky <[email protected]> |
14
|
|
|
* @author Hristo Mitev <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class BlizzardClient |
17
|
|
|
{ |
18
|
|
|
const API_URL_PATTERN = 'https://region.api.battle.net'; |
19
|
|
|
const API_ACCESS_TOKEN_URL_PATTERN = 'https://region.battle.net/oauth/token'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string $apiUrl Blizzard API url |
23
|
|
|
*/ |
24
|
|
|
private $apiUrl; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string $apiAccessTokenUrl Blizzard API Access Token url |
28
|
|
|
*/ |
29
|
|
|
private $apiAccessTokenUrl; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string $apiKey API key |
33
|
|
|
*/ |
34
|
|
|
private $apiKey; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Access[] $accessTokens Access tokens |
38
|
|
|
*/ |
39
|
|
|
private $accessTokens; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string $locale Locale |
43
|
|
|
*/ |
44
|
|
|
private $locale; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string $region Region |
48
|
|
|
*/ |
49
|
|
|
private $region; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Constructor |
53
|
|
|
* |
54
|
|
|
* @param string $apiKey API key |
55
|
|
|
* @param string $apiSecret API Secret key |
56
|
|
|
* @param string $region Region |
57
|
|
|
* @param string $locale Locale |
58
|
|
|
*/ |
59
|
|
|
public function __construct($apiKey, $apiSecret, $region = 'us', $locale = 'en_us') |
60
|
|
|
{ |
61
|
|
|
$options = [ |
62
|
|
|
'apiKey' => $apiKey, |
63
|
|
|
'apiSecret' => $apiSecret, |
64
|
|
|
'region' => strtolower($region), |
65
|
|
|
'locale' => strtolower($locale), |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
$resolver = new OptionsResolver(); |
69
|
|
|
$this->configureOptions($resolver, $options['region']); |
70
|
|
|
|
71
|
|
|
$options = $resolver->resolve($options); |
72
|
|
|
|
73
|
|
|
$this->apiKey = $options['apiKey']; |
74
|
|
|
$this->apiSecret = $options['apiSecret']; |
|
|
|
|
75
|
|
|
$this->region = $options['region']; |
76
|
|
|
$this->locale = $options['locale']; |
77
|
|
|
|
78
|
|
|
$this->updateApiUrl($options['region']); |
79
|
|
|
$this->updateApiAccessTokenUrl($options['region']); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get api url |
84
|
|
|
* |
85
|
|
|
* @return string Api url |
86
|
|
|
*/ |
87
|
|
|
public function getApiUrl() |
88
|
|
|
{ |
89
|
|
|
return $this->apiUrl; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get api Access Token url |
94
|
|
|
* |
95
|
|
|
* @return string Api Access Token url |
96
|
|
|
*/ |
97
|
|
|
public function getApiAccessTokenUrl() |
98
|
|
|
{ |
99
|
|
|
return $this->apiAccessTokenUrl; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get api key |
104
|
|
|
* |
105
|
|
|
* @return string Api key |
106
|
|
|
*/ |
107
|
|
|
public function getApiKey() |
108
|
|
|
{ |
109
|
|
|
return $this->apiKey; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Set api key |
114
|
|
|
* |
115
|
|
|
* @param string $apiKey Api key |
116
|
|
|
* |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function setApiKey($apiKey) |
120
|
|
|
{ |
121
|
|
|
$this->apiKey = $apiKey; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get api secret |
128
|
|
|
* |
129
|
|
|
* @return string Api secret |
130
|
|
|
*/ |
131
|
|
|
public function getApiSecret() |
132
|
|
|
{ |
133
|
|
|
return $this->apiSecret; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set api secret |
138
|
|
|
* |
139
|
|
|
* @param string $apiSecret Api secret |
140
|
|
|
* |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
|
|
public function setApiSecret($apiSecret) |
144
|
|
|
{ |
145
|
|
|
$this->apiSecret = $apiSecret; |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get region |
152
|
|
|
* |
153
|
|
|
* @return string Region |
154
|
|
|
*/ |
155
|
|
|
public function getRegion() |
156
|
|
|
{ |
157
|
|
|
return strtolower($this->region); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Set region |
162
|
|
|
* |
163
|
|
|
* @param string $region region |
164
|
|
|
* |
165
|
|
|
* @return $this |
166
|
|
|
*/ |
167
|
|
|
public function setRegion($region) |
168
|
|
|
{ |
169
|
|
|
$this->region = strtolower($region); |
170
|
|
|
|
171
|
|
|
$this->updateApiUrl($region); |
172
|
|
|
$this->updateApiAccessTokenUrl($region); |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get locale |
179
|
|
|
* |
180
|
|
|
* @return string Locale |
181
|
|
|
*/ |
182
|
|
|
public function getLocale() |
183
|
|
|
{ |
184
|
|
|
return strtolower($this->locale); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Set locale |
189
|
|
|
* |
190
|
|
|
* @param string $locale locale |
191
|
|
|
* |
192
|
|
|
* @return $this |
193
|
|
|
*/ |
194
|
|
|
public function setLocale($locale) |
195
|
|
|
{ |
196
|
|
|
$this->locale = strtolower($locale); |
197
|
|
|
|
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Get access token |
203
|
|
|
* |
204
|
|
|
* @return null|string Access token |
205
|
|
|
*/ |
206
|
|
|
public function getAccessToken() |
207
|
|
|
{ |
208
|
|
|
if (null === $this->accessTokens) { |
209
|
|
|
$this->accessTokens = []; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if (!array_key_exists($this->getRegion(), $this->accessTokens)) { |
213
|
|
|
$this->accessTokens[$this->getRegion()] = $this->requestAccessToken(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $this->accessTokens[$this->getRegion()]->getToken(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Set access token |
221
|
|
|
* |
222
|
|
|
* @param null|Access $accessToken Access token |
223
|
|
|
* |
224
|
|
|
* @return $this |
225
|
|
|
*/ |
226
|
|
|
public function setAccessToken(Access $accessToken) |
227
|
|
|
{ |
228
|
|
|
$this->accessTokens[$this->getRegion()] = $accessToken; |
229
|
|
|
|
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Update API url |
235
|
|
|
* |
236
|
|
|
* Update API url by replacing region in API url pattern |
237
|
|
|
* |
238
|
|
|
* @param string $region Region |
239
|
|
|
* |
240
|
|
|
* @return $this |
241
|
|
|
*/ |
242
|
|
|
private function updateApiUrl($region) |
243
|
|
|
{ |
244
|
|
|
$this->apiUrl = str_replace('region', strtolower($region), self::API_URL_PATTERN); |
245
|
|
|
|
246
|
|
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Update API Access Token url |
251
|
|
|
* |
252
|
|
|
* Update API Access Token url by replacing region in API url pattern |
253
|
|
|
* |
254
|
|
|
* @param string $region Region |
255
|
|
|
* |
256
|
|
|
* @return $this |
257
|
|
|
*/ |
258
|
|
|
private function updateApiAccessTokenUrl($region) |
259
|
|
|
{ |
260
|
|
|
$this->apiAccessTokenUrl = str_replace('region', strtolower($region), self::API_ACCESS_TOKEN_URL_PATTERN); |
261
|
|
|
|
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Configure options |
267
|
|
|
* |
268
|
|
|
* @param OptionsResolver $resolver Symfony options resolver |
269
|
|
|
* @param string $region Region |
270
|
|
|
* |
271
|
|
|
* @throws InvalidOptionsException |
272
|
|
|
*/ |
273
|
|
|
private function configureOptions(OptionsResolver $resolver, $region) |
274
|
|
|
{ |
275
|
|
|
if (isset(GeoData::$list[$region])) { |
276
|
|
|
$locales = GeoData::$list[$region]; |
277
|
|
|
} else { |
278
|
|
|
throw new InvalidOptionsException( |
279
|
|
|
sprintf( |
280
|
|
|
'The option "region" with value "%s" is invalid. Accepted values are: "%s".', |
281
|
|
|
$region, |
282
|
|
|
implode('", "', array_keys(GeoData::$list)) |
283
|
|
|
) |
284
|
|
|
); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
$resolver->setRequired(['apiKey', 'apiSecret', 'region', 'locale']) |
288
|
|
|
->setAllowedTypes('apiKey', 'string') |
289
|
|
|
->setAllowedTypes('apiSecret', 'string') |
290
|
|
|
->setAllowedTypes('region', 'string') |
291
|
|
|
->setAllowedValues('region', array_keys(GeoData::$list)) |
292
|
|
|
->setAllowedTypes('locale', 'string') |
293
|
|
|
->setAllowedValues('locale', $locales); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Request an Access Token from Blizzard |
298
|
|
|
* |
299
|
|
|
* @return Access |
300
|
|
|
* |
301
|
|
|
* @throws \HttpResponseException |
302
|
|
|
*/ |
303
|
|
|
protected function requestAccessToken() |
304
|
|
|
{ |
305
|
|
|
$options = [ |
306
|
|
|
'form_params' => [ |
307
|
|
|
'grant_type' => 'client_credentials', |
308
|
|
|
'client_id' => $this->getApiKey(), |
309
|
|
|
'client_secret' => $this->getApiSecret(), |
310
|
|
|
], |
311
|
|
|
]; |
312
|
|
|
|
313
|
|
|
$result = (new Client())->post($this->getApiAccessTokenUrl(), $options); |
314
|
|
|
|
315
|
|
|
if (200 === $result->getStatusCode()) { |
316
|
|
|
return Access::fromJson(json_decode($result->getBody()->getContents())); |
317
|
|
|
} else { |
318
|
|
|
throw new \HttpResponseException('Invalid Response'); |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: