| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace SpotifyApiConnect\Application\SpotifyWebApiPhp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SpotifyApiConnect\Domain\DataTransferObject\DeleteTrackInfoDataProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SpotifyApiConnect\Domain\DataTransferObject\PlaylistDataProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SpotifyApiConnect\Domain\DataTransferObject\PlaylistTracksDataProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SpotifyApiConnect\Domain\DataTransferObject\TrackSearchRequestDataProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SpotifyApiConnect\Domain\DataTransferObject\TracksSearchDataProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SpotifyApiConnect\Domain\DataTransferObject\UserPlaylistsDataProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SpotifyApiConnect\Domain\Exception\PlaylistNotFound; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SpotifyWebAPI\SpotifyWebAPI as BaseSpotifyWebAPI; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use SpotifyWebAPI\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class SpotifyWebApi implements SpotifyWebApiInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var BaseSpotifyWebAPI | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $baseSpotifyWebAPI; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param string $accessToken | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function __construct(string $accessToken) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->baseSpotifyWebAPI = new BaseSpotifyWebAPI(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->baseSpotifyWebAPI->setAccessToken($accessToken); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->baseSpotifyWebAPI->setReturnType(Request::RETURN_ASSOC); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param $playlistId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param array $tracks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function addPlaylistTracks($playlistId, array $tracks, array $options = []): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         return $this->baseSpotifyWebAPI->addPlaylistTracks($playlistId, $tracks, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param string $playlistId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param DeleteTrackInfoDataProvider[] $tracksInfo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function deletePlaylistTracks(string $playlistId, array $tracksInfo): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $tracksToDelete = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         foreach ($tracksInfo as $deleteTrackInfoDataProvider) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             if (!$deleteTrackInfoDataProvider instanceof DeleteTrackInfoDataProvider) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 throw new \RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                     sprintf('tracksInfo should be instanceof class: %s', DeleteTrackInfoDataProvider::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $tracksToDelete[] = $deleteTrackInfoDataProvider->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->baseSpotifyWebAPI->setReturnType(Request::RETURN_OBJECT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $delete = (bool)$this->baseSpotifyWebAPI->deletePlaylistTracks($playlistId, $tracksToDelete); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $this->baseSpotifyWebAPI->setReturnType(Request::RETURN_ASSOC); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         return $delete; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param string $playlistId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @return PlaylistDataProvider | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     public function getPlaylist(string $playlistId, array $options = []): PlaylistDataProvider | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         $jsonObjectResult = $this->baseSpotifyWebAPI->getPlaylist($playlistId, $options); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         $playlistDataProvider = new PlaylistDataProvider(); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         $playlistDataProvider->fromArray($jsonObjectResult); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         return $playlistDataProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param string $userId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param string $playlistName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @return PlaylistDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @throws PlaylistNotFound | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function getUserPlaylistByName(string $userId, string $playlistName, array $options = []): PlaylistDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $userPlaylistsDataProvider = $this->getUserPlaylists($userId, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $playlists = $userPlaylistsDataProvider->getItems(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         foreach ($playlists as $playlist) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             if (trim($playlist->getName()) === trim($playlistName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 return $playlist; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         throw new PlaylistNotFound(sprintf('Playlist "%s" not found', $playlistName)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param string $playlistId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @return PlaylistTracksDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     public function getPlaylistTracks(string $playlistId, array $options = []): PlaylistTracksDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $jsonObjectResult = $this->baseSpotifyWebAPI->getPlaylistTracks($playlistId, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $playlistTracksDataProvider = new PlaylistTracksDataProvider(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $playlistTracksDataProvider->fromArray($jsonObjectResult); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         return $playlistTracksDataProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @param TrackSearchRequestDataProvider $trackSearchRequest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @return TracksSearchDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     public function searchTrack(TrackSearchRequestDataProvider $trackSearchRequest, array $options = []): TracksSearchDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $jsonObjectResult = $this->search( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 'track:%s artist:%s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 $trackSearchRequest->getTrack(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 $trackSearchRequest->getArtist() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             ), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             $trackSearchRequest->getType() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $tracksSearchDataProvider = new TracksSearchDataProvider(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $tracksSearchDataProvider->fromArray($jsonObjectResult[$trackSearchRequest->getResultType()]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         return $tracksSearchDataProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @param string $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @param array $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     private function search(string $query, array $type, array $options = []): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         return (array)$this->baseSpotifyWebAPI->search($query, $type, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * @param string $userId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * @return UserPlaylistsDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     private function getUserPlaylists(string $userId, array $options = []): UserPlaylistsDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $userPlaylistInfo = (array)$this->baseSpotifyWebAPI->getUserPlaylists($userId, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         $userPlaylistsDataProvider = new UserPlaylistsDataProvider(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         $userPlaylistsDataProvider->fromArray($userPlaylistInfo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         return $userPlaylistsDataProvider; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 167 |  |  | } |