Completed
Pull Request — master (#7)
by Michaël
06:02 queued 02:38
created

ApiClient   B

Complexity

Total Complexity 26

Size/Duplication

Total Lines 246
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 17

Test Coverage

Coverage 96.3%

Importance

Changes 9
Bugs 0 Features 2
Metric Value
wmc 26
c 9
b 0
f 2
lcom 2
cbo 17
dl 0
loc 246
ccs 52
cts 54
cp 0.963
rs 7.8571

23 Methods

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