|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* (c) Christian Gripp <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Core23\LastFm\Service; |
|
13
|
|
|
|
|
14
|
|
|
use Core23\LastFm\Builder\ScrobbeBuilder; |
|
15
|
|
|
use Core23\LastFm\Builder\SimilarTrackBuilder; |
|
16
|
|
|
use Core23\LastFm\Builder\TrackInfoBuilder; |
|
17
|
|
|
use Core23\LastFm\Builder\TrackTagsBuilder; |
|
18
|
|
|
use Core23\LastFm\Builder\TrackTopTagsBuilder; |
|
19
|
|
|
use Core23\LastFm\Client\ApiClientInterface; |
|
20
|
|
|
use Core23\LastFm\Model\NowPlaying; |
|
21
|
|
|
use Core23\LastFm\Model\Song; |
|
22
|
|
|
use Core23\LastFm\Model\SongInfo; |
|
23
|
|
|
use Core23\LastFm\Model\Tag; |
|
24
|
|
|
use Core23\LastFm\Session\SessionInterface; |
|
25
|
|
|
use Core23\LastFm\Util\ApiHelper; |
|
26
|
|
|
use InvalidArgumentException; |
|
27
|
|
|
|
|
28
|
|
|
final class TrackService implements TrackServiceInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var ApiClientInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $client; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param ApiClientInterface $client |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(ApiClientInterface $client) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->client = $client; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public function addTags(SessionInterface $session, string $artist, string $track, array $tags): void |
|
47
|
|
|
{ |
|
48
|
|
|
$count = \count($tags); |
|
49
|
|
|
|
|
50
|
|
|
if (0 === $count) { |
|
51
|
|
|
throw new InvalidArgumentException('No tags given'); |
|
52
|
|
|
} |
|
53
|
|
|
if ($count > 10) { |
|
54
|
|
|
throw new InvalidArgumentException('A maximum of 10 tags is allowed'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
array_filter($tags, static function ($tag) { |
|
58
|
|
|
if (null === $tag || !\is_string($tag)) { |
|
59
|
|
|
throw new InvalidArgumentException(sprintf('Invalid tag given')); |
|
60
|
|
|
} |
|
61
|
|
|
}); |
|
62
|
|
|
|
|
63
|
|
|
$this->client->signedCall('track.addTags', [ |
|
64
|
|
|
'artist' => $artist, |
|
65
|
|
|
'track' => $track, |
|
66
|
|
|
'tags' => implode(',', $tags), |
|
67
|
|
|
], $session, 'POST'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getCorrection(string $artist, string $track): ?Song |
|
74
|
|
|
{ |
|
75
|
|
|
$response = $this->client->unsignedCall('track.getCorrection', [ |
|
76
|
|
|
'artist' => $artist, |
|
77
|
|
|
'track' => $track, |
|
78
|
|
|
]); |
|
79
|
|
|
|
|
80
|
|
|
if (!isset($response['corrections']['correction']['track'])) { |
|
81
|
|
|
return null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return Song::fromApi($response['corrections']['correction']['track']); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getInfo(TrackInfoBuilder $builder): ?SongInfo |
|
91
|
|
|
{ |
|
92
|
|
|
$response = $this->client->unsignedCall('track.getInfo', $builder->getQuery()); |
|
93
|
|
|
|
|
94
|
|
|
if (!isset($response['track'])) { |
|
95
|
|
|
return null; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return SongInfo::fromApi($response['track']); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getSimilar(SimilarTrackBuilder $builder): array |
|
105
|
|
|
{ |
|
106
|
|
|
$response = $this->client->unsignedCall('track.getSimilar', $builder->getQuery()); |
|
107
|
|
|
|
|
108
|
|
|
if (!isset($response['similartracks']['track'])) { |
|
109
|
|
|
return []; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return ApiHelper::mapList( |
|
113
|
|
|
static function ($data) { |
|
114
|
|
|
return SongInfo::fromApi($data); |
|
115
|
|
|
}, |
|
116
|
|
|
$response['similartracks']['track'] |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* {@inheritdoc} |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getTags(TrackTagsBuilder $builder): array |
|
124
|
|
|
{ |
|
125
|
|
|
$response = $this->client->unsignedCall('track.getTags', $builder->getQuery()); |
|
126
|
|
|
|
|
127
|
|
|
if (!isset($response['tags']['tag'])) { |
|
128
|
|
|
return []; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return ApiHelper::mapList( |
|
132
|
|
|
static function ($data) { |
|
133
|
|
|
return Tag::fromApi($data); |
|
134
|
|
|
}, |
|
135
|
|
|
$response['tags']['tag'] |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* {@inheritdoc} |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getTopTags(TrackTopTagsBuilder $builder): array |
|
143
|
|
|
{ |
|
144
|
|
|
$response = $this->client->unsignedCall('track.getTopTags', $builder->getQuery()); |
|
145
|
|
|
|
|
146
|
|
|
if (!isset($response['toptags']['tag'])) { |
|
147
|
|
|
return []; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return ApiHelper::mapList( |
|
151
|
|
|
static function ($data) { |
|
152
|
|
|
return Tag::fromApi($data); |
|
153
|
|
|
}, |
|
154
|
|
|
$response['toptags']['tag'] |
|
155
|
|
|
); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* {@inheritdoc} |
|
160
|
|
|
*/ |
|
161
|
|
|
public function love(SessionInterface $session, string $artist, string $track): void |
|
162
|
|
|
{ |
|
163
|
|
|
$this->client->signedCall('track.love', [ |
|
164
|
|
|
'artist' => $artist, |
|
165
|
|
|
'track' => $track, |
|
166
|
|
|
], $session, 'POST'); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* {@inheritdoc} |
|
171
|
|
|
*/ |
|
172
|
|
|
public function removeTag(SessionInterface $session, string $artist, string $track, string $tag): void |
|
173
|
|
|
{ |
|
174
|
|
|
$this->client->signedCall('track.removeTag', [ |
|
175
|
|
|
'artist' => $artist, |
|
176
|
|
|
'track' => $track, |
|
177
|
|
|
'tag' => $tag, |
|
178
|
|
|
], $session, 'POST'); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* {@inheritdoc} |
|
183
|
|
|
*/ |
|
184
|
|
|
public function scrobble(SessionInterface $session, ScrobbeBuilder $builder): void |
|
185
|
|
|
{ |
|
186
|
|
|
$count = $builder->count(); |
|
187
|
|
|
|
|
188
|
|
|
if (0 === $count) { |
|
189
|
|
|
return; |
|
190
|
|
|
} |
|
191
|
|
|
if ($count > 10) { |
|
192
|
|
|
throw new InvalidArgumentException('A maximum of 50 tracks is allowed'); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$this->client->signedCall('album.scrobble', $builder->getQuery(), $session, 'POST'); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* {@inheritdoc} |
|
200
|
|
|
*/ |
|
201
|
|
|
public function search(string $track, int $limit = 50, int $page = 1): array |
|
202
|
|
|
{ |
|
203
|
|
|
$response = $this->client->unsignedCall('track.search', [ |
|
204
|
|
|
'track' => $track, |
|
205
|
|
|
'limit' => $limit, |
|
206
|
|
|
'page' => $page, |
|
207
|
|
|
]); |
|
208
|
|
|
|
|
209
|
|
|
if (!isset($response['results']['trackmatches']['track'])) { |
|
210
|
|
|
return []; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return ApiHelper::mapList( |
|
214
|
|
|
static function ($data) { |
|
215
|
|
|
return SongInfo::fromApi($data); |
|
216
|
|
|
}, |
|
217
|
|
|
$response['results']['trackmatches']['track'] |
|
218
|
|
|
); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* {@inheritdoc} |
|
223
|
|
|
*/ |
|
224
|
|
|
public function unlove(SessionInterface $session, string $artist, string $track): void |
|
225
|
|
|
{ |
|
226
|
|
|
$this->client->signedCall('track.love', [ |
|
227
|
|
|
'artist' => $artist, |
|
228
|
|
|
'track' => $track, |
|
229
|
|
|
], $session, 'POST'); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* {@inheritdoc} |
|
234
|
|
|
*/ |
|
235
|
|
|
public function updateNowPlaying(SessionInterface $session, NowPlaying $nowPlaying): void |
|
236
|
|
|
{ |
|
237
|
|
|
$this->client->signedCall('track.updateNowPlaying', $nowPlaying->toArray(), $session, 'POST'); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.