Skip to content

Commit aab6ec1

Browse files
committed
BattleNet refactoring
1 parent 8693847 commit aab6ec1

File tree

18 files changed

+242
-240
lines changed

18 files changed

+242
-240
lines changed

src/Common/Http/Url.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use League\Url\Components\AbstractSegment;
66
use League\Url\Url as BaseUrl;
7+
use OAuth\Common\Exception\Exception;
78

89
class Url extends BaseUrl
910
{
@@ -41,4 +42,73 @@ public function __clone()
4142
}
4243
}
4344
}
45+
46+
public static function replacePlaceholders($uri, array $placeholders, array $placeholdersEmptyReplaces = [])
47+
{
48+
// Quick checks
49+
50+
if (!$placeholders and !$placeholdersEmptyReplaces) {
51+
return $uri;
52+
} elseif (false === strpos($uri, '{')) {
53+
return $uri;
54+
}
55+
56+
// Replace placeholders with values
57+
foreach ($placeholders as $placeholder => $value) {
58+
// Validate placeholder
59+
if (!(is_string($value) or is_numeric($value) or false === $value)) {
60+
throw new Exception(
61+
'Placeholder value must be a string or numeric! Actual value "' .
62+
$value .
63+
'"'
64+
);
65+
}
66+
67+
// If placeholder value is empty skip current loop pass
68+
if (!($value or 0 === $value or '0' === $value)) {
69+
if (empty($placeholdersEmptyReplaces[ $placeholder ])) {
70+
$placeholdersEmptyReplaces[ $placeholder ] = '{}';
71+
}
72+
continue;
73+
}
74+
75+
// Update uri
76+
if (false !== strpos($uri, '{' . $placeholder . '}')) {
77+
// Replace with value
78+
if ($value or 0 === $value or '0' === $value) {
79+
$uri = str_replace('{' . $placeholder . '}', $value, $uri);
80+
}
81+
}
82+
}
83+
84+
// Replace empty placeholders or placeholders with empty values
85+
foreach ($placeholdersEmptyReplaces as $placeholder => $emptyValue) {
86+
if (!(is_string($emptyValue) or is_numeric($emptyValue))
87+
) {
88+
throw new Exception(
89+
'Placeholder replacer value must be a string or numeric! Actual value: "' .
90+
$emptyValue .
91+
'"'
92+
);
93+
} elseif (false === strpos($emptyValue, '{}')) {
94+
throw new Exception(
95+
'Placeholder replacer must contain "{}"! Actual value: "' .
96+
$emptyValue .
97+
'"'
98+
);
99+
}
100+
101+
// Replace with empty value
102+
if (false !== strpos($uri, '{' . $placeholder . '}')) {
103+
$uri = str_replace(
104+
str_replace('{}', '{' . $placeholder . '}', $emptyValue),
105+
"",
106+
$uri
107+
);
108+
}
109+
}
110+
111+
112+
return $uri;
113+
}
44114
}

src/Common/Service/AbstractService.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ abstract class AbstractService implements ServiceInterface
3838
/** @var string */
3939
protected $accessTokenEndpoint = '';
4040

41+
/** @var array */
42+
protected $urlPlaceholders = [];
43+
44+
/** @var array */
45+
protected $urlPlaceholdersEmptyReplaces = [];
46+
4147
/** @var ExtractorFactory */
4248
protected static $extractorFactory;
4349

@@ -58,9 +64,7 @@ public function __construct(
5864
$this->storage = $storage;
5965

6066
if ($baseApiUrl) {
61-
$this->baseApiUri = new Url($baseApiUrl);
62-
} elseif (is_string($this->baseApiUri)) {
63-
$this->baseApiUri = new Url($this->baseApiUri);
67+
$this->baseApiUri = $baseApiUrl;
6468
}
6569

6670
$this->initialize();
@@ -70,15 +74,15 @@ public function initialize()
7074
{
7175
}
7276

73-
public function getBaseApiUri($clone = true)
77+
public function getBaseApiUri()
7478
{
7579
if (null === $this->baseApiUri) {
7680
throw new Exception(
7781
'An absolute URI must be passed to ServiceInterface::request as no baseApiUri is set.'
7882
);
7983
}
8084

81-
return !$clone ? $this->baseApiUri : clone $this->baseApiUri;
85+
return new Url($this->injectPlaceholdersToUri($this->baseApiUri));
8286
}
8387

8488
/**
@@ -90,7 +94,7 @@ public function getAuthorizationEndpoint()
9094
throw new Exception('Authorization endpoint isn\'t defined!');
9195
}
9296

93-
return new Url($this->authorizationEndpoint);
97+
return new Url($this->injectPlaceholdersToUri($this->authorizationEndpoint));
9498
}
9599

96100
/**
@@ -102,7 +106,7 @@ public function getAccessTokenEndpoint()
102106
throw new Exception('Access token endpoint isn\'t defined!');
103107
}
104108

105-
return new Url($this->accessTokenEndpoint);
109+
return new Url($this->injectPlaceholdersToUri($this->accessTokenEndpoint));
106110
}
107111

108112
/**
@@ -116,7 +120,7 @@ protected function determineRequestUriFromPath($path)
116120
if ($path instanceof Url) {
117121
$uri = $path;
118122
} elseif (stripos($path, 'http://') === 0 || stripos($path, 'https://') === 0) {
119-
$uri = new Url($path);
123+
$uri = new Url($this->injectPlaceholdersToUri($path));
120124
} else {
121125
$path = (string) $path;
122126
$uri = $this->getBaseApiUri();
@@ -138,6 +142,12 @@ protected function determineRequestUriFromPath($path)
138142
return $uri;
139143
}
140144

145+
protected function injectPlaceholdersToUri($uri)
146+
{
147+
148+
return Url::replacePlaceholders($uri, $this->urlPlaceholders, $this->urlPlaceholdersEmptyReplaces);
149+
}
150+
141151
/**
142152
* Accessor to the storage adapter to be able to retrieve tokens
143153
*

src/OAuth1/Service/AbstractService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function __construct(
5555
*/
5656
public function getRequestTokenEndpoint()
5757
{
58-
return new Url($this->requestTokenEndpoint);
58+
return new Url($this->injectPlaceholdersToUri($this->requestTokenEndpoint));
5959
}
6060

6161
/**
@@ -130,7 +130,7 @@ public function requestAccessToken($token, $verifier, $tokenSecret = null)
130130
*/
131131
public function request($path, array $body = [], $method = 'GET', array $extraHeaders = [])
132132
{
133-
$uri = $this->determineRequestUriFromPath($path, $this->baseApiUri);
133+
$uri = $this->determineRequestUriFromPath($path);
134134

135135
/** @var $token StdOAuth1Token */
136136
$token = $this->storage->retrieveAccessToken($this->service());

src/OAuth2/Service/AbstractService.php

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -65,29 +65,16 @@ public function __construct(
6565

6666
$this->scopes = $scopes;
6767

68+
// Replaces "/{apiVersion}" to configured proper api version, or remove version from URI if it's have not configured
6869
if ($apiVersion) {
6970
$this->apiVersion = $apiVersion;
7071
}
71-
72-
if ($this->baseApiUri) {
73-
$this->injectApiVersionToUri($this->baseApiUri);
72+
if ($this->apiVersion) {
73+
$this->urlPlaceholders[ 'apiVersion' ] = $this->apiVersion;
7474
}
75-
}
76-
77-
/**
78-
* {@inheritdoc}
79-
*/
80-
public function getAuthorizationEndpoint()
81-
{
82-
return $this->injectApiVersionToUri(parent::getAuthorizationEndpoint());
83-
}
8475

85-
/**
86-
* {@inheritdoc}
87-
*/
88-
public function getAccessTokenEndpoint()
89-
{
90-
return $this->injectApiVersionToUri(parent::getAccessTokenEndpoint());
76+
// How replace empty value
77+
$this->urlPlaceholdersEmptyReplaces[ 'apiVersion' ] = '/{}';
9178
}
9279

9380
/**
@@ -345,30 +332,6 @@ protected function getAuthorizationMethod()
345332
return $this->authorizationMethod;
346333
}
347334

348-
/**
349-
* Returns api version string if is set else retrun empty string
350-
*
351-
* @return string
352-
*/
353-
protected function getApiVersionString()
354-
{
355-
return !(empty($this->apiVersion)) ? "/" . $this->apiVersion : "";
356-
}
357-
358-
/**
359-
* Replaces "/{apiVersion}" to configured proper api version, or remove version from URI if it's have not configured
360-
*
361-
* @param Url $uri
362-
*
363-
* @return Url
364-
*/
365-
protected function injectApiVersionToUri(Url $uri)
366-
{
367-
$uri->setPath(str_replace('/{apiVersion}', $this->getApiVersionString(), '/' . urldecode($uri->getPath())));
368-
369-
return $uri;
370-
}
371-
372335
/**
373336
* {@inheritdoc}
374337
*/

src/OAuth2/Service/BattleNet.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
namespace OAuth\OAuth2\Service;
3+
4+
use OAuth\Common\Exception\Exception;
5+
use OAuth\Common\Http\Exception\TokenResponseException;
6+
use OAuth\OAuth2\Token\StdOAuth2Token;
7+
8+
class BattleNet extends AbstractService
9+
{
10+
11+
/**
12+
* Scopes
13+
*
14+
* @var string
15+
*/
16+
17+
const SCOPE_WOW_PROFILE = 'wow.profile';
18+
const SCOPE_SC2_PROFILE = 'sc2.profile';
19+
const REGION_CN = 'cn';
20+
const REGION_EU = 'eu';
21+
const REGION_KR = 'kr';
22+
const REGION_TW = 'tw';
23+
const REGION_US = 'us';
24+
25+
protected $baseApiUri = 'https://{region}.api.battle.net/';
26+
protected $authorizationEndpoint = 'https://{region}.battle.net/oauth/authorize';
27+
protected $accessTokenEndpoint = 'https://{region}.battle.net/oauth/token';
28+
protected $authorizationMethod = self::AUTHORIZATION_METHOD_QUERY_STRING;
29+
30+
/**
31+
* BattleNet Region
32+
*
33+
* @var string
34+
*/
35+
protected $region;
36+
37+
/**
38+
* Set region based on constants
39+
* $service->setRegion(BattleNet::REGION_US)
40+
*
41+
* @param $region
42+
*
43+
* @return $this
44+
* @throws Exception
45+
*/
46+
public function setRegion($region)
47+
{
48+
// Check region
49+
50+
$reflClass = new \ReflectionClass($this);
51+
$constants = $reflClass->getConstants();
52+
$regions = [];
53+
54+
foreach ($constants as $constant => $value) {
55+
if (0 === strpos($constant, 'REGION_')) {
56+
$regions[] = $value;
57+
}
58+
}
59+
if (!in_array($region, $regions)) {
60+
throw new Exception("Region \"$region\" is unknown!");
61+
}
62+
63+
$this->region = $this->urlPlaceholders[ 'region' ] = $region;
64+
65+
return $this;
66+
}
67+
68+
/**
69+
* @param string $responseBody
70+
*
71+
* @return \OAuth\Common\Token\TokenInterface|\OAuth\OAuth2\Token\StdOAuth2Token
72+
* @throws \OAuth\Common\Http\Exception\TokenResponseException
73+
*/
74+
protected function parseAccessTokenResponse($responseBody)
75+
{
76+
$data = json_decode($responseBody, true);
77+
78+
if (null === $data || !is_array($data)) {
79+
throw new TokenResponseException('Unable to parse response.');
80+
} elseif (isset($data[ 'error' ])) {
81+
throw new TokenResponseException('Error in retrieving token: "' . $data[ 'error' ] . '"');
82+
}
83+
84+
$token = new StdOAuth2Token();
85+
86+
$token->setAccessToken($data[ 'access_token' ]);
87+
// I'm invincible!!!
88+
$token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES);
89+
unset($data[ 'access_token' ]);
90+
$token->setExtraParams($data);
91+
92+
return $token;
93+
}
94+
}

0 commit comments

Comments
 (0)