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\Artist; |
16
|
|
|
use Core23\LastFm\Model\Song; |
17
|
|
|
use Core23\LastFm\Util\ApiHelper; |
18
|
|
|
|
19
|
|
|
final class GeoService implements GeoServiceInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ApiClientInterface |
23
|
|
|
*/ |
24
|
|
|
private $client; |
25
|
|
|
|
26
|
|
|
public function __construct(ApiClientInterface $client) |
27
|
|
|
{ |
28
|
|
|
$this->client = $client; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
public function getTopArtists(string $country, int $limit = 50, int $page = 1): array |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$response = $this->client->unsignedCall('geo.getTopArtists', [ |
34
|
|
|
'country' => $country, |
35
|
|
|
'limit' => $limit, |
36
|
|
|
'page' => $page, |
37
|
|
|
]); |
38
|
|
|
|
39
|
|
|
if (!isset($response['topartists']['artist'])) { |
40
|
|
|
return []; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return ApiHelper::mapList( |
44
|
|
|
static function ($data) { |
45
|
|
|
return Artist::fromApi($data); |
46
|
|
|
}, |
47
|
|
|
$response['topartists']['artist'] |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getTopTracks(string $country, string $location = null, $limit = 50, int $page = 1): array |
52
|
|
|
{ |
53
|
|
|
$response = $this->client->unsignedCall('geo.getTopTracks', [ |
54
|
|
|
'country' => $country, |
55
|
|
|
'location' => $location, |
56
|
|
|
'limit' => $limit, |
57
|
|
|
'page' => $page, |
58
|
|
|
]); |
59
|
|
|
|
60
|
|
|
if (!isset($response['tracks']['track'])) { |
61
|
|
|
return []; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return ApiHelper::mapList( |
65
|
|
|
static function ($data) { |
66
|
|
|
return Song::fromApi($data); |
67
|
|
|
}, |
68
|
|
|
$response['tracks']['track'] |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
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.