AbstractBattleNetAuth::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4286
cc 1
eloc 9
nc 1
nop 6
1
<?php
2
namespace Jleagle\BattleNet\Request;
3
4
use Jleagle\BattleNet\Enums\AuthScopes;
5
use Jleagle\BattleNet\Enums\ServerLocations;
6
use Jleagle\BattleNet\Exceptions\BattleNetException;
7
use Jleagle\CurlWrapper\Curl;
8
use Jleagle\CurlWrapper\Exceptions\CurlException;
9
use Jleagle\CurlWrapper\Exceptions\CurlInvalidJsonException;
10
11
abstract class AbstractBattleNetAuth
12
{
13
  use BattleNetTrait;
14
15
  protected $_apiSecret;
16
  protected $_redirectUrl;
17
  protected $_scopes;
18
19
  /**
20
   * @param string   $apiKey
21
   * @param string   $apiSecret
22
   * @param string   $redirectUrl
23
   * @param string[] $scopes
24
   * @param string   $serverLocation
25
   * @param string   $responseLocale
26
   */
27
  public function __construct(
28
    $apiKey, $apiSecret, $redirectUrl, $scopes = [],
29
    $serverLocation = ServerLocations::US, $responseLocale = null
30
  )
31
  {
32
    $this->_apiKey = $apiKey;
0 ignored issues
show
Bug introduced by
The property _apiKey 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...
33
    $this->_apiSecret = $apiSecret;
34
    $this->_redirectUrl = $redirectUrl;
35
    $this->_scopes = $scopes;
36
    $this->_serverLocation = $serverLocation;
37
    $this->_responseLocale = $responseLocale;
0 ignored issues
show
Bug introduced by
The property _responseLocale 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...
38
  }
39
40
  /**
41
   * @return string
42
   */
43
  private function _getScopes()
44
  {
45
    $scopes = $this->_scopes;
46
    if(!$scopes)
0 ignored issues
show
Bug Best Practice introduced by
The expression $scopes of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
47
    {
48
      $scopes = [AuthScopes::WOW, AuthScopes::SC2];
49
    }
50
    return implode(' ', $scopes);
51
  }
52
53
  /**
54
   * @param string|null $state
55
   * @param bool        $redirect
56
   *
57
   * @return string[]
58
   */
59
  public function getCode($state = null, $redirect = false)
60
  {
61
    if(!$state)
0 ignored issues
show
Bug Best Practice introduced by
The expression $state of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
62
    {
63
      $state = rand(11111, 99999);
64
    }
65
66
    $params = http_build_query(
67
      [
68
        'client_id'     => $this->_apiKey,
69
        'scope'         => $this->_getScopes(),
70
        'state'         => $state,
71
        'redirect_uri'  => $this->_redirectUrl,
72
        'response_type' => 'code',
73
      ]
74
    );
75
    $url = 'https://' . $this->_serverLocation . '.battle.net/oauth/authorize?' . $params;
76
77
    if($redirect)
78
    {
79
      header('Location: ' . $url);
80
      exit;
81
    }
82
83
    return ['redirectUrl' => $url, 'state' => $state];
84
  }
85
86
  /**
87
   * Each code can only be used once
88
   *
89
   * @param string $code
90
   *
91
   * @return array
92
   *
93
   * @throws CurlException
94
   * @throws CurlInvalidJsonException
95
   */
96
  public function getAccessToken($code)
97
  {
98
    $url = 'https://' . $this->_serverLocation . '.battle.net/oauth/token';
99
100
    $data = [
101
      'redirect_uri' => $this->_redirectUrl,
102
      'scope'        => $this->_getScopes(),
103
      'grant_type'   => 'authorization_code',
104
      'code'         => $code,
105
    ];
106
107
    return Curl::post($url, $data)
108
      ->setBasicAuth($this->_apiKey, $this->_apiSecret)
109
      ->run()
110
      ->getJson();
111
  }
112
113
  /**
114
   * @param string $path
115
   * @param string $accessToken
116
   *
117
   * @return array
118
   * @throws BattleNetException
119
   */
120
  protected function _get($path, $accessToken)
121
  {
122
    $data = ['access_token' => $accessToken];
123
124
    try
125
    {
126
      return Curl::get($this->_makeApiUrl($path), $data)->run()->getJson();
127
    }
128
    catch(CurlException $e)
129
    {
130
      $json = $e->getResponse()->getJson();
131
      $message = isset($json['reason']) ? $json['reason'] : $e->getMessage();
132
      throw new BattleNetException($message);
133
    }
134
  }
135
}
136