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

Spotify   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A me() 0 15 3
1
<?php
2
/**
3
 * Class Spotify
4
 *
5
 * @created      06.04.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\{ClientCredentials, CSRFToken, OAuth2Provider, ProviderException, TokenRefresh};
15
use Psr\Http\Message\ResponseInterface;
16
use function sprintf;
17
18
/**
19
 * @see https://developer.spotify.com/web-api
20
 * @see https://beta.developer.spotify.com/documentation/general/guides/authorization-guide/
21
 */
22
class Spotify extends OAuth2Provider implements ClientCredentials, CSRFToken, TokenRefresh{
23
24
	/**
25
	 * @see https://developer.spotify.com/documentation/general/guides/scopes/
26
	 */
27
	// images
28
	public const SCOPE_UGC_IMAGE_UPLOAD            = 'ugc-image-upload';
29
	// playlists
30
	public const SCOPE_PLAYLIST_MODIFY_PRIVATE     = 'playlist-modify-private';
31
	public const SCOPE_PLAYLIST_READ_PRIVATE       = 'playlist-read-private';
32
	public const SCOPE_PLAYLIST_MODIFY_PUBLIC      = 'playlist-modify-public';
33
	public const SCOPE_PLAYLIST_READ_COLLABORATIVE = 'playlist-read-collaborative';
34
	// users
35
	public const SCOPE_USER_READ_PRIVATE           = 'user-read-private';
36
	public const SCOPE_USER_READ_EMAIL             = 'user-read-email';
37
	// spotify connect
38
	public const SCOPE_USER_READ_PLAYBACK_STATE    = 'user-read-playback-state';
39
	public const SCOPE_USER_MODIFY_PLAYBACK_STATE  = 'user-modify-playback-state';
40
	public const SCOPE_USER_READ_CURRENTLY_PLAYING = 'user-read-currently-playing';
41
	// library
42
	public const SCOPE_USER_LIBRARY_MODIFY         = 'user-library-modify';
43
	public const SCOPE_USER_LIBRARY_READ           = 'user-library-read';
44
	// listening history
45
	public const SCOPE_USER_READ_PLAYBACK_POSITION = 'user-read-playback-position';
46
	public const SCOPE_USER_READ_RECENTLY_PLAYED   = 'user-read-recently-played';
47
	public const SCOPE_USER_TOP_READ               = 'user-top-read';
48
	// playback
49
#	public const SCOPE_APP_REMOTE_CONTROL          = 'app-remote-control'; // currently only on ios and android
50
	public const SCOPE_STREAMING                   = 'streaming'; // web playback SDK
51
	// follow
52
	public const SCOPE_USER_FOLLOW_MODIFY          = 'user-follow-modify';
53
	public const SCOPE_USER_FOLLOW_READ            = 'user-follow-read';
54
55
	protected string  $authURL                     = 'https://accounts.spotify.com/authorize';
56
	protected string  $accessTokenURL              = 'https://accounts.spotify.com/api/token';
57
	protected string  $apiURL                      = 'https://api.spotify.com';
58
	protected ?string $userRevokeURL               = 'https://www.spotify.com/account/apps/';
59
	protected ?string $apiDocs                     = 'https://developer.spotify.com/documentation/web-api/';
60
	protected ?string $applicationURL              = 'https://developer.spotify.com/dashboard/applications';
61
62
	protected array   $defaultScopes               = [
63
		self::SCOPE_PLAYLIST_READ_COLLABORATIVE,
64
		self::SCOPE_PLAYLIST_MODIFY_PUBLIC,
65
		self::SCOPE_USER_FOLLOW_MODIFY,
66
		self::SCOPE_USER_FOLLOW_READ,
67
		self::SCOPE_USER_LIBRARY_READ,
68
		self::SCOPE_USER_LIBRARY_MODIFY,
69
		self::SCOPE_USER_TOP_READ,
70
		self::SCOPE_USER_READ_EMAIL,
71
		self::SCOPE_STREAMING,
72
		self::SCOPE_USER_READ_PLAYBACK_STATE,
73
		self::SCOPE_USER_MODIFY_PLAYBACK_STATE,
74
		self::SCOPE_USER_READ_CURRENTLY_PLAYING,
75
		self::SCOPE_USER_READ_RECENTLY_PLAYED,
76
	];
77
78
	/**
79
	 * @inheritDoc
80
	 */
81
	public function me():ResponseInterface{
82
		$response = $this->request('/v1/me');
83
		$status   = $response->getStatusCode();
84
85
		if($status === 200){
86
			return $response;
87
		}
88
89
		$json = MessageUtil::decodeJSON($response);
90
91
		if(isset($json->error, $json->error->message)){
92
			throw new ProviderException($json->error->message);
93
		}
94
95
		throw new ProviderException(sprintf('user info error error HTTP/%s', $status));
96
	}
97
98
}
99