Passed
Push — main ( e868ec...9f95cc )
by smiley
12:00
created

BattleNet::me()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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