Test Setup Failed
Push — master ( 76bea9...67ec1c )
by Willy
05:10
created

RequestService::getAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 0
cp 0
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
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);
0 ignored issues
show
Unused Code introduced by
$accessToken is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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