1 | <?php |
||
28 | final class TrackService implements TrackServiceInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var ApiClientInterface |
||
32 | */ |
||
33 | private $client; |
||
34 | |||
35 | public function __construct(ApiClientInterface $client) |
||
36 | { |
||
37 | $this->client = $client; |
||
38 | } |
||
39 | |||
40 | public function addTags(SessionInterface $session, string $artist, string $track, array $tags): void |
||
41 | { |
||
42 | $count = \count($tags); |
||
43 | |||
44 | if (0 === $count) { |
||
45 | throw new InvalidArgumentException('No tags given'); |
||
46 | } |
||
47 | if ($count > 10) { |
||
48 | throw new InvalidArgumentException('A maximum of 10 tags is allowed'); |
||
49 | } |
||
50 | |||
51 | array_filter($tags, static function ($tag) { |
||
52 | if (null === $tag || !\is_string($tag)) { |
||
53 | throw new InvalidArgumentException(sprintf('Invalid tag given')); |
||
54 | } |
||
55 | }); |
||
56 | |||
57 | $this->client->signedCall('track.addTags', [ |
||
58 | 'artist' => $artist, |
||
59 | 'track' => $track, |
||
60 | 'tags' => implode(',', $tags), |
||
61 | ], $session, 'POST'); |
||
62 | } |
||
63 | |||
64 | public function getCorrection(string $artist, string $track): ?Song |
||
77 | |||
78 | public function getInfo(TrackInfoBuilder $builder): ?SongInfo |
||
79 | { |
||
80 | $response = $this->client->unsignedCall('track.getInfo', $builder->getQuery()); |
||
81 | |||
82 | if (!isset($response['track'])) { |
||
83 | return null; |
||
84 | } |
||
85 | |||
86 | return SongInfo::fromApi($response['track']); |
||
87 | } |
||
88 | |||
89 | public function getSimilar(SimilarTrackBuilder $builder): array |
||
90 | { |
||
91 | $response = $this->client->unsignedCall('track.getSimilar', $builder->getQuery()); |
||
92 | |||
93 | if (!isset($response['similartracks']['track'])) { |
||
94 | return []; |
||
95 | } |
||
96 | |||
97 | return ApiHelper::mapList( |
||
98 | static function ($data) { |
||
99 | return SongInfo::fromApi($data); |
||
100 | }, |
||
101 | $response['similartracks']['track'] |
||
102 | ); |
||
103 | } |
||
104 | |||
105 | public function getTags(TrackTagsBuilder $builder): array |
||
106 | { |
||
107 | $response = $this->client->unsignedCall('track.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 getTopTags(TrackTopTagsBuilder $builder): array |
||
122 | { |
||
123 | $response = $this->client->unsignedCall('track.getTopTags', $builder->getQuery()); |
||
124 | |||
125 | if (!isset($response['toptags']['tag'])) { |
||
126 | return []; |
||
127 | } |
||
128 | |||
129 | return ApiHelper::mapList( |
||
130 | static function ($data) { |
||
131 | return Tag::fromApi($data); |
||
132 | }, |
||
133 | $response['toptags']['tag'] |
||
134 | ); |
||
135 | } |
||
136 | |||
137 | public function love(SessionInterface $session, string $artist, string $track): void |
||
144 | |||
145 | public function removeTag(SessionInterface $session, string $artist, string $track, string $tag): void |
||
146 | { |
||
147 | $this->client->signedCall('track.removeTag', [ |
||
148 | 'artist' => $artist, |
||
149 | 'track' => $track, |
||
150 | 'tag' => $tag, |
||
153 | |||
154 | public function scrobble(SessionInterface $session, ScrobbeBuilder $builder): void |
||
167 | |||
168 | public function search(string $track, int $limit = 50, int $page = 1): array |
||
187 | |||
188 | public function unlove(SessionInterface $session, string $artist, string $track): void |
||
195 | |||
196 | public function updateNowPlaying(SessionInterface $session, NowPlaying $nowPlaying): void |
||
200 | } |
||
201 |