Completed
Push — master ( 10c692...bd5cf0 )
by Michaël
23s
created

ApiClient::getChampionApi()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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\MatchApi;
15
use LoLApi\Api\MatchListApi;
16
use LoLApi\Api\StaticDataApi;
17
use LoLApi\Api\StatsApi;
18
use LoLApi\Api\StatusApi;
19
use LoLApi\Api\SummonerApi;
20
use LoLApi\Api\TeamApi;
21
use LoLApi\Exception\InvalidRegionException;
22
use LoLApi\Result\ApiResult;
23
24
/**
25
 * Class ApiClient
26
 *
27
 * @package LoLApi
28
 */
29
class ApiClient
30
{
31
    const REGION_BR = 'br';
32
    const REGION_EUNE = 'eune';
33
    const REGION_EUW = 'euw';
34
    const REGION_KR = 'kr';
35
    const REGION_LAN = 'lan';
36
    const REGION_LAS = 'las';
37
    const REGION_NA = 'na';
38
    const REGION_OCE = 'oce';
39
    const REGION_RU = 'ru';
40
    const REGION_TR = 'tr';
41
42
    /**
43
     * @var array
44
     */
45
    public static $availableRegions = [
46
        self::REGION_BR,
47
        self::REGION_EUNE,
48
        self::REGION_EUW,
49
        self::REGION_KR,
50
        self::REGION_LAN,
51
        self::REGION_LAS,
52
        self::REGION_NA,
53
        self::REGION_OCE,
54
        self::REGION_RU,
55
        self::REGION_TR
56
    ];
57
58
    /**
59
     * @var string
60
     */
61
    protected $region;
62
63
    /**
64
     * @var string
65
     */
66
    protected $apiKey;
67
68
    /**
69
     * @var Client
70
     */
71
    protected $httpClient;
72
73
    /**
74
     * @var CacheProvider
75
     */
76
    protected $cacheProvider;
77
78
    /**
79
     * @param string        $region
80
     * @param string        $apiKey
81
     * @param CacheProvider $cacheProvider
82
     * @param Client        $client
83
     *
84
     * @throws InvalidRegionException
85
     */
86 3
    public function __construct($region, $apiKey, CacheProvider $cacheProvider = null, Client $client = null)
87
    {
88 3
        if (!in_array($region, self::$availableRegions)) {
89 1
            throw new InvalidRegionException(sprintf('Invalid region %s', $region));
90
        }
91
92 2
        $this->region        = $region;
93 2
        $this->httpClient    = $client ? $client : new Client(['base_uri' => $this->getBaseUrlWithRegion()]);
94 2
        $this->apiKey        = $apiKey;
95 2
        $this->cacheProvider = $cacheProvider ? $cacheProvider : new VoidCache();
96 2
    }
97
98
    /**
99
     * @param CacheProvider $cacheProvider
100
     */
101 1
    public function setCacheProvider(CacheProvider $cacheProvider)
102
    {
103 1
        $this->cacheProvider = $cacheProvider;
104 1
    }
105
106
    /**
107
     * @return CacheProvider
108
     */
109 1
    public function getCacheProvider()
110
    {
111 1
        return $this->cacheProvider;
112
    }
113
114
    /**
115
     * @return Client
116
     */
117 1
    public function getHttpClient()
118
    {
119 1
        return $this->httpClient;
120
    }
121
122
    /**
123
     * @return string
124
     */
125 1
    public function getRegion()
126
    {
127 1
        return $this->region;
128
    }
129
130
    /**
131
     * @return string
132
     */
133 1
    public function getApiKey()
134
    {
135 1
        return $this->apiKey;
136
    }
137
138
    /**
139
     * @return MatchListApi
140
     */
141 1
    public function getMatchListApi()
142
    {
143 1
        return new MatchListApi($this);
144
    }
145
146
    /**
147
     * @return MatchApi
148
     */
149 1
    public function getMatchApi()
150
    {
151 1
        return new MatchApi($this);
152
    }
153
154
    /**
155
     * @return SummonerApi
156
     */
157 1
    public function getSummonerApi()
158
    {
159 1
        return new SummonerApi($this);
160
    }
161
162
    /**
163
     * @return ChampionApi
164
     */
165 1
    public function getChampionApi()
166
    {
167 1
        return new ChampionApi($this);
168
    }
169
170
    /**
171
     * @return FeaturedGamesApi
172
     */
173 1
    public function getFeaturedGamesApi()
174
    {
175 1
        return new FeaturedGamesApi($this);
176
    }
177
178
    /**
179
     * @return StatsApi
180
     */
181 1
    public function getStatsApi()
182
    {
183 1
        return new StatsApi($this);
184
    }
185
186
    /**
187
     * @return TeamApi
188
     */
189 1
    public function getTeamApi()
190
    {
191 1
        return new TeamApi($this);
192
    }
193
194
    /**
195
     * @return GameApi
196
     */
197 1
    public function getGameApi()
198
    {
199 1
        return new GameApi($this);
200
    }
201
202
    /**
203
     * @return CurrentGameApi
204
     */
205 1
    public function getCurrentGameApi()
206
    {
207 1
        return new CurrentGameApi($this);
208
    }
209
210
    /**
211
     * @return LeagueApi
212
     */
213 1
    public function getLeagueApi()
214
    {
215 1
        return new LeagueApi($this);
216
    }
217
218
    /**
219
     * @return StaticDataApi
220
     */
221 1
    public function getStaticDataApi()
222
    {
223 1
        return new StaticDataApi($this);
224
    }
225
226
    /**
227
     * @return StatusApi
228
     */
229 1
    public function getStatusApi()
230
    {
231 1
        return new StatusApi($this);
232
    }
233
234
    /**
235
     * @return ChampionMasteryApi
236
     */
237 1
    public function getChampionMasteryApi()
238
    {
239 1
        return new ChampionMasteryApi($this);
240
    }
241
242
    /**
243
     * @return string
244
     */
245 1
    public function getBaseUrlWithRegion()
246
    {
247 1
        return 'https://' . $this->region . '.api.pvp.net';
248
    }
249
250
    /**
251
     * @return string
252
     */
253 1
    public function getGlobalUrl()
254
    {
255 1
        return 'https://global.api.pvp.net';
256
    }
257
258
    /**
259
     * @return string
260
     */
261 1
    public function getStatusUrl()
262
    {
263 1
        return 'http://status.leagueoflegends.com';
264
    }
265
266
    /**
267
     * @param ApiResult $apiResult
268
     * @param int       $ttl
269
     */
270 1
    public function cacheApiResult(ApiResult $apiResult, $ttl = 60)
271
    {
272 1
        $this->cacheProvider->save($apiResult->getUrl(), json_encode($apiResult->getResult()), $ttl);
273 1
    }
274
}
275