Completed
Push — master ( c69bbe...09f4af )
by David
16:02 queued 13:17
created

BattleNet   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 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
//-----------------------------------------------------------------------------
6
use OAuth\OAuth2\Token\StdOAuth2Token;
7
use OAuth\Common\Http\Exception\TokenResponseException;
8
use OAuth\Common\Http\Uri\Uri;
9
use OAuth\Common\Consumer\CredentialsInterface;
10
use OAuth\Common\Http\Client\ClientInterface;
11
use OAuth\Common\Storage\TokenStorageInterface;
12
use OAuth\Common\Http\Uri\UriInterface;
13
14
//-----------------------------------------------------------------------------
15
class BattleNet extends AbstractService { 
16
    
17
    /** -----------------------------------------------------------------------
18
     * Defined scopes.
19
     *
20
     * @link https://dev.battle.net/docs
21
     */
22
    const SCOPE_WOW_PROFILE = "wow.profile";
23
    const SCOPE_SC2_PROFILE = "sc2.profile";
24
    
25
    /** -----------------------------------------------------------------------
26
     * Defined API URIs.
27
     *
28
     * @link https://dev.battle.net/docs
29
     */
30
    const API_URI_US  = 'https://us.api.battle.net/';
31
    const API_URI_EU  = 'https://eu.api.battle.net/';
32
    const API_URI_KR  = 'https://kr.api.battle.net/';
33
    const API_URI_TW  = 'https://tw.api.battle.net/';
34
    const API_URI_CN  = 'https://api.battlenet.com.cn/';
35
    const API_URI_SEA = 'https://sea.api.battle.net/';
36
    
37
    public function __construct( CredentialsInterface $credentials,
0 ignored issues
show
Coding Style introduced by
The first parameter of a multi-line function declaration must be on the line after the opening bracket
Loading history...
38
                                 ClientInterface $httpClient,
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 33
Loading history...
39
                                 TokenStorageInterface $storage,
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 33
Loading history...
40
                                 $scopes = array(),
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 33
Loading history...
41
                                 UriInterface $baseApiUri = null ) {
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 33
Loading history...
Coding Style introduced by
The closing parenthesis of a multi-line function declaration must be on a new line
Loading history...
42
                                 
43
        parent::__construct( $credentials, $httpClient, $storage, 
44
                             $scopes, $baseApiUri );
45
        
46
        if( $baseApiUri === null ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
47
            $this->baseApiUri = new Uri( self::API_URI_US );
48
        }
49
    }
50
    
51
    /** -----------------------------------------------------------------------
52
     * Translates the current base API URI into an OAuth base URI. 
53
     *
54
     * @returns string Base URI of oauth services.
55
     */
56
    private function GetOAuthBaseUri() {
0 ignored issues
show
Coding Style introduced by
Method name "BattleNet::GetOAuthBaseUri" is not in camel caps format
Loading history...
57
    
58
        // i love china
59
        switch( $this->baseApiUri ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
Coding Style introduced by
Expected 1 space after SWITCH keyword; 0 found
Loading history...
60
            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...
61
            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...
62
            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...
63
            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...
64
            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...
65
            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...
66
        }
67
        
68
    }
69
    
70
    /** -----------------------------------------------------------------------
71
     * {@inheritdoc}
72
     */
73
    public function getAuthorizationEndpoint() {
74
        return new Uri( $this->GetOAuthBaseUri() . 'authorize' );
75
    }
76
    
77
    /** -----------------------------------------------------------------------
78
     * {@inheritdoc}
79
     */
80
    public function getAccessTokenEndpoint() {
81
        return new Uri( $this->GetOAuthBaseUri() . 'token' );
82
    }
83
    
84
    /** -----------------------------------------------------------------------
85
     * {@inheritdoc}
86
     */
87
    protected function getAuthorizationMethod()
88
    {
89
        return static::AUTHORIZATION_METHOD_QUERY_STRING;
90
    }
91
    
92
    /** -----------------------------------------------------------------------
93
     * {@inheritdoc}
94
     */
95
    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...
96
    {
97
        $data = json_decode($responseBody, true);
98
        if( $data === null || !is_array($data) ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
99
            throw new TokenResponseException( 'Unable to parse response.' );
100
        } elseif( isset($data['error']) ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
Coding Style introduced by
Expected 1 space after ELSEIF keyword; 0 found
Loading history...
101
            $err = $data['error'];
102
            throw new TokenResponseException( 
103
                                "Error in retrieving token: \"$err\"" );
104
        }
105
        
106
        $token = new StdOAuth2Token( $data['access_token'], null, 
107
                                     $data['expires_in'] );
108
        
109
        unset( $data['access_token'] );
110
        unset( $data['expires_in'] );
111
        
112
        $token->setExtraParams( $data );
113
        
114
        return $token;
115
    }
116
}
117