Gitter::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 Gitter
4
 *
5
 * @created      09.08.2018
6
 * @author       Smiley <[email protected]>
7
 * @copyright    2018 Smiley
8
 * @license      MIT
9
 */
10
11
namespace chillerlan\OAuth\Providers;
12
13
use chillerlan\HTTP\Utils\MessageUtil;
14
use chillerlan\OAuth\Core\{CSRFToken, OAuth2Provider, ProviderException};
15
use Psr\Http\Message\ResponseInterface;
16
use function sprintf;
17
18
/**
19
 * @see https://developer.gitter.im/
20
 * @see https://developer.gitter.im/docs/authentication
21
 */
22
class Gitter extends OAuth2Provider implements CSRFToken{
23
24
	public const SCOPE_FLOW            = 'flow';
25
	public const SCOPE_PRIVATE         = 'private';
26
27
	protected string  $authURL         = 'https://gitter.im/login/oauth/authorize';
28
	protected string  $accessTokenURL  = 'https://gitter.im/login/oauth/token';
29
	protected string  $scopesDelimiter = ',';
30
	protected string  $apiURL          = 'https://api.gitter.im';
31
	protected ?string $apiDocs         = 'https://developer.gitter.im';
32
	protected ?string $applicationURL  = 'https://developer.gitter.im/apps';
33
34
	protected array $defaultScopes     = [
35
		self::SCOPE_FLOW,
36
		self::SCOPE_PRIVATE,
37
	];
38
39
	/**
40
	 * @inheritDoc
41
	 */
42
	public function me():ResponseInterface{
43
		$response = $this->request('/v1/user/me');
44
		$status   = $response->getStatusCode();
45
46
		if($status === 200){
47
			return $response;
48
		}
49
50
		$json = MessageUtil::decodeJSON($response);
51
52
		if(isset($json->error)){
53
			throw new ProviderException($json->error);
54
		}
55
56
		throw new ProviderException(sprintf('user info error error HTTP/%s', $status));
57
	}
58
59
}
60