| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Services; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Models\Song; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use GuzzleHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Log; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class Musixmatch extends RESTfulService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * Construct an instance of Musixmatch service. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @param string      $key    The Musixmatch API key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @param Client|null $client The Guzzle HTTP client | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 17 |  | View Code Duplication |     public function __construct($key = null, Client $client = null) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |         parent::__construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |             $key ?: config('koel.musixmatch.key'), | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |             null, | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |             'https://api.musixmatch.com/ws/1.1', | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |             $client ?: new Client() | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * Determine if our application is using Musixmatch. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function enabled() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         return (bool) config('koel.musixmatch.key'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param App\Models\Song $song | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return mixed|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function searchLyricsRelatedToSong(Song $song) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if($song->hasLyrics() == false) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             if (!$song->artist->isUnknown() && !$song->artist->isVarious()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 $lyrics = $this->search($song->title, $song->artist->name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 $lyrics = $this->search($song->title); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |              | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             return $song->updateSingle($song->title, $song->album->name, $song->artist->name, $lyrics, $song->track, (int) $song->compilationState); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param string $artistName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return mixed|false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function search(string $name, string $artistName = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         if (!$this->enabled()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $uri = sprintf('matcher.lyrics.get?format=jsonp&callback=callback&q_track=%s&q_artist=%s&apikey=%s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             urlencode($name), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             urlencode($artistName), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $this->key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         try{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $response = $this->jsonpDecode($this->get($uri)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |              | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             return $this->getLyrics($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         }catch(\Exception $e){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             Log::error($e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @param string $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     private function jsonpDecode($response) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         return json_decode(substr(preg_replace("/[^(]*\((.*)\)/","$1",$response), 0, -1)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param JSON $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     private function getLyrics($response) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $lyrics = $response->message->body->lyrics->lyrics_body; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         if(strlen($lyrics)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             // Correct Musixmatch spelling mistake (is for are). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             return substr($lyrics, 0, strpos($lyrics, "******* This Lyrics is NOT for Commercial use *******")). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 "*** This Lyrics are NOT for Commercial use ***"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  | } | 
            
                        
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.