-
Notifications
You must be signed in to change notification settings - Fork 28
/
Api.php
225 lines (202 loc) · 4.87 KB
/
Api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
namespace LeagueWrap;
use LeagueWrap\Api\AbstractApi;
use LeagueWrap\Api\Staticdata;
use LeagueWrap\Limit\Limit;
use LeagueWrap\Limit\Collection;
use LeagueWrap\Limit\FileLimit;
use LeagueWrap\Exception\NoKeyException;
use LeagueWrap\Exception\ApiClassNotFoundException;
use LeagueWrap\Exception\NoValidLimitInterfaceException;
/**
* @method Api\Champion champion()
* @method Api\Game game()
* @method Api\League league()
* @method Api\Staticdata staticData()
* @method Api\Stats stats()
* @method Api\Summoner summoner()
* @method Api\Team team()
* @method Api\CurrentGame currentGame()
*/
class Api {
use Api\ConfigTrait;
/**
* The client used to connect with the riot API.
*
* @var ClientInterface
*/
protected $client;
/**
* This contains the cache container that we intend to use.
*
* @var CacheInterface
*/
protected $cache;
/**
* How long, in seconds, should we remember a query's response.
*
* @var int
*/
protected $remember = null;
/**
* The collection of limits to be used for all requests in this api.
*
* @var Collection
*/
protected $limits = null;
/**
* This is the api key, very important.
*
* @var string
*/
private $key;
/**
* Initiat the default client and key.
*
* @param string $key
* @param ClientInterface $client
* @throws NoKeyException
*/
public function __construct($key = null, ClientInterface $client = null)
{
if (is_null($key))
{
throw new NoKeyException('We need a key... it\'s very important!');
}
if (is_null($client))
{
// set up the default client
$client = new Client;
}
$this->client = $client;
// add the key
$this->key = $key;
// set up the limit collection
$this->collection = new Collection;
}
/**
* This is the primary service locater if you utilize the api (which you should) to
* load instance of the abstractApi. This is the method that is called when you attempt
* to invoke "Champion()", "League()", etc.
*
* @param string $method
* @param array $arguments
* @return AbstractApi
* @throws ApiClassNotFoundException
*/
public function __call($method, $arguments)
{
// we don't use the arguments at the moment.
unset($arguments);
$className = 'LeagueWrap\\Api\\'.ucwords(strtolower($method));
if ( ! class_exists($className))
{
// This class does not exist
throw new ApiClassNotFoundException('The api class "'.$className.'" was not found.');
}
$api = new $className($this->client, $this->collection, $this);
if ( ! $api instanceof AbstractApi)
{
// This is not an api class.
throw new ApiClassNotFoundException('The api class "'.$className.'" was not found.');
}
$api->setKey($this->key)
->setRegion($this->region)
->setTimeout($this->timeout)
->setCacheOnly($this->cacheOnly)
->setClientErrorCaching($this->cacheClientError)
->setServerErrorCaching($this->cacheServerError);
if ($this->attachStaticData &&
! ($api instanceof Staticdata)
)
{
$api->attachStaticData($this->attachStaticData, $this->staticData());
}
if ($this->cache instanceof CacheInterface)
{
$api->remember($this->remember, $this->cache);
}
return $api;
}
/**
* Sets the amount of seconds we should remember the response for.
* Leave it empty (or null) if you want to use the default set for
* each api request.
*
* @param int $seconds
* @param CacheInterface $cache
* @return $this
*/
public function remember($seconds = null, CacheInterface $cache = null)
{
if (is_null($cache))
{
// use the built in cache interface
$cache = new Cache;
}
$this->cache = $cache;
$this->remember = $seconds;
return $this;
}
/**
* Sets a limit to be added to the collection.
*
* @param int $hits
* @param int $seconds
* @param string $region
* @param LimitInterface $limit
* @return $this
* @throws NoValidLimitInterfaceException
*/
public function limit($hits, $seconds, $region = 'all', LimitInterface $limit = null)
{
if (is_null($limit))
{
// use the built in limit interface
$limit = new Limit;
}
if ( ! $limit->isValid())
{
// fall back to the file base limit handling
$limit = new FileLimit;
if ( ! $limit->isValid())
{
throw new NoValidLimitInterfaceException("We could not load a valid limit interface.");
}
}
if ($region == 'all')
{
foreach ([
'br',
'eune',
'euw',
'kr',
'lan',
'las',
'na',
'oce',
'ru',
'tr'] as $region)
{
$newLimit = $limit->newInstance();
$newLimit->setRate($hits, $seconds, $region);
$this->collection->addLimit($newLimit);
}
}
else
{
// lower case the region
$region = strtolower($region);
$limit->setRate($hits, $seconds, $region);
$this->collection->addLimit($limit);
}
return $this;
}
/**
* @return array of Limit
*/
public function getLimits()
{
return $this->collection->getLimits();
}
}