GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 9d7c7b...fab8f4 )
by Oleg
7s
created

BlizzardClient::requestAccessToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 0
1
<?php
2
3
namespace BlizzardApi;
4
5
use GuzzleHttp\Client;
6
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
9
/**
10
 * Class Blizzard Client
11
 *
12
 * @author Oleg Kachinsky <[email protected]>
13
 */
14
class BlizzardClient
15
{
16
    const API_URL_PATTERN = 'https://region.api.battle.net';
17
    const API_ACCESS_TOKEN_URL_PATTERN = 'https://region.battle.net/oauth/token';
18
19
    /**
20
     * @var string $apiUrl Blizzard API url
21
     */
22
    private $apiUrl;
23
24
    /**
25
     * @var string $apiAccessTokenUrl Blizzard API Access Token url
26
     */
27
    private $apiAccessTokenUrl;
28
29
    /**
30
     * @var string $apiKey API key
31
     */
32
    private $apiKey;
33
34
    /**
35
     * @var \BlizzardApi\Tokens\Access[] $accessTokens Access tokens
36
     */
37
    private $accessTokens;
38
39
    /**
40
     * @var string $locale Locale
41
     */
42
    private $locale;
43
44
    /**
45
     * @var string $region Region
46
     */
47
    private $region;
48
49
    /**
50
     * BlizzardClient constructor
51
     *
52
     * @param string      $apiKey      API key
53
     * @param string      $apiSecret   API Secret key
54
     * @param string      $region      Region
55
     * @param string      $locale      Locale
56
     */
57
    public function __construct($apiKey, $apiSecret, $region = 'us', $locale = 'en_us')
58
    {
59
        $options = [
60
            'apiKey'      => $apiKey,
61
            'apiSecret'   => $apiSecret,
62
            'region'      => strtolower($region),
63
            'locale'      => strtolower($locale)
64
        ];
65
66
        $resolver = (new OptionsResolver());
67
        $this->configureOptions($resolver, $options['region']);
68
69
        $options = $resolver->resolve($options);
70
71
        $this->apiKey      = $options['apiKey'];
72
        $this->apiSecret   = $options['apiSecret'];
0 ignored issues
show
Bug introduced by
The property apiSecret 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...
73
        $this->region      = $options['region'];
74
        $this->locale      = $options['locale'];
75
76
        $this->updateApiUrl($options['region']);
77
        $this->updateApiAccessTokenUrl($options['region']);
78
    }
79
80
    /**
81
     * Get api url
82
     *
83
     * @return string Api url
84
     */
85
    public function getApiUrl()
86
    {
87
        return $this->apiUrl;
88
    }
89
90
    /**
91
     * Get api Access Token url
92
     *
93
     * @return string Api Access Token url
94
     */
95
    public function getApiAccessTokenUrl()
96
    {
97
        return $this->apiAccessTokenUrl;
98
    }
99
100
    /**
101
     * Get api key
102
     *
103
     * @return string Api key
104
     */
105
    public function getApiKey()
106
    {
107
        return $this->apiKey;
108
    }
109
110
    /**
111
     * Set api key
112
     *
113
     * @param string $apiKey Api key
114
     *
115
     * @return $this
116
     */
117
    public function setApiKey($apiKey)
118
    {
119
        $this->apiKey = $apiKey;
120
121
        return $this;
122
    }
123
124
    /**
125
     * Get api secret
126
     *
127
     * @return string Api secret
128
     */
129
    public function getApiSecret()
130
    {
131
        return $this->apiSecret;
132
    }
133
134
    /**
135
     * Set api secret
136
     *
137
     * @param string $apiSecret Api secret
138
     *
139
     * @return $this
140
     */
141
    public function setApiSecret($apiSecret)
142
    {
143
        $this->apiSecret = $apiSecret;
144
145
        return $this;
146
    }
147
148
    /**
149
     * Get region
150
     *
151
     * @return string Region
152
     */
153
    public function getRegion()
154
    {
155
        return strtolower($this->region);
156
    }
157
158
    /**
159
     * Set region
160
     *
161
     * @param string $region region
162
     *
163
     * @return $this
164
     */
165
    public function setRegion($region)
166
    {
167
        $this->region = strtolower($region);
168
169
        $this->updateApiUrl($region);
170
        $this->updateApiAccessTokenUrl($region);
171
172
        return $this;
173
    }
174
175
    /**
176
     * Get locale
177
     *
178
     * @return string Locale
179
     */
180
    public function getLocale()
181
    {
182
        return strtolower($this->locale);
183
    }
184
185
    /**
186
     * Set locale
187
     *
188
     * @param string $locale locale
189
     *
190
     * @return $this
191
     */
192
    public function setLocale($locale)
193
    {
194
        $this->locale = strtolower($locale);
195
196
        return $this;
197
    }
198
199
    /**
200
     * Get access token
201
     *
202
     * @return null|string Access token
203
     */
204
    public function getAccessToken()
205
    {
206
        if ($this->accessTokens == null) {
207
            $this->accessTokens = [];
208
        }
209
210
        if (!array_key_exists($this->getRegion(), $this->accessTokens)) {
211
            $this->accessTokens[$this->getRegion()] = $this->requestAccessToken();
212
        }
213
214
        return $this->accessTokens[$this->getRegion()]->getToken();
215
    }
216
217
    /**
218
     * Set access token
219
     *
220
     * @param null|Tokens\Access $accessToken Access token
221
     *
222
     * @return $this
223
     */
224
    public function setAccessToken(Tokens\Access $accessToken)
225
    {
226
        $this->accessTokens[$this->getRegion()] = $accessToken;
227
228
        return $this;
229
    }
230
231
    /**
232
     * Update API url
233
     *
234
     * Update API url by replacing region in API url pattern
235
     *
236
     * @param string $region Region
237
     *
238
     * @return $this
239
     */
240
    private function updateApiUrl($region)
241
    {
242
        $this->apiUrl = str_replace('region', strtolower($region), self::API_URL_PATTERN);
243
    }
244
245
    /**
246
     * Update API Access Token url
247
     *
248
     * Update API Access Token url by replacing region in API url pattern
249
     *
250
     * @param string $region Region
251
     *
252
     * @return $this
253
     */
254
    private function updateApiAccessTokenUrl($region)
255
    {
256
        $this->apiAccessTokenUrl = str_replace('region', strtolower($region), self::API_ACCESS_TOKEN_URL_PATTERN);
257
    }
258
259
    /**
260
     * Configure options
261
     *
262
     * @param OptionsResolver $resolver Symfony options resolver
263
     * @param string          $region   Region
264
     *
265
     * @throws InvalidOptionsException
266
     */
267
    private function configureOptions(OptionsResolver $resolver, $region)
268
    {
269
        if (isset(GeoData::$list[$region])) {
270
            $locales = GeoData::$list[$region];
271
        } else {
272
            throw new InvalidOptionsException(
273
                sprintf(
274
                    'The option "region" with value "%s" is invalid. Accepted values are: "%s".',
275
                    $region,
276
                    implode('", "', array_keys(GeoData::$list))
277
                )
278
            );
279
        }
280
281
        $resolver->setRequired(['apiKey', 'apiSecret', 'region', 'locale'])
282
                 ->setAllowedTypes('apiKey', 'string')
283
                 ->setAllowedTypes('apiSecret', 'string')
284
                 ->setAllowedTypes('region', 'string')
285
                 ->setAllowedValues('region', array_keys(GeoData::$list))
286
                 ->setAllowedTypes('locale', 'string')
287
                 ->setAllowedValues('locale', $locales);
288
    }
289
290
    /**
291
     * Request an Access Token from Blizzard
292
     * @return Tokens\Access
293
     * @throws \HttpResponseException
294
     */
295
    protected function requestAccessToken()
296
    {
297
        $client = new Client();
298
299
        $options = [
300
            'form_params' => [
301
                'grant_type' => 'client_credentials',
302
                'client_id' => $this->getApiKey(),
303
                'client_secret' => $this->getApiSecret(),
304
            ]
305
        ];
306
307
        $result = $client->post($this->getApiAccessTokenUrl(), $options);
308
309
        if ($result->getStatusCode() == 200) {
310
            return Tokens\Access::fromJson(json_decode($result->getBody()->getContents()));
311
        } else {
312
            throw new \HttpResponseException("Invalid Response");
313
        }
314
    }
315
316
}
317