1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: PBX_g33k |
5
|
|
|
* Date: 30-May-16 |
6
|
|
|
* Time: 13:01 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Pbxg33k\MusicInfo\Service\Spotify; |
10
|
|
|
|
11
|
|
|
use Pbxg33k\MusicInfo\Service\BaseService; |
12
|
|
|
use Pbxg33k\MusicInfo\Service\Spotify\Endpoint\Artist; |
13
|
|
|
use SpotifyWebAPI\Session; |
14
|
|
|
use SpotifyWebAPI\SpotifyWebAPI; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
|
17
|
|
|
class Service extends BaseService |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Is the client authorized via OAuth2.0 |
21
|
|
|
* |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
protected $authorized = false; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Session |
28
|
|
|
*/ |
29
|
|
|
protected $spotifySession; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Endpoints |
33
|
|
|
*/ |
34
|
|
|
protected $artist; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
1 |
|
public function init($config = []) |
40
|
|
|
{ |
41
|
1 |
|
if(!$config) { |
|
|
|
|
42
|
1 |
|
$config = $this->getConfig(); |
43
|
1 |
|
} |
44
|
|
|
|
45
|
1 |
|
$this->spotifySession = new Session($config['client_id'], $config['client_secret'], $config['redirect_uri']); |
46
|
1 |
|
$this->setApiClient(new SpotifyWebAPI()); |
47
|
|
|
|
48
|
1 |
|
$this->requestCredentialsToken($config['scopes']); |
49
|
|
|
|
50
|
1 |
|
$this->artist = new Artist($this); |
51
|
|
|
|
52
|
1 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $scopes |
57
|
|
|
* |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
1 |
|
public function requestCredentialsToken($scopes) |
61
|
|
|
{ |
62
|
1 |
|
$this->spotifySession->requestCredentialsToken($scopes); |
63
|
1 |
|
return $this->setAccessTokenFromSession(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Request $request |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function requestAccessToken(Request $request) |
72
|
|
|
{ |
73
|
|
|
$this->spotifySession->requestAccessToken($request->query->get('code')); |
74
|
|
|
$this->setAccessTokenFromSession(); |
75
|
|
|
return $this->getApiClient()->getAccessToken(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function refreshTokens() |
82
|
|
|
{ |
83
|
|
|
return $this->spotifySession->refreshAccessToken($this->spotifySession->getRefreshToken()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
1 |
|
protected function setAccessTokenFromSession() |
90
|
|
|
{ |
91
|
1 |
|
return $this->getApiClient()->setAccessToken($this->spotifySession->getAccessToken()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
public function getArtist() |
98
|
|
|
{ |
99
|
|
|
return $this->artist; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return Session |
104
|
|
|
*/ |
105
|
|
|
public function getSpotifySession() |
106
|
|
|
{ |
107
|
|
|
return $this->spotifySession; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return boolean |
112
|
|
|
*/ |
113
|
|
|
public function isAuthorized() |
114
|
|
|
{ |
115
|
|
|
return $this->authorized; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.