1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LoLApi; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
6
|
|
|
use Doctrine\Common\Cache\VoidCache; |
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use LoLApi\Api\ChampionApi; |
9
|
|
|
use LoLApi\Api\ChampionMasteryApi; |
10
|
|
|
use LoLApi\Api\CurrentGameApi; |
11
|
|
|
use LoLApi\Api\FeaturedGamesApi; |
12
|
|
|
use LoLApi\Api\GameApi; |
13
|
|
|
use LoLApi\Api\LeagueApi; |
14
|
|
|
use LoLApi\Api\MasteryApi; |
15
|
|
|
use LoLApi\Api\MatchApi; |
16
|
|
|
use LoLApi\Api\MatchListApi; |
17
|
|
|
use LoLApi\Api\RuneApi; |
18
|
|
|
use LoLApi\Api\StaticDataApi; |
19
|
|
|
use LoLApi\Api\StatsApi; |
20
|
|
|
use LoLApi\Api\StatusApi; |
21
|
|
|
use LoLApi\Api\SummonerApi; |
22
|
|
|
use LoLApi\Api\TeamApi; |
23
|
|
|
use LoLApi\Exception\InvalidRegionException; |
24
|
|
|
use LoLApi\Result\ApiResult; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class ApiClient |
28
|
|
|
* |
29
|
|
|
* @package LoLApi |
30
|
|
|
*/ |
31
|
|
|
class ApiClient |
32
|
|
|
{ |
33
|
|
|
const REGION_BR = 'br'; |
34
|
|
|
const REGION_EUNE = 'eune'; |
35
|
|
|
const REGION_EUW = 'euw'; |
36
|
|
|
const REGION_JP = 'jp'; |
37
|
|
|
const REGION_KR = 'kr'; |
38
|
|
|
const REGION_LAN = 'lan'; |
39
|
|
|
const REGION_LAS = 'las'; |
40
|
|
|
const REGION_NA = 'na'; |
41
|
|
|
const REGION_OCE = 'oce'; |
42
|
|
|
const REGION_TR = 'tr'; |
43
|
|
|
const REGION_RU = 'ru'; |
44
|
|
|
|
45
|
|
|
const REGION_BR_ID = 'br1'; |
46
|
|
|
const REGION_EUNE_ID = 'eun1'; |
47
|
|
|
const REGION_EUW_ID = 'euw1'; |
48
|
|
|
const REGION_JP_ID = 'jp1'; |
49
|
|
|
const REGION_KR_ID = 'kr'; |
50
|
|
|
const REGION_LAN_ID = 'la1'; |
51
|
|
|
const REGION_LAS_ID = 'la2'; |
52
|
|
|
const REGION_NA_ID = 'na1'; |
53
|
|
|
const REGION_OCE_ID = 'oc1'; |
54
|
|
|
const REGION_TR_ID = 'tr1'; |
55
|
|
|
const REGION_RU_ID = 'ru'; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
public static $availableRegions = [ |
61
|
|
|
self::REGION_BR, |
62
|
|
|
self::REGION_EUNE, |
63
|
|
|
self::REGION_EUW, |
64
|
|
|
self::REGION_KR, |
65
|
|
|
self::REGION_LAN, |
66
|
|
|
self::REGION_LAS, |
67
|
|
|
self::REGION_NA, |
68
|
|
|
self::REGION_OCE, |
69
|
|
|
self::REGION_RU, |
70
|
|
|
self::REGION_TR, |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
public static $regionsWithIds = [ |
74
|
|
|
self::REGION_BR => self::REGION_BR_ID, |
75
|
|
|
self::REGION_EUNE => self::REGION_EUNE_ID, |
76
|
|
|
self::REGION_EUW => self::REGION_EUW_ID, |
77
|
|
|
self::REGION_KR => self::REGION_KR_ID, |
78
|
|
|
self::REGION_LAN => self::REGION_LAN_ID, |
79
|
|
|
self::REGION_LAS => self::REGION_LAS_ID, |
80
|
|
|
self::REGION_NA => self::REGION_NA_ID, |
81
|
|
|
self::REGION_OCE => self::REGION_OCE_ID, |
82
|
|
|
self::REGION_RU => self::REGION_RU_ID, |
83
|
|
|
self::REGION_TR => self::REGION_TR_ID, |
84
|
|
|
]; |
85
|
|
|
|
86
|
3 |
|
/** |
87
|
|
|
* @var string |
88
|
3 |
|
*/ |
89
|
1 |
|
protected $region; |
90
|
|
|
|
91
|
|
|
/** |
92
|
2 |
|
* @var bool |
93
|
2 |
|
*/ |
94
|
2 |
|
protected $endpointStandardization; |
95
|
2 |
|
|
96
|
2 |
|
/** |
97
|
|
|
* @var string |
98
|
|
|
*/ |
99
|
|
|
protected $apiKey; |
100
|
|
|
|
101
|
1 |
|
/** |
102
|
|
|
* @var Client |
103
|
1 |
|
*/ |
104
|
1 |
|
protected $httpClient; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @var CacheProvider |
108
|
|
|
*/ |
109
|
1 |
|
protected $cacheProvider; |
110
|
|
|
|
111
|
1 |
|
/** |
112
|
|
|
* @param string $region |
113
|
|
|
* @param string $apiKey |
114
|
|
|
* @param CacheProvider $cacheProvider |
115
|
|
|
* @param Client $client |
116
|
|
|
* @param bool $endpointStandardization |
117
|
1 |
|
* |
118
|
|
|
* @throws InvalidRegionException |
119
|
1 |
|
*/ |
120
|
|
|
public function __construct($region, $apiKey, CacheProvider $cacheProvider = null, Client $client = null, $endpointStandardization = false) |
121
|
|
|
{ |
122
|
|
|
if (!in_array($region, self::$availableRegions)) { |
123
|
|
|
throw new InvalidRegionException(sprintf('Invalid region %s', $region)); |
124
|
|
|
} |
125
|
1 |
|
|
126
|
|
|
$this->endpointStandardization = $endpointStandardization; |
127
|
1 |
|
$this->region = $region; |
128
|
|
|
$this->httpClient = $client ? $client : new Client(); |
129
|
|
|
$this->apiKey = $apiKey; |
130
|
|
|
$this->cacheProvider = $cacheProvider ? $cacheProvider : new VoidCache(); |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
/** |
134
|
|
|
* @param CacheProvider $cacheProvider |
135
|
1 |
|
*/ |
136
|
|
|
public function setCacheProvider(CacheProvider $cacheProvider) |
137
|
|
|
{ |
138
|
|
|
$this->cacheProvider = $cacheProvider; |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
/** |
142
|
|
|
* @return CacheProvider |
143
|
1 |
|
*/ |
144
|
|
|
public function getCacheProvider() |
145
|
|
|
{ |
146
|
|
|
return $this->cacheProvider; |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
/** |
150
|
|
|
* @return Client |
151
|
1 |
|
*/ |
152
|
|
|
public function getHttpClient() |
153
|
|
|
{ |
154
|
|
|
return $this->httpClient; |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
/** |
158
|
|
|
* @return string |
159
|
1 |
|
*/ |
160
|
|
|
public function getRegion() |
161
|
|
|
{ |
162
|
|
|
return $this->region; |
163
|
|
|
} |
164
|
|
|
|
165
|
1 |
|
/** |
166
|
|
|
* @return string |
167
|
1 |
|
*/ |
168
|
|
|
public function getApiKey() |
169
|
|
|
{ |
170
|
|
|
return $this->apiKey; |
171
|
|
|
} |
172
|
|
|
|
173
|
1 |
|
/** |
174
|
|
|
* @return MatchListApi |
175
|
1 |
|
*/ |
176
|
|
|
public function getMatchListApi() |
177
|
|
|
{ |
178
|
|
|
return new MatchListApi($this); |
179
|
|
|
} |
180
|
|
|
|
181
|
1 |
|
/** |
182
|
|
|
* @return MatchApi |
183
|
1 |
|
*/ |
184
|
|
|
public function getMatchApi() |
185
|
|
|
{ |
186
|
|
|
return new MatchApi($this); |
187
|
|
|
} |
188
|
|
|
|
189
|
1 |
|
/** |
190
|
|
|
* @return SummonerApi |
191
|
1 |
|
*/ |
192
|
|
|
public function getSummonerApi() |
193
|
|
|
{ |
194
|
|
|
return new SummonerApi($this); |
195
|
|
|
} |
196
|
|
|
|
197
|
1 |
|
/** |
198
|
|
|
* @return ChampionApi |
199
|
1 |
|
*/ |
200
|
|
|
public function getChampionApi() |
201
|
|
|
{ |
202
|
|
|
return new ChampionApi($this); |
203
|
|
|
} |
204
|
|
|
|
205
|
1 |
|
/** |
206
|
|
|
* @return FeaturedGamesApi |
207
|
1 |
|
*/ |
208
|
|
|
public function getFeaturedGamesApi() |
209
|
|
|
{ |
210
|
|
|
return new FeaturedGamesApi($this); |
211
|
|
|
} |
212
|
|
|
|
213
|
1 |
|
/** |
214
|
|
|
* @return StatsApi |
215
|
1 |
|
*/ |
216
|
|
|
public function getStatsApi() |
217
|
|
|
{ |
218
|
|
|
return new StatsApi($this); |
219
|
|
|
} |
220
|
|
|
|
221
|
1 |
|
/** |
222
|
|
|
* @return MasteryApi |
223
|
1 |
|
*/ |
224
|
|
|
public function getMasteriesApi() |
225
|
|
|
{ |
226
|
|
|
return new MasteryApi($this); |
227
|
|
|
} |
228
|
|
|
|
229
|
1 |
|
/** |
230
|
|
|
* @return RuneApi |
231
|
1 |
|
*/ |
232
|
|
|
public function getRunesApi() |
233
|
|
|
{ |
234
|
|
|
return new RuneApi($this); |
235
|
|
|
} |
236
|
|
|
|
237
|
1 |
|
/** |
238
|
|
|
* @return GameApi |
239
|
1 |
|
*/ |
240
|
|
|
public function getGameApi() |
241
|
|
|
{ |
242
|
|
|
return new GameApi($this); |
243
|
|
|
} |
244
|
|
|
|
245
|
1 |
|
/** |
246
|
|
|
* @return CurrentGameApi |
247
|
1 |
|
*/ |
248
|
|
|
public function getCurrentGameApi() |
249
|
|
|
{ |
250
|
|
|
return new CurrentGameApi($this); |
251
|
|
|
} |
252
|
|
|
|
253
|
1 |
|
/** |
254
|
|
|
* @return LeagueApi |
255
|
1 |
|
*/ |
256
|
|
|
public function getLeagueApi() |
257
|
|
|
{ |
258
|
|
|
return new LeagueApi($this); |
259
|
|
|
} |
260
|
|
|
|
261
|
1 |
|
/** |
262
|
|
|
* @return StaticDataApi |
263
|
1 |
|
*/ |
264
|
|
|
public function getStaticDataApi() |
265
|
|
|
{ |
266
|
|
|
return new StaticDataApi($this); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
1 |
|
* @return StatusApi |
271
|
|
|
*/ |
272
|
1 |
|
public function getStatusApi() |
273
|
1 |
|
{ |
274
|
|
|
return new StatusApi($this); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @return ChampionMasteryApi |
279
|
|
|
*/ |
280
|
|
|
public function getChampionMasteryApi() |
281
|
|
|
{ |
282
|
|
|
return new ChampionMasteryApi($this); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param bool $endpointStandardization |
287
|
|
|
* |
288
|
|
|
* @return string |
289
|
|
|
*/ |
290
|
|
|
public function getBaseUrl($endpointStandardization = false) |
291
|
|
|
{ |
292
|
|
|
if ($endpointStandardization === true) { |
293
|
|
|
return 'https://' . self::$regionsWithIds[$this->region] . '.api.riotgames.com'; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return 'https://' . $this->region . '.api.pvp.net'; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @return string |
301
|
|
|
*/ |
302
|
|
|
public function getGlobalUrl() |
303
|
|
|
{ |
304
|
|
|
return 'https://global.api.riotgames.com'; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @return string |
309
|
|
|
*/ |
310
|
|
|
public function getStatusUrl() |
311
|
|
|
{ |
312
|
|
|
return 'http://status.leagueoflegends.com'; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @param ApiResult $apiResult |
317
|
|
|
* @param int $ttl |
318
|
|
|
*/ |
319
|
|
|
public function cacheApiResult(ApiResult $apiResult, $ttl = 60) |
320
|
|
|
{ |
321
|
|
|
$this->cacheProvider->save($apiResult->getUrl(), json_encode($apiResult->getResult()), $ttl); |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|