| 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\AlbumInfoBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Core23\LastFm\Builder\AlbumTagsBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Core23\LastFm\Builder\AlbumTopTagsBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Core23\LastFm\Client\ApiClientInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Core23\LastFm\Model\Album; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Core23\LastFm\Model\AlbumInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Core23\LastFm\Model\Tag; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Core23\LastFm\Session\SessionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Core23\LastFm\Util\ApiHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | final class AlbumService implements AlbumServiceInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @var ApiClientInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     private $client; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function __construct(ApiClientInterface $client) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->client = $client; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function addTags(SessionInterface $session, string $artist, string $album, array $tags): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $count = \count($tags); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         if (0 === $count) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             throw new InvalidArgumentException('No tags given'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if ($count > 10) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             throw new InvalidArgumentException('A maximum of 10 tags is allowed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         array_filter($tags, static function ($tag) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             if (null === $tag || !\is_string($tag)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 throw new InvalidArgumentException(sprintf('Invalid tag given')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->client->signedCall('album.addTags', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             'artist' => $artist, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             'album'  => $album, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             'tags'   => implode(',', $tags), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         ], $session, 'POST'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public function getInfo(AlbumInfoBuilder $builder): AlbumInfo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $response = $this->client->unsignedCall('album.getInfo', $builder->getQuery()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return AlbumInfo::fromApi($response['album']); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 69 |  | View Code Duplication |     public function getTags(AlbumTagsBuilder $builder): array | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $response = $this->client->unsignedCall('album.getTags', $builder->getQuery()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if (!isset($response['tags']['tag'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         return ApiHelper::mapList( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             static function ($data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 return Tag::fromApi($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $response['tags']['tag'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 85 |  | View Code Duplication |     public function getTopTags(AlbumTopTagsBuilder $builder): array | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $response = $this->client->unsignedCall('album.getTopTags', $builder->getQuery()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         if (!isset($response['toptags']['tag'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         return ApiHelper::mapList( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             static function ($data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 return Tag::fromApi($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $response['toptags']['tag'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     public function removeTag(SessionInterface $session, string $artist, string $album, string $tag): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $this->client->signedCall('album.removeTag', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             'artist' => $artist, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             'album'  => $album, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             'tag'    => $tag, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         ], $session, 'POST'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public function search(string $album, int $limit = 50, int $page = 1): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $response = $this->client->unsignedCall('album.search', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             'album' => $album, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             'limit' => $limit, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             'page'  => $page, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         if (!isset($response['results']['albummatches']['album'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         return ApiHelper::mapList( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             static function ($data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 return Album::fromApi($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             $response['results']['albummatches']['album'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 129 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |  | 
            
                        
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.