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 Pbxg33k\MusicInfo\Service\Spotify\Endpoint\Track; |
14
|
|
|
use SpotifyWebAPI\Session; |
15
|
|
|
use SpotifyWebAPI\SpotifyWebAPI; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
|
18
|
|
|
class Service extends BaseService |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Is the client authorized via OAuth2.0 |
22
|
|
|
* |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
protected $authorized = false; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Session |
29
|
|
|
*/ |
30
|
|
|
protected $spotifySession; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Artist |
34
|
|
|
*/ |
35
|
|
|
protected $artist; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Track |
39
|
|
|
*/ |
40
|
|
|
protected $track; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
11 |
|
public function init($config = []) |
46
|
|
|
{ |
47
|
11 |
|
if (empty($config)) { |
48
|
11 |
|
$config = $this->getConfig(); |
49
|
11 |
|
} |
50
|
|
|
|
51
|
11 |
|
$this->spotifySession = new Session($config['client_id'], $config['client_secret'], $config['redirect_uri']); |
52
|
11 |
|
$this->setApiClient(new SpotifyWebAPI()); |
53
|
|
|
|
54
|
11 |
|
$this->requestCredentialsToken($config['scopes']); |
55
|
11 |
|
$this->setInitialized(true); |
56
|
|
|
|
57
|
11 |
|
$this->artist = new Artist($this); |
58
|
11 |
|
$this->track = new Track($this); |
59
|
|
|
|
60
|
11 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $scopes |
65
|
|
|
* |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
11 |
|
public function requestCredentialsToken($scopes) |
69
|
|
|
{ |
70
|
11 |
|
$this->spotifySession->requestCredentialsToken($scopes); |
71
|
|
|
|
72
|
11 |
|
return $this->setAccessTokenFromSession(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param Request $request |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function requestAccessToken(Request $request) |
81
|
|
|
{ |
82
|
|
|
$this->spotifySession->requestAccessToken($request->query->get('code')); |
83
|
|
|
$this->setAccessTokenFromSession(); |
84
|
|
|
|
85
|
|
|
return $this->getApiClient()->getAccessToken(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function refreshTokens() |
92
|
|
|
{ |
93
|
|
|
return $this->spotifySession->refreshAccessToken($this->spotifySession->getRefreshToken()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
11 |
|
protected function setAccessTokenFromSession() |
100
|
|
|
{ |
101
|
11 |
|
return $this->getApiClient()->setAccessToken($this->spotifySession->getAccessToken()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return Artist |
106
|
|
|
*/ |
107
|
1 |
|
public function getArtist() |
108
|
|
|
{ |
109
|
1 |
|
return $this->artist; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return Track |
114
|
|
|
*/ |
115
|
1 |
|
public function getTrack() |
116
|
|
|
{ |
117
|
1 |
|
return $this->track; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testArtistSearchWithServiceAsString() |
121
|
|
|
{ |
122
|
|
|
$result = $this->musicInfo->doSearch(self::TEST_SEARCH_NAME, 'artist', self::SERVICE_KEY); |
|
|
|
|
123
|
|
|
|
124
|
|
|
$this->assertInstanceOf(ArrayCollection::class, $result); |
|
|
|
|
125
|
|
|
$this->assertInstanceOf(ArrayCollection::class, $result->get( self::SERVICE_KEY )); |
|
|
|
|
126
|
|
|
$this->assertInstanceOf(\Pbxg33k\MusicInfo\Model\Artist::class, $result->get( self::SERVICE_KEY )->first()); |
|
|
|
|
127
|
|
|
} |
128
|
5 |
|
|
129
|
|
|
/** |
130
|
5 |
|
* @return Session |
131
|
|
|
*/ |
132
|
|
|
public function getSpotifySession() |
133
|
2 |
|
{ |
134
|
|
|
return $this->spotifySession; |
135
|
2 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return boolean |
139
|
|
|
*/ |
140
|
|
|
public function isAuthorized() |
141
|
|
|
{ |
142
|
|
|
return $this->authorized; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function artist() |
146
|
|
|
{ |
147
|
|
|
return $this->artist; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function track() |
151
|
|
|
{ |
152
|
|
|
return $this->track; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: