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

Gitter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 19
c 1
b 0
f 0
dl 0
loc 35
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A me() 0 15 3
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