BattleNet   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 30
c 0
b 0
f 0
dl 0
loc 70
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A me() 0 15 3
A setRegion() 0 14 3
1
<?php
2
/**
3
 * Class BattleNet
4
 *
5
 * @created      02.08.2019
6
 * @author       smiley <[email protected]>
7
 * @copyright    2019 smiley
8
 * @license      MIT
9
 */
10
11
namespace chillerlan\OAuth\Providers;
12
13
use chillerlan\HTTP\Utils\MessageUtil;
14
use chillerlan\OAuth\Core\{ClientCredentials, CSRFToken, OAuth2Provider, ProviderException};
15
use chillerlan\OAuth\OAuthOptions;
16
use chillerlan\Settings\SettingsContainerInterface;
17
use Psr\Http\Client\ClientInterface;
18
use Psr\Http\Message\ResponseInterface;
19
use Psr\Log\LoggerInterface;
20
use function in_array;
21
use function sprintf;
22
use function strtolower;
23
24
/**
25
 * Battle.net OAuth
26
 *
27
 * @see https://develop.battle.net/documentation
28
 */
29
class BattleNet extends OAuth2Provider implements ClientCredentials, CSRFToken{
30
31
	public const SCOPE_OPENID         = 'openid';
32
	public const SCOPE_PROFILE_D3     = 'd3.profile';
33
	public const SCOPE_PROFILE_SC2    = 'sc2.profile';
34
	public const SCOPE_PROFILE_WOW    = 'wow.profile';
35
36
	protected ?string $apiDocs        = 'https://develop.battle.net/documentation';
37
	protected ?string $applicationURL = 'https://develop.battle.net/access/clients';
38
	protected ?string $userRevokeURL  = 'https://account.blizzard.com/connections';
39
40
	protected array $defaultScopes    = [
41
		self::SCOPE_OPENID,
42
		self::SCOPE_PROFILE_D3,
43
		self::SCOPE_PROFILE_SC2,
44
		self::SCOPE_PROFILE_WOW,
45
	];
46
47
	/**
48
	 * @inheritDoc
49
	 */
50
	public function __construct(
51
		ClientInterface $http,
52
		OAuthOptions|SettingsContainerInterface $options,
53
		LoggerInterface $logger = null
54
	){
55
		parent::__construct($http, $options, $logger);
56
57
		$this->setRegion('eu');
58
	}
59
60
	/**
61
	 * Set the datacenter URLs for the given region
62
	 *
63
	 * @throws \chillerlan\OAuth\Core\ProviderException
64
	 */
65
	public function setRegion(string $region):BattleNet{
66
		$region = strtolower($region);
67
68
		if(!in_array($region, ['apac', 'cn', 'eu', 'us'], true)){
69
			throw new ProviderException('invalid region: '.$region);
70
		}
71
72
		$url = 'https://'.($region === 'cn' ? 'www.battlenet.com.cn' : $region.'.battle.net');
73
74
		$this->apiURL         = $url;
75
		$this->authURL        = $url.'/oauth/authorize';
76
		$this->accessTokenURL = $url.'/oauth/token';
77
78
		return $this;
79
	}
80
81
	/**
82
	 * @inheritDoc
83
	 */
84
	public function me():ResponseInterface{
85
		$response = $this->request('/oauth/userinfo');
86
		$status   = $response->getStatusCode();
87
88
		if($status === 200){
89
			return $response;
90
		}
91
92
		$json = MessageUtil::decodeJSON($response);
93
94
		if(isset($json->error, $json->error_description)){
95
			throw new ProviderException($json->error_description);
96
		}
97
98
		throw new ProviderException(sprintf('user info error error HTTP/%s', $status));
99
	}
100
101
}
102