|
1
|
|
|
<?php |
|
2
|
|
|
/******************************************************************************* |
|
3
|
|
|
* This file is part of the Pbxg33k\MusicInfo package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) 2017 Oguzhan uysal. All rights reserved |
|
9
|
|
|
******************************************************************************/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Pbxg33k\MusicInfo\Service\Spotify; |
|
12
|
|
|
|
|
13
|
|
|
use Pbxg33k\MusicInfo\Service\BaseService; |
|
14
|
|
|
use Pbxg33k\MusicInfo\Service\Spotify\Endpoint\Album; |
|
15
|
|
|
use Pbxg33k\MusicInfo\Service\Spotify\Endpoint\Artist; |
|
16
|
|
|
use Pbxg33k\MusicInfo\Service\Spotify\Endpoint\Track; |
|
17
|
|
|
use SpotifyWebAPI\Session; |
|
18
|
|
|
use SpotifyWebAPI\SpotifyWebAPI; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class Service |
|
23
|
|
|
* @package Pbxg33k\MusicInfo\Service\Spotify |
|
24
|
|
|
*/ |
|
25
|
|
|
class Service extends BaseService |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Is the client authorized via OAuth2.0 |
|
29
|
|
|
* |
|
30
|
|
|
* @var bool |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $authorized = false; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var Session |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $spotifySession; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Artist |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $artist; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var Track |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $track; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var Album |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $album; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
39 |
|
public function init($config = []) |
|
58
|
|
|
{ |
|
59
|
39 |
|
if (empty($config)) { |
|
60
|
39 |
|
$config = $this->getConfig(); |
|
61
|
39 |
|
} |
|
62
|
|
|
|
|
63
|
39 |
|
$this->spotifySession = new Session($config['client_id'], $config['client_secret'], $config['redirect_uri']); |
|
64
|
39 |
|
$this->setApiClient(new SpotifyWebAPI()); |
|
65
|
|
|
|
|
66
|
39 |
|
$this->requestCredentialsToken($config['scopes']); |
|
67
|
39 |
|
$this->setInitialized(true); |
|
68
|
|
|
|
|
69
|
39 |
|
$this->artist = new Artist($this); |
|
70
|
39 |
|
$this->track = new Track($this); |
|
71
|
39 |
|
$this->album = new Album($this); |
|
72
|
|
|
|
|
73
|
39 |
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param $scopes |
|
78
|
|
|
* |
|
79
|
|
|
* @return mixed |
|
80
|
|
|
*/ |
|
81
|
39 |
|
public function requestCredentialsToken($scopes) |
|
82
|
|
|
{ |
|
83
|
39 |
|
$this->spotifySession->requestCredentialsToken($scopes); |
|
84
|
|
|
|
|
85
|
39 |
|
return $this->setAccessTokenFromSession(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param Request $request |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function requestAccessToken(Request $request) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->spotifySession->requestAccessToken($request->query->get('code')); |
|
96
|
|
|
$this->setAccessTokenFromSession(); |
|
97
|
|
|
|
|
98
|
|
|
return $this->getApiClient()->getAccessToken(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
|
|
public function refreshTokens() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->spotifySession->refreshAccessToken($this->spotifySession->getRefreshToken()); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return mixed |
|
111
|
|
|
*/ |
|
112
|
39 |
|
protected function setAccessTokenFromSession() |
|
113
|
|
|
{ |
|
114
|
39 |
|
return $this->getApiClient()->setAccessToken($this->spotifySession->getAccessToken()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return Artist |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function getArtist() |
|
121
|
|
|
{ |
|
122
|
1 |
|
return $this->artist; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return Track |
|
127
|
|
|
*/ |
|
128
|
1 |
|
public function getTrack() |
|
129
|
|
|
{ |
|
130
|
1 |
|
return $this->track; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return Album |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getAlbum() |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->album; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return Session |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public function getSpotifySession() |
|
145
|
|
|
{ |
|
146
|
1 |
|
return $this->spotifySession; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return boolean |
|
151
|
|
|
*/ |
|
152
|
|
|
public function isAuthorized() |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->authorized; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return Artist |
|
159
|
|
|
*/ |
|
160
|
7 |
|
public function artist() |
|
161
|
|
|
{ |
|
162
|
7 |
|
return $this->artist; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @return Track |
|
167
|
|
|
*/ |
|
168
|
3 |
|
public function track() |
|
169
|
|
|
{ |
|
170
|
3 |
|
return $this->track; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @return Album |
|
175
|
|
|
*/ |
|
176
|
2 |
|
public function album() |
|
177
|
|
|
{ |
|
178
|
2 |
|
return $this->album; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
} |