1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HansOtt\Lastify\Services; |
4
|
|
|
|
5
|
|
|
use Dandelionmood\LastFm\LastFm as LastFmWrapper; |
6
|
|
|
use HansOtt\Lastify\TrackInfo; |
7
|
|
|
use HansOtt\Lastify\TrackCollection; |
8
|
|
|
use HansOtt\Lastify\TrackInfo\Artist; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
final class LastFm |
12
|
|
|
{ |
13
|
|
|
private $api; |
14
|
|
|
|
15
|
|
|
public function __construct(LastFmWrapper $api) |
16
|
|
|
{ |
17
|
|
|
$this->api = $api; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public static function connect($apiKey) |
21
|
|
|
{ |
22
|
|
|
$api = new LastFmWrapper($apiKey); |
23
|
|
|
|
24
|
|
|
return new static($api); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
View Code Duplication |
private function fetchTopTracks($username, $limit) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$params = [ |
30
|
|
|
'user' => $username, |
31
|
|
|
'limit' => $limit, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
$response = $this->api->user_getTopTracks($params); |
35
|
|
|
|
36
|
|
|
$topTracks = isset($response->toptracks) && is_array($response->toptracks->track) |
37
|
|
|
? $response->toptracks->track |
38
|
|
|
: []; |
39
|
|
|
|
40
|
|
|
return $topTracks; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private static function convertToTrackCollection(array $tracks) |
44
|
|
|
{ |
45
|
|
|
$convertTrack = function ($track) { |
46
|
|
|
return static::convertToTrack($track); |
|
|
|
|
47
|
|
|
}; |
48
|
|
|
|
49
|
|
|
$tracks = array_map($convertTrack, $tracks); |
50
|
|
|
|
51
|
|
|
return new TrackCollection($tracks); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private static function convertToTrack($track) |
55
|
|
|
{ |
56
|
|
|
$artist = new Artist($track->artist->name); |
57
|
|
|
|
58
|
|
|
return new TrackInfo($track->name, [$artist]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
private function assertValidUsername($username) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
if (!is_string($username)) { |
64
|
|
|
throw new InvalidArgumentException('The username should be a string, instead got:' . gettype($username)); |
65
|
|
|
} |
66
|
|
|
if (empty($username)) { |
67
|
|
|
throw new InvalidArgumentException('The username cannot be empty'); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getTopTracks($username, $limit = 50) |
72
|
|
|
{ |
73
|
|
|
$this->assertValidUsername($username); |
74
|
|
|
$topTracks = $this->fetchTopTracks($username, $limit); |
75
|
|
|
$topTracks = static::convertToTrackCollection($topTracks); |
|
|
|
|
76
|
|
|
|
77
|
|
|
return $topTracks; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getLovedTracks($username, $limit = 50) |
81
|
|
|
{ |
82
|
|
|
$lovedTracks = $this->fetchLovedTracks($username, $limit); |
83
|
|
|
$lovedTracks = static::convertToTrackCollection($lovedTracks); |
|
|
|
|
84
|
|
|
|
85
|
|
|
return $lovedTracks; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
private function fetchLovedTracks($username, $limit) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$params = [ |
91
|
|
|
'user' => $username, |
92
|
|
|
'limit' => $limit, |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
$response = $this->api->user_getLovedTracks($params); |
96
|
|
|
|
97
|
|
|
$lovedTracks = isset($response->lovedtracks) && is_array($response->lovedtracks->track) |
98
|
|
|
? $response->lovedtracks->track |
99
|
|
|
: []; |
100
|
|
|
|
101
|
|
|
return $lovedTracks; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.