1 | <?php |
||
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() |
|
132 | |||
133 | /** |
||
134 | * @return Album |
||
135 | */ |
||
136 | public function getAlbum() |
||
137 | { |
||
138 | return $this->album; |
||
140 | |||
141 | /** |
||
142 | * @return Session |
||
143 | */ |
||
144 | 1 | public function getSpotifySession() |
|
148 | |||
149 | /** |
||
150 | * @return boolean |
||
151 | */ |
||
152 | public function isAuthorized() |
||
156 | |||
157 | /** |
||
158 | * @return Artist |
||
159 | */ |
||
160 | 7 | public function artist() |
|
164 | |||
165 | /** |
||
166 | * @return Track |
||
167 | */ |
||
168 | 3 | public function track() |
|
172 | |||
173 | /** |
||
174 | * @return Album |
||
175 | */ |
||
176 | 2 | public function album() |
|
180 | |||
181 | } |