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

SoundCloud   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 18
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 SoundCloud
4
 *
5
 * @created      22.10.2017
6
 * @author       Smiley <[email protected]>
7
 * @copyright    2017 Smiley
8
 * @license      MIT
9
 */
10
11
namespace chillerlan\OAuth\Providers;
12
13
use chillerlan\HTTP\Utils\MessageUtil;
14
use chillerlan\OAuth\Core\{AccessToken, ClientCredentials, OAuth2Provider, ProviderException, TokenRefresh};
15
use chillerlan\HTTP\Utils\QueryUtil;
16
use Psr\Http\Message\ResponseInterface;
17
use function base64_encode;
18
use function implode;
19
use function sprintf;
20
use const PHP_QUERY_RFC1738;
21
22
/**
23
 * @see https://developers.soundcloud.com/
24
 * @see https://developers.soundcloud.com/docs/api/guide#authentication
25
 * @see https://developers.soundcloud.com/blog/security-updates-api
26
 */
27
class SoundCloud extends OAuth2Provider implements ClientCredentials, TokenRefresh{
28
29
	public const SCOPE_NONEXPIRING      = 'non-expiring';
30
#	public const SCOPE_EMAIL            = 'email'; // ???
31
32
	protected string  $authURL          = 'https://api.soundcloud.com/connect';
33
	protected string  $accessTokenURL   = 'https://api.soundcloud.com/oauth2/token';
34
	protected string  $apiURL           = 'https://api.soundcloud.com';
35
	protected ?string $userRevokeURL    = 'https://soundcloud.com/settings/connections';
36
	protected ?string $apiDocs          = 'https://developers.soundcloud.com/';
37
	protected ?string $applicationURL   = 'https://soundcloud.com/you/apps';
38
	protected string  $authMethodHeader = 'OAuth';
39
40
	protected array   $defaultScopes    = [
41
		self::SCOPE_NONEXPIRING,
42
	];
43
44
	/**
45
	 * @inheritDoc
46
	 */
47
	public function me():ResponseInterface{
48
		$response = $this->request('/me');
49
		$status   = $response->getStatusCode();
50
51
		if($status === 200){
52
			return $response;
53
		}
54
55
		$json = MessageUtil::decodeJSON($response);
56
57
		if(isset($json->status)){
58
			throw new ProviderException($json->status);
59
		}
60
61
		throw new ProviderException(sprintf('user info error error HTTP/%s', $status));
62
	}
63
64
}
65