ApiClient::getLeagueApi()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace LoLApi;
4
5
use GuzzleHttp\Client;
6
use LoLApi\Api\ChampionApi;
7
use LoLApi\Api\ChampionMasteryApi;
8
use LoLApi\Api\SpectatorApi;
9
use LoLApi\Api\LeagueApi;
10
use LoLApi\Api\MasteryApi;
11
use LoLApi\Api\MatchApi;
12
use LoLApi\Api\RuneApi;
13
use LoLApi\Api\StaticDataApi;
14
use LoLApi\Api\StatusApi;
15
use LoLApi\Api\SummonerApi;
16
use LoLApi\Exception\InvalidRegionException;
17
use LoLApi\Result\ApiResult;
18
use Symfony\Component\Cache\Adapter\AdapterInterface;
19
use Symfony\Component\Cache\Adapter\ArrayAdapter;
20
use Symfony\Component\Cache\Adapter\NullAdapter;
21
22
/**
23
 * Class ApiClient
24
 *
25
 * @package LoLApi
26
 */
27
class ApiClient
28
{
29
    const REGION_BR   = 'br';
30
    const REGION_EUNE = 'eune';
31
    const REGION_EUW  = 'euw';
32
    const REGION_JP   = 'jp';
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_TR   = 'tr';
39
    const REGION_RU   = 'ru';
40
41
    const REGION_BR_ID   = 'br1';
42
    const REGION_EUNE_ID = 'eun1';
43
    const REGION_EUW_ID  = 'euw1';
44
    const REGION_JP_ID   = 'jp1';
45
    const REGION_KR_ID   = 'kr';
46
    const REGION_LAN_ID  = 'la1';
47
    const REGION_LAS_ID  = 'la2';
48
    const REGION_NA_ID   = 'na1';
49
    const REGION_OCE_ID  = 'oc1';
50
    const REGION_TR_ID   = 'tr1';
51
    const REGION_RU_ID   = 'ru';
52
53
    /**
54
     * @var array
55
     */
56
    public static $availableRegions = [
57
        self::REGION_BR,
58
        self::REGION_EUNE,
59
        self::REGION_EUW,
60
        self::REGION_JP,
61
        self::REGION_KR,
62
        self::REGION_LAN,
63
        self::REGION_LAS,
64
        self::REGION_NA,
65
        self::REGION_OCE,
66
        self::REGION_RU,
67
        self::REGION_TR,
68
    ];
69
70
    public static $regionsWithIds = [
71
        self::REGION_BR   => self::REGION_BR_ID,
72
        self::REGION_EUNE => self::REGION_EUNE_ID,
73
        self::REGION_EUW  => self::REGION_EUW_ID,
74
        self::REGION_JP   => self::REGION_JP_ID,
75
        self::REGION_KR   => self::REGION_KR_ID,
76
        self::REGION_LAN  => self::REGION_LAN_ID,
77
        self::REGION_LAS  => self::REGION_LAS_ID,
78
        self::REGION_NA   => self::REGION_NA_ID,
79
        self::REGION_OCE  => self::REGION_OCE_ID,
80
        self::REGION_RU   => self::REGION_RU_ID,
81
        self::REGION_TR   => self::REGION_TR_ID,
82
    ];
83
84
    /**
85
     * @var string
86 3
     */
87
    protected $region;
88 3
89 1
    /**
90
     * @var string
91
     */
92 2
    protected $apiKey;
93 2
94 2
    /**
95 2
     * @var Client
96 2
     */
97
    protected $httpClient;
98
99
    /**
100
     * @var AdapterInterface
101 1
     */
102
    protected $cacheProvider;
103 1
104 1
    /**
105
     * @param string          $region
106
     * @param string          $apiKey
107
     * @param AdapterInterface $cache
108
     * @param Client          $client
109 1
     *
110
     * @throws InvalidRegionException
111 1
     */
112
    public function __construct($region, $apiKey, AdapterInterface $cache = null, Client $client = null)
113
    {
114
        if (!in_array($region, self::$availableRegions)) {
115
            throw new InvalidRegionException(sprintf('Invalid region %s', $region));
116
        }
117 1
118
        $this->region     = $region;
119 1
        $this->httpClient = $client ? $client : new Client();
120
        $this->apiKey     = $apiKey;
121
        $this->cache      = $cache ? $cache : new NullAdapter();
0 ignored issues
show
Bug introduced by
The property cache does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
122
    }
123
124
    /**
125 1
     * @param AdapterInterface $cache
126
     */
127 1
    public function setCacheProvider(AdapterInterface $cache)
128
    {
129
        $this->cache = $cache;
130
    }
131
132
    /**
133 1
     * @return AdapterInterface
134
     */
135 1
    public function getCacheProvider()
136
    {
137
        return $this->cache;
138
    }
139
140
    /**
141 1
     * @return Client
142
     */
143 1
    public function getHttpClient()
144
    {
145
        return $this->httpClient;
146
    }
147
148
    /**
149 1
     * @return string
150
     */
151 1
    public function getRegion()
152
    {
153
        return $this->region;
154
    }
155
156
    /**
157 1
     * @return string
158
     */
159 1
    public function getApiKey()
160
    {
161
        return $this->apiKey;
162
    }
163
164
    /**
165 1
     * @return MatchApi
166
     */
167 1
    public function getMatchApi()
168
    {
169
        return new MatchApi($this);
170
    }
171
172
    /**
173 1
     * @return SummonerApi
174
     */
175 1
    public function getSummonerApi()
176
    {
177
        return new SummonerApi($this);
178
    }
179
180
    /**
181 1
     * @return ChampionApi
182
     */
183 1
    public function getChampionApi()
184
    {
185
        return new ChampionApi($this);
186
    }
187
188
    /**
189 1
     * @return SpectatorApi
190
     */
191 1
    public function getFeaturedGamesApi()
192
    {
193
        return new SpectatorApi($this);
194
    }
195
196
    /**
197 1
     * @return MasteryApi
198
     */
199 1
    public function getMasteriesApi()
200
    {
201
        return new MasteryApi($this);
202
    }
203
204
    /**
205 1
     * @return RuneApi
206
     */
207 1
    public function getRunesApi()
208
    {
209
        return new RuneApi($this);
210
    }
211
212
    /**
213 1
     * @return SpectatorApi
214
     */
215 1
    public function getSpectatorApi()
216
    {
217
        return new SpectatorApi($this);
218
    }
219
220
    /**
221 1
     * @return LeagueApi
222
     */
223 1
    public function getLeagueApi()
224
    {
225
        return new LeagueApi($this);
226
    }
227
228
    /**
229 1
     * @return StaticDataApi
230
     */
231 1
    public function getStaticDataApi()
232
    {
233
        return new StaticDataApi($this);
234
    }
235
236
    /**
237 1
     * @return StatusApi
238
     */
239 1
    public function getStatusApi()
240
    {
241
        return new StatusApi($this);
242
    }
243
244
    /**
245 1
     * @return ChampionMasteryApi
246
     */
247 1
    public function getChampionMasteryApi()
248
    {
249
        return new ChampionMasteryApi($this);
250
    }
251
252
    /**
253 1
     * @return string
254
     */
255 1
    public function getBaseUrl()
256
    {
257
        return 'https://'.self::$regionsWithIds[$this->region].'.api.riotgames.com';
258
    }
259
260
    /**
261 1
     * @param ApiResult $apiResult
262
     * @param int       $ttl
263 1
     */
264
    public function cacheApiResult(ApiResult $apiResult, $ttl = 60)
265
    {
266
        $cacheKey = md5($apiResult->getUrl());
267
268
        $item = $this->cache->getItem($cacheKey);
269
270 1
        if (!$item->isHit()) {
271
           $item->set(json_encode($apiResult->getResult()));
272 1
           $item->expiresAfter($ttl);
273 1
           $this->cache->save($item);
274
        }
275
    }
276
}
277