Completed
Pull Request — master (#498)
by Dragonqos
02:30
created

BattleNet   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 4
dl 0
loc 99
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 2
B GetOAuthBaseUri() 0 13 7
A getAuthorizationEndpoint() 0 3 1
A getAccessTokenEndpoint() 0 3 1
A getAuthorizationMethod() 0 4 1
A parseAccessTokenResponse() 0 21 4
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\OAuth2\Token\StdOAuth2Token;
6
use OAuth\Common\Http\Exception\TokenResponseException;
7
use OAuth\Common\Http\Uri\Uri;
8
9
/**
10
 * Class BattleNet
11
 * @package OAuth\OAuth2\Service
12
 */
13
class BattleNet extends AbstractService { 
14
    
15
    /** 
16
     * Defined scopes.
17
     *
18
     * @link https://dev.battle.net/docs
19
     */
20
    const SCOPE_WOW_PROFILE = "wow.profile";
21
    const SCOPE_SC2_PROFILE = "sc2.profile";
22
    
23
    /** 
24
     * Defined API URIs.
25
     *
26
     * @link https://dev.battle.net/docs
27
     */
28
    const API_URI_US  = 'https://us.api.battle.net/';
29
    const API_URI_EU  = 'https://eu.api.battle.net/';
30
    const API_URI_KR  = 'https://kr.api.battle.net/';
31
    const API_URI_TW  = 'https://tw.api.battle.net/';
32
    const API_URI_CN  = 'https://api.battlenet.com.cn/';
33
    const API_URI_SEA = 'https://sea.api.battle.net/';
34
    
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected function init()
39
    {
40
        if( $this->baseApiUri === null ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
41
            $this->baseApiUri = new Uri( self::API_URI_US );
42
        }
43
    }
44
45
    
46
    /** 
47
     * Translates the current base API URI into an OAuth base URI. 
48
     *
49
     * @returns string Base URI of oauth services.
50
     */
51
    private function GetOAuthBaseUri() {
0 ignored issues
show
Coding Style introduced by
Method name "BattleNet::GetOAuthBaseUri" is not in camel caps format
Loading history...
52
    
53
        // i love china
54
        switch( $this->baseApiUri ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after SWITCH keyword; 0 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
55
            case self::API_URI_US:  return 'https://us.battle.net/oauth/';
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
56
            case self::API_URI_EU:  return 'https://eu.battle.net/oauth/';
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
57
            case self::API_URI_KR:  return 'https://kr.battle.net/oauth/';
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
58
            case self::API_URI_TW:  return 'https://tw.battle.net/oauth/';
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
59
            case self::API_URI_CN:  return 'https://www.battlenet.com.cn/oauth/';
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
60
            case self::API_URI_SEA: return 'https://sea.battle.net/oauth/'; 
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
61
        }
62
        
63
    }
64
    
65
    /** 
66
     * {@inheritdoc}
67
     */
68
    public function getAuthorizationEndpoint() {
69
        return new Uri( $this->GetOAuthBaseUri() . 'authorize' );
70
    }
71
    
72
    /** 
73
     * {@inheritdoc}
74
     */
75
    public function getAccessTokenEndpoint() {
76
        return new Uri( $this->GetOAuthBaseUri() . 'token' );
77
    }
78
    
79
    /** 
80
     * {@inheritdoc}
81
     */
82
    protected function getAuthorizationMethod()
83
    {
84
        return static::AUTHORIZATION_METHOD_QUERY_STRING;
85
    }
86
    
87
    /** 
88
     * {@inheritdoc}
89
     */
90
    protected function parseAccessTokenResponse( $responseBody )
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between opening bracket and argument "$responseBody"; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces between argument "$responseBody" and closing bracket; 1 found
Loading history...
91
    {
92
        $data = json_decode($responseBody, true);
93
        if( $data === null || !is_array($data) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
94
            throw new TokenResponseException( 'Unable to parse response.' );
95
        } elseif( isset($data['error']) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ELSEIF keyword; 0 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
96
            $err = $data['error'];
97
            throw new TokenResponseException( 
98
                                "Error in retrieving token: \"$err\"" );
99
        }
100
        
101
        $token = new StdOAuth2Token( $data['access_token'], null, 
102
                                     $data['expires_in'] );
103
        
104
        unset( $data['access_token'] );
105
        unset( $data['expires_in'] );
106
        
107
        $token->setExtraParams( $data );
108
        
109
        return $token;
110
    }
111
}
112