MicrosoftGraph::me()   A
last analyzed

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 MicrosoftGraph
4
 *
5
 * @created      30.07.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\ProviderException;
15
use Psr\Http\Message\ResponseInterface;
16
use function sprintf;
17
18
/**
19
 * @see https://docs.microsoft.com/graph/permissions-reference
20
 */
21
class MicrosoftGraph extends AzureActiveDirectory{
22
23
	public const SCOPE_USER_READ          = 'User.Read';
24
	public const SCOPE_USER_READBASIC_ALL = 'User.ReadBasic.All';
25
26
	protected string  $apiURL             = 'https://graph.microsoft.com';
27
	protected ?string $apiDocs            = 'https://docs.microsoft.com/graph/overview';
28
29
	protected array   $defaultScopes      = [
30
		self::SCOPE_OPENID,
31
		self::SCOPE_OPENID_EMAIL,
32
		self::SCOPE_OPENID_PROFILE,
33
		self::SCOPE_OFFLINE_ACCESS,
34
		self::SCOPE_USER_READ,
35
		self::SCOPE_USER_READBASIC_ALL,
36
	];
37
38
	/**
39
	 * @inheritDoc
40
	 */
41
	public function me():ResponseInterface{
42
		$response = $this->request('/v1.0/me');
43
		$status   = $response->getStatusCode();
44
45
		if($status === 200){
46
			return $response;
47
		}
48
49
		$json = MessageUtil::decodeJSON($response);
50
51
		if(isset($json->error, $json->error->message)){
52
			throw new ProviderException($json->error->message);
53
		}
54
55
		throw new ProviderException(sprintf('user info error error HTTP/%s', $status));
56
	}
57
58
}
59