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\Client\ApiClientInterface; |
15
|
|
|
use Core23\LastFm\Model\ArtistInfo; |
16
|
|
|
use Core23\LastFm\Model\Song; |
17
|
|
|
use Core23\LastFm\Model\Tag; |
18
|
|
|
use Core23\LastFm\Util\ApiHelper; |
19
|
|
|
|
20
|
|
|
final class ChartService implements ChartServiceInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ApiClientInterface |
24
|
|
|
*/ |
25
|
|
|
private $client; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ApiClientInterface $client |
29
|
|
|
*/ |
30
|
|
|
public function __construct(ApiClientInterface $client) |
31
|
|
|
{ |
32
|
|
|
$this->client = $client; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
public function getTopArtists(int $limit = 50, int $page = 1): array |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$response = $this->client->unsignedCall('chart.getTopArtists', [ |
41
|
|
|
'limit' => $limit, |
42
|
|
|
'page' => $page, |
43
|
|
|
]); |
44
|
|
|
|
45
|
|
|
if (!isset($response['artists']['artist'])) { |
46
|
|
|
return []; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return ApiHelper::mapList( |
50
|
|
|
static function ($data) { |
51
|
|
|
return ArtistInfo::fromApi($data); |
52
|
|
|
}, |
53
|
|
|
$response['artists']['artist'] |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function getTopTags(int $limit = 50, int $page = 1): array |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$response = $this->client->unsignedCall('chart.getTopTags', [ |
63
|
|
|
'limit' => $limit, |
64
|
|
|
'page' => $page, |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
if (!isset($response['tags']['tag'])) { |
68
|
|
|
return []; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return ApiHelper::mapList( |
72
|
|
|
static function ($data) { |
73
|
|
|
return Tag::fromApi($data); |
74
|
|
|
}, |
75
|
|
|
$response['tags']['tag'] |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function getTopTracks(int $limit = 50, int $page = 1): array |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$response = $this->client->unsignedCall('chart.getTopTracks', [ |
85
|
|
|
'limit' => $limit, |
86
|
|
|
'page' => $page, |
87
|
|
|
]); |
88
|
|
|
|
89
|
|
|
if (!isset($response['tracks']['track'])) { |
90
|
|
|
return []; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return ApiHelper::mapList( |
94
|
|
|
static function ($data) { |
95
|
|
|
return Song::fromApi($data); |
96
|
|
|
}, |
97
|
|
|
$response['tracks']['track'] |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.