|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kubinashi\BattlenetApi\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Kubinashi\BattlenetApi\External\HttpClient; |
|
6
|
|
|
use Kubinashi\BattlenetApi\Model\RequestModel; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @author Willy Reiche |
|
10
|
|
|
* @since 2017-07-20 |
|
11
|
|
|
* @version 1.0 |
|
12
|
|
|
*/ |
|
13
|
|
|
class RequestService |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param RequestModel $requestModel |
|
17
|
|
|
* @return string |
|
18
|
|
|
*/ |
|
19
|
10 |
|
public function doRequest(RequestModel $requestModel) |
|
20
|
|
|
{ |
|
21
|
10 |
|
$clientId = $requestModel->getClientId(); |
|
22
|
10 |
|
$clientSecret = $requestModel->getClientSecret(); |
|
23
|
10 |
|
$locale = $requestModel->getLocale(); |
|
24
|
10 |
|
$fields = implode('/', $requestModel->getFields()); |
|
25
|
10 |
|
$api = $requestModel->getApi(); |
|
26
|
10 |
|
$franchise = $requestModel->getFranchise(); |
|
27
|
10 |
|
$region = $requestModel->getRegion(); |
|
28
|
|
|
$addition = $requestModel->getAddition(); |
|
29
|
10 |
|
|
|
30
|
10 |
|
$accessToken = $this->getAccessToken($clientId, $clientSecret, $region); |
|
|
|
|
|
|
31
|
8 |
|
|
|
32
|
8 |
|
$url = 'https://'.$region.'.api.battle.net/'.$franchise.'/'.$api.'/'.$fields.'?locale='.$locale.'&apikey='.$clientId; |
|
33
|
|
|
if (!empty($addition)) { |
|
34
|
10 |
|
$url = 'https://'.$region.'.api.battle.net/'.$franchise.'/'.$api.'/'.$fields.'?fields='.$addition.'&locale='.$locale.'&apikey='.$clientId; |
|
35
|
10 |
|
} |
|
36
|
10 |
|
|
|
37
|
|
|
$client = new HttpClient(); |
|
38
|
|
|
$response = $client->request('get', $url); |
|
39
|
|
|
return (string)$response->getBody(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function getAccessToken(string $clientId, string $clientSecret, string $region): string |
|
43
|
|
|
{ |
|
44
|
|
|
$tokenUrl = sprintf( |
|
45
|
|
|
'https://%s.battle.net/oauth/token', |
|
46
|
|
|
$region |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
$client = new HttpClient(); |
|
50
|
|
|
$result = $client->post( |
|
51
|
|
|
$tokenUrl, |
|
52
|
|
|
[ |
|
53
|
|
|
'auth' => [$clientId, $clientSecret], |
|
54
|
|
|
'query' => ['grant_type' => 'client_credentials'], |
|
55
|
|
|
'verify' => false, |
|
56
|
|
|
'http_errors' => false |
|
57
|
|
|
] |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
return json_decode((string)$result->getBody())->access_token; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.