| 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\Builder\ScrobbeBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Core23\LastFm\Builder\SimilarTrackBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Core23\LastFm\Builder\TrackInfoBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Core23\LastFm\Builder\TrackTagsBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Core23\LastFm\Builder\TrackTopTagsBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Core23\LastFm\Client\ApiClientInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Core23\LastFm\Model\NowPlaying; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Core23\LastFm\Model\Song; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Core23\LastFm\Model\SongInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Core23\LastFm\Model\Tag; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Core23\LastFm\Session\SessionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Core23\LastFm\Util\ApiHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         $response = $this->client->unsignedCall('track.getCorrection', [ | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             'artist' => $artist, | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             'track'  => $track, | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         if (!isset($response['corrections']['correction']['track'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         return Song::fromApi($response['corrections']['correction']['track']); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $this->client->signedCall('track.love', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             'artist' => $artist, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             'track'  => $track, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         ], $session, 'POST'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         ], $session, 'POST'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     public function scrobble(SessionInterface $session, ScrobbeBuilder $builder): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $count = $builder->count(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         if (0 === $count) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         if ($count > 10) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             throw new InvalidArgumentException('A maximum of 50 tracks is allowed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         $this->client->signedCall('album.scrobble', $builder->getQuery(), $session, 'POST'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     public function search(string $track, int $limit = 50, int $page = 1): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         $response = $this->client->unsignedCall('track.search', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             'track' => $track, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             'limit' => $limit, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             'page'  => $page, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         if (!isset($response['results']['trackmatches']['track'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         return ApiHelper::mapList( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |             static function ($data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |                 return SongInfo::fromApi($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             $response['results']['trackmatches']['track'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     public function unlove(SessionInterface $session, string $artist, string $track): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         $this->client->signedCall('track.love', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |             'artist' => $artist, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |             'track'  => $track, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         ], $session, 'POST'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     public function updateNowPlaying(SessionInterface $session, NowPlaying $nowPlaying): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         $this->client->signedCall('track.updateNowPlaying', $nowPlaying->toArray(), $session, 'POST'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 200 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 201 |  |  |  |