1 | <?php |
||
30 | final class ArtistService implements ArtistServiceInterface |
||
31 | { |
||
32 | /** |
||
33 | * @var ApiClientInterface |
||
34 | */ |
||
35 | private $client; |
||
36 | |||
37 | public function __construct(ApiClientInterface $client) |
||
38 | { |
||
39 | $this->client = $client; |
||
40 | } |
||
41 | |||
42 | public function addTags(SessionInterface $session, string $artist, array $tags): void |
||
43 | { |
||
44 | $count = \count($tags); |
||
45 | |||
46 | if (0 === $count) { |
||
47 | throw new InvalidArgumentException('No tags given'); |
||
48 | } |
||
49 | if ($count > 10) { |
||
50 | throw new InvalidArgumentException('A maximum of 10 tags is allowed'); |
||
51 | } |
||
52 | |||
53 | array_filter($tags, static function ($tag) { |
||
54 | if (null === $tag || !\is_string($tag)) { |
||
55 | throw new InvalidArgumentException(sprintf('Invalid tag given')); |
||
56 | } |
||
57 | }); |
||
58 | |||
59 | $this->client->signedCall('artist.addTags', [ |
||
60 | 'artist' => $artist, |
||
61 | 'tags' => implode(',', $tags), |
||
62 | ], $session, 'POST'); |
||
63 | } |
||
64 | |||
65 | public function getCorrection(string $artist): ?Artist |
||
66 | { |
||
67 | $response = $this->client->unsignedCall('artist.getCorrection', [ |
||
68 | 'artist' => $artist, |
||
69 | ]); |
||
70 | |||
71 | if (!isset($response['corrections']['correction']['artist'])) { |
||
72 | return null; |
||
73 | } |
||
74 | |||
75 | return Artist::fromApi($response['corrections']['correction']['artist']); |
||
76 | } |
||
77 | |||
78 | public function getInfo(ArtistInfoBuilder $builder): ?ArtistInfo |
||
79 | { |
||
80 | $response = $this->client->unsignedCall('artist.getInfo', $builder->getQuery()); |
||
81 | |||
82 | if (!isset($response['artist'])) { |
||
83 | return null; |
||
84 | } |
||
85 | |||
86 | return ArtistInfo::fromApi($response['artist']); |
||
87 | } |
||
88 | |||
89 | public function getSimilar(SimilarArtistBuilder $builder): array |
||
90 | { |
||
91 | $response = $this->client->unsignedCall('artist.getSimilar', $builder->getQuery()); |
||
92 | |||
93 | if (!isset($response['similarartists']['artist'])) { |
||
94 | return []; |
||
95 | } |
||
96 | |||
97 | return ApiHelper::mapList( |
||
98 | static function ($data) { |
||
99 | return Artist::fromApi($data); |
||
100 | }, |
||
101 | $response['similarartists']['artist'] |
||
102 | ); |
||
103 | } |
||
104 | |||
105 | public function getTags(ArtistTagsBuilder $builder): array |
||
106 | { |
||
107 | $response = $this->client->unsignedCall('artist.getTags', $builder->getQuery()); |
||
108 | |||
109 | if (!isset($response['tags']['tag'])) { |
||
110 | return []; |
||
111 | } |
||
112 | |||
113 | return ApiHelper::mapList( |
||
114 | static function ($data) { |
||
115 | return Tag::fromApi($data); |
||
116 | }, |
||
117 | $response['tags']['tag'] |
||
118 | ); |
||
119 | } |
||
120 | |||
121 | public function getTopAlbums(ArtistTopAlbumsBuilder $builder): array |
||
122 | { |
||
123 | $response = $this->client->unsignedCall('artist.getTopAlbums', $builder->getQuery()); |
||
124 | |||
125 | if (!isset($response['topalbums']['album'])) { |
||
126 | return []; |
||
127 | } |
||
128 | |||
129 | return ApiHelper::mapList( |
||
130 | static function ($data) { |
||
131 | return Album::fromApi($data); |
||
132 | }, |
||
133 | $response['topalbums']['album'] |
||
134 | ); |
||
135 | } |
||
136 | |||
137 | public function getTopTags(ArtistTopTagsBuilder $builder): array |
||
138 | { |
||
139 | $response = $this->client->unsignedCall('artist.getTopTags', $builder->getQuery()); |
||
140 | |||
141 | if (!isset($response['toptags']['tag'])) { |
||
142 | return []; |
||
143 | } |
||
144 | |||
145 | return ApiHelper::mapList( |
||
146 | static function ($data) { |
||
147 | return Tag::fromApi($data); |
||
148 | }, |
||
149 | $response['toptags']['tag'] |
||
150 | ); |
||
151 | } |
||
152 | |||
153 | public function getTopTracks(ArtistTopTracksBuilder $builder): array |
||
154 | { |
||
155 | $response = $this->client->unsignedCall('artist.getTopTracks', $builder->getQuery()); |
||
156 | |||
157 | if (!isset($response['toptracks']['track'])) { |
||
158 | return []; |
||
159 | } |
||
160 | |||
161 | return ApiHelper::mapList( |
||
162 | static function ($data) { |
||
163 | return Song::fromApi($data); |
||
164 | }, |
||
165 | $response['toptracks']['track'] |
||
166 | ); |
||
167 | } |
||
168 | |||
169 | public function removeTag(SessionInterface $session, string $artist, string $tag): void |
||
176 | |||
177 | public function search(string $artist, int $limit = 50, int $page = 1): array |
||
178 | { |
||
179 | $response = $this->client->unsignedCall('artist.search', [ |
||
180 | 'artist' => $artist, |
||
181 | 'limit' => $limit, |
||
182 | 'page' => $page, |
||
183 | ]); |
||
184 | |||
185 | if (!isset($response['results']['artistmatches']['artist'])) { |
||
196 | } |
||
197 |