From 4a487383daf3c4e1ec212be369a3226a267bdc57 Mon Sep 17 00:00:00 2001 From: AJ Henderson Date: Wed, 29 Apr 2015 09:54:38 -0400 Subject: [PATCH] Add Battle.Net Services This can probably be cleaned up a bit, but I wasn't sure how. Each region uses the same syntax and pattern, however all URIs have a different sub-domain. I implemented it as a base class and overrides for the region, but ideally it should be a single class that can take a region setting. I wasn't sure how to tackle such a thing consistent with the architectural objectives of the project though. --- src/OAuth2/Service/BattleNetBase.php | 87 ++++++++++++++++++++++++++++ src/OAuth2/Service/BattleNetCN.php | 18 ++++++ src/OAuth2/Service/BattleNetEU.php | 18 ++++++ src/OAuth2/Service/BattleNetKR.php | 18 ++++++ src/OAuth2/Service/BattleNetTW.php | 18 ++++++ src/OAuth2/Service/BattleNetUS.php | 18 ++++++ 6 files changed, 177 insertions(+) create mode 100644 src/OAuth2/Service/BattleNetBase.php create mode 100644 src/OAuth2/Service/BattleNetCN.php create mode 100644 src/OAuth2/Service/BattleNetEU.php create mode 100644 src/OAuth2/Service/BattleNetKR.php create mode 100644 src/OAuth2/Service/BattleNetTW.php create mode 100644 src/OAuth2/Service/BattleNetUS.php diff --git a/src/OAuth2/Service/BattleNetBase.php b/src/OAuth2/Service/BattleNetBase.php new file mode 100644 index 0000000..4868ee7 --- /dev/null +++ b/src/OAuth2/Service/BattleNetBase.php @@ -0,0 +1,87 @@ +baseApiUri = new Uri('https://' . $this->region . '.api.battle.net/'); + } + } + + /** + * @return \OAuth\Common\Http\Uri\UriInterface + */ + public function getAuthorizationEndpoint() + { + return new Uri('https://' . $this->region . '.battle.net/oauth/authorize'); + } + + /** + * @return \OAuth\Common\Http\Uri\UriInterface + */ + public function getAccessTokenEndpoint() + { + return new Uri('https://' . $this->region . '.battle.net/oauth/token'); + } + + /** + * @param string $responseBody + * @return \OAuth\Common\Token\TokenInterface|\OAuth\OAuth2\Token\StdOAuth2Token + * @throws \OAuth\Common\Http\Exception\TokenResponseException + */ + protected function parseAccessTokenResponse($responseBody) + { + $data = json_decode( $responseBody, true ); + + if( null === $data || !is_array($data) ) { + throw new TokenResponseException('Unable to parse response.'); + } elseif( isset($data['error'] ) ) { + throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); + } + + $token = new StdOAuth2Token(); + + $token->setAccessToken( $data['access_token'] ); + // I'm invincible!!! + $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES); + unset( $data['access_token'] ); + $token->setExtraParams( $data ); + + return $token; + } + + /** + * @return int + */ + protected function getAuthorizationMethod() + { + return static::AUTHORIZATION_METHOD_QUERY_STRING; + } +} \ No newline at end of file diff --git a/src/OAuth2/Service/BattleNetCN.php b/src/OAuth2/Service/BattleNetCN.php new file mode 100644 index 0000000..872280c --- /dev/null +++ b/src/OAuth2/Service/BattleNetCN.php @@ -0,0 +1,18 @@ +region = 'cn'; + $this->baseApiUri = new Uri('https://' . $this->region . '.api.battle.net/'); + } +} \ No newline at end of file diff --git a/src/OAuth2/Service/BattleNetEU.php b/src/OAuth2/Service/BattleNetEU.php new file mode 100644 index 0000000..a304577 --- /dev/null +++ b/src/OAuth2/Service/BattleNetEU.php @@ -0,0 +1,18 @@ +region = 'eu'; + $this->baseApiUri = new Uri('https://' . $this->region . '.api.battle.net/'); + } +} \ No newline at end of file diff --git a/src/OAuth2/Service/BattleNetKR.php b/src/OAuth2/Service/BattleNetKR.php new file mode 100644 index 0000000..b73aaee --- /dev/null +++ b/src/OAuth2/Service/BattleNetKR.php @@ -0,0 +1,18 @@ +region = 'kr'; + $this->baseApiUri = new Uri('https://' . $this->region . '.api.battle.net/'); + } +} \ No newline at end of file diff --git a/src/OAuth2/Service/BattleNetTW.php b/src/OAuth2/Service/BattleNetTW.php new file mode 100644 index 0000000..30181cb --- /dev/null +++ b/src/OAuth2/Service/BattleNetTW.php @@ -0,0 +1,18 @@ +region = 'tw'; + $this->baseApiUri = new Uri('https://' . $this->region . '.api.battle.net/'); + } +} diff --git a/src/OAuth2/Service/BattleNetUS.php b/src/OAuth2/Service/BattleNetUS.php new file mode 100644 index 0000000..4347cdc --- /dev/null +++ b/src/OAuth2/Service/BattleNetUS.php @@ -0,0 +1,18 @@ +region = 'us'; + $this->baseApiUri = new Uri('https://' . $this->region . '.api.battle.net/'); + } +} \ No newline at end of file