diff --git a/src/SoundCloudAPI.php b/src/SoundCloudAPI.php index 67347d5..969c362 100644 --- a/src/SoundCloudAPI.php +++ b/src/SoundCloudAPI.php @@ -15,7 +15,7 @@ class SoundCloudAPI /** * @param SoundCloudClient $client */ - public function __construct(private SoundCloudClient $client) + public function __construct(readonly private SoundCloudClient $client) { } @@ -45,7 +45,7 @@ public function setClientId(string $clientId): void * * @throws SoundCloudAPIException */ - public function getUser(?int $userId = null) + public function getUser(?int $userId = null): object|array { $url = 'me'; @@ -67,7 +67,7 @@ public function getUser(?int $userId = null) * * @throws SoundCloudAPIException */ - public function getTrack(int $trackId, ?string $secretToken = null) + public function getTrack(int $trackId, ?string $secretToken = null): object|array { $url = sprintf('tracks/%d', $trackId); @@ -88,9 +88,9 @@ public function getTrack(int $trackId, ?string $secretToken = null) * * @throws SoundCloudAPIException */ - public function getTracks(?int $userId = null) + public function getTracks(?int $userId = null): object|array { - $url = sprintf('me/tracks'); + $url = 'me/tracks'; if (null !== $userId) { $url = sprintf('users/%d/tracks', $userId); @@ -106,7 +106,7 @@ public function getTracks(?int $userId = null) * * @throws SoundCloudAPIException */ - public function getStreamUrlsForTrack(int $trackId) + public function getStreamUrlsForTrack(int $trackId): object|array { $url = sprintf('tracks/%d/streams', $trackId); @@ -120,7 +120,7 @@ public function getStreamUrlsForTrack(int $trackId) * * @throws SoundCloudAPIException */ - public function repostTrack(int $trackId) + public function repostTrack(int $trackId): object|array { $url = sprintf('e1/me/track_reposts/%d', $trackId); @@ -134,7 +134,7 @@ public function repostTrack(int $trackId) * * @throws SoundCloudAPIException */ - public function likeTrack(int $trackId) + public function likeTrack(int $trackId): object|array { $url = sprintf('e1/me/track_likes/%d', $trackId); @@ -149,7 +149,7 @@ public function likeTrack(int $trackId) * * @throws SoundCloudAPIException */ - public function commentOnTrack(int $trackId, string $comment) + public function commentOnTrack(int $trackId, string $comment): object|array { $url = sprintf('tracks/%d/comments', $trackId); $data = [ @@ -170,7 +170,7 @@ public function commentOnTrack(int $trackId, string $comment) * * @throws SoundCloudAPIException */ - public function followUser(int $userId) + public function followUser(int $userId): object|array { $url = sprintf('me/followings/%d', $userId); @@ -187,7 +187,7 @@ public function followUser(int $userId) * * @throws SoundCloudAPIException */ - public function unFollowUser(int $userId) + public function unFollowUser(int $userId): object|array { $url = sprintf('me/followings/%d', $userId); @@ -202,7 +202,7 @@ public function unFollowUser(int $userId) * * @throws SoundCloudAPIException */ - public function getFollowings() + public function getFollowings(): object|array { return $this->client->apiRequest('GET', 'me/followings'); } @@ -217,13 +217,61 @@ public function getFollowings() * * @throws SoundCloudAPIException */ - public function resolveUrl(string $url) + public function resolveUrl(string $url): object|array { $url = sprintf('resolve?url=%s', $url); return $this->client->apiRequest('GET', $url); } + /** + * Search for tracks + * + * @param string $query + * + * @return object|array + * + * @throws SoundCloudAPIException + */ + public function searchTracks(string $query): object|array + { + $searchUrl = sprintf('tracks?q=%s', urlencode($query)); + + return $this->client->apiRequest('GET', $searchUrl); + } + + /** + * Search for playlists + * + * @param string $query + * + * @return object|array + * + * @throws SoundCloudAPIException + */ + public function searchPlaylists(string $query): object|array + { + $searchUrl = sprintf('playlists?q=%s', urlencode($query)); + + return $this->client->apiRequest('GET', $searchUrl); + } + + /** + * Search for users + * + * @param string $query + * + * @return object|array + * + * @throws SoundCloudAPIException + */ + public function searchUsers(string $query): object|array + { + $searchUrl = sprintf('users?q=%s', urlencode($query)); + + return $this->client->apiRequest('GET', $searchUrl); + } + /** * @param int $trackId * @@ -249,7 +297,7 @@ public function getStreamUrl(int $trackId): ?string * * @throws SoundCloudAPIException */ - public function authenticate(string $clientSecret) + public function authenticate(string $clientSecret): object|array { $bodyData = sprintf( 'client_id=%s&client_secret=%s&grant_type=client_credentials', @@ -275,7 +323,7 @@ public function authenticate(string $clientSecret) * * @throws SoundCloudAPIException */ - public function refreshToken(string $clientSecret, string $refreshToken) + public function refreshToken(string $clientSecret, string $refreshToken): object|array { $bodyData = sprintf( 'client_id=%s&client_secret=%s&grant_type=refresh_token&refresh_token=%s', @@ -304,7 +352,7 @@ public function refreshToken(string $clientSecret, string $refreshToken) * * @throws SoundCloudAPIException */ - public function uploadTrack(string $title, string $trackFilePath, string $description = '', string $artworkFilePath = '') + public function uploadTrack(string $title, string $trackFilePath, string $description = '', string $artworkFilePath = ''): object|array { if (!is_file($trackFilePath)) { throw new SoundCloudAPIException(sprintf('The file \'%s\' could not be found', $trackFilePath)); @@ -343,7 +391,7 @@ public function uploadTrack(string $title, string $trackFilePath, string $descri * * @throws SoundCloudAPIException */ - public function deleteTrack(int $trackId) + public function deleteTrack(int $trackId): object|array { $url = sprintf('tracks/%d', $trackId); diff --git a/src/SoundCloudClient.php b/src/SoundCloudClient.php index a0fe0dd..2abaf1b 100644 --- a/src/SoundCloudClient.php +++ b/src/SoundCloudClient.php @@ -27,27 +27,27 @@ class SoundCloudClient /** * @var HttpClientInterface */ - private $httpClient; + private HttpClientInterface $httpClient; /** * @var string */ - private $accessToken = ''; + private string $accessToken = ''; /** * @var string */ - private $clientId = ''; + private string $clientId = ''; /** * @var int */ - protected $lastHttpStatusCode = 0; + protected int $lastHttpStatusCode = 0; /** - * @var + * @var int */ - protected $responseType = self::RETURN_AS_OBJECT; + protected int $responseType = self::RETURN_AS_OBJECT; /** * SoundCloudClient constructor. @@ -127,16 +127,16 @@ public function setResponseType(int $responseType): SoundCloudClient } /** - * @param string $method - * @param string $service - * @param array $headers - * @param array|string|resource|\Traversable|\Closure $body + * @param string $method + * @param string $service + * @param array $headers + * @param mixed $body * * @return array|object * * @throws SoundCloudAPIException */ - public function apiRequest($method, $service, array $headers = [], $body = null) + public function apiRequest(string $method, string $service, array $headers = [], mixed $body = null): object|array { $url = sprintf( '%s/%s', @@ -175,16 +175,16 @@ public function apiRequest($method, $service, array $headers = [], $body = null) } /** - * @param string $method - * @param string $service - * @param array $headers - * @param array|string|resource|\Traversable|\Closure $body + * @param string $method + * @param string $service + * @param array $headers + * @param mixed $body * * @return array|object * * @throws SoundCloudAPIException */ - public function urlRequest(string $method, string $service, array $headers = [], $body = null): ?string + public function urlRequest(string $method, string $service, array $headers = [], mixed $body = null): ?string { $url = sprintf( '%s/%s', diff --git a/tests/SoundCloudAPITest.php b/tests/SoundCloudAPITest.php index e9c2a18..202dd36 100644 --- a/tests/SoundCloudAPITest.php +++ b/tests/SoundCloudAPITest.php @@ -66,10 +66,10 @@ public function testGetUser(): void ->withConsecutive( ['GET', 'me'], ['GET', 'users/1234'] - )->willReturn('{"OK"}'); + )->willReturn(['OK']); - self::assertEquals('{"OK"}', $this->api->getUser()); - self::assertEquals('{"OK"}', $this->api->getUser(1234)); + self::assertEquals(['OK'], $this->api->getUser()); + self::assertEquals(['OK'], $this->api->getUser(1234)); } /** @@ -80,9 +80,9 @@ public function testGetTrack(): void $this->client->expects(static::once()) ->method('apiRequest') ->with('GET', 'tracks/123') - ->willReturn('{"OK"}'); + ->willReturn(['OK']); - self::assertEquals('{"OK"}', $this->api->getTrack(123)); + self::assertEquals(['OK'], $this->api->getTrack(123)); } /** @@ -93,9 +93,9 @@ public function testDeleteTrack(): void $this->client->expects(static::once()) ->method('apiRequest') ->with('DELETE', 'tracks/789') - ->willReturn('{"OK"}'); + ->willReturn(['OK']); - self::assertEquals('{"OK"}', $this->api->deleteTrack(789)); + self::assertEquals(['OK'], $this->api->deleteTrack(789)); } /** @@ -106,9 +106,9 @@ public function testGetStreamUrlsForTrack(): void $this->client->expects(static::once()) ->method('apiRequest') ->with('GET', 'tracks/555/streams') - ->willReturn('{"OK"}'); + ->willReturn(['OK']); - self::assertEquals('{"OK"}', $this->api->getStreamUrlsForTrack(555)); + self::assertEquals(['OK'], $this->api->getStreamUrlsForTrack(555)); } /** @@ -119,9 +119,9 @@ public function testGetTrackSecretToken(): void $this->client->expects(static::once()) ->method('apiRequest') ->with('GET', 'tracks/456?secret_token=unittest') - ->willReturn('{"OK"}'); + ->willReturn(['OK']); - self::assertEquals('{"OK"}', $this->api->getTrack(456, 'unittest')); + self::assertEquals(['OK'], $this->api->getTrack(456, 'unittest')); } /** @@ -134,10 +134,10 @@ public function testGetTracks(): void ->withConsecutive( ['GET', 'me/tracks'], ['GET', 'users/4321/tracks'] - )->willReturn('{"OK"}'); + )->willReturn(['OK']); - self::assertEquals('{"OK"}', $this->api->getTracks()); - self::assertEquals('{"OK"}', $this->api->getTracks(4321)); + self::assertEquals(['OK'], $this->api->getTracks()); + self::assertEquals(['OK'], $this->api->getTracks(4321)); } /** @@ -148,9 +148,9 @@ public function testRepostTrack(): void $this->client->expects(static::once()) ->method('apiRequest') ->with('PUT', 'e1/me/track_reposts/1337') - ->willReturn('{}'); + ->willReturn([]); - self::assertEquals('{}', $this->api->repostTrack(1337)); + self::assertEquals([], $this->api->repostTrack(1337)); } /** @@ -161,9 +161,9 @@ public function testLikeTrack(): void $this->client->expects(static::once()) ->method('apiRequest') ->with('PUT', 'e1/me/track_likes/555') - ->willReturn('{}'); + ->willReturn([]); - self::assertEquals('{}', $this->api->likeTrack(555)); + self::assertEquals([], $this->api->likeTrack(555)); } /** @@ -178,9 +178,9 @@ public function testCommentOnTrack(): void 'tracks/123/comments', [], ['comment[body]' => 'Test', 'comment[timestamp]' => 0] - )->willReturn('{}'); + )->willReturn([]); - self::assertEquals('{}', $this->api->commentOnTrack(123, 'Test')); + self::assertEquals([], $this->api->commentOnTrack(123, 'Test')); } /** @@ -191,9 +191,9 @@ public function testFollowUser(): void $this->client->expects(static::once()) ->method('apiRequest') ->with('PUT', 'me/followings/222') - ->willReturn('{}'); + ->willReturn([]); - self::assertEquals('{}', $this->api->followUser(222)); + self::assertEquals([], $this->api->followUser(222)); } /** @@ -204,9 +204,9 @@ public function testUnFollowUser(): void $this->client->expects(static::once()) ->method('apiRequest') ->with('DELETE', 'me/followings/333') - ->willReturn('{}'); + ->willReturn([]); - self::assertEquals('{}', $this->api->unFollowUser(333)); + self::assertEquals([], $this->api->unFollowUser(333)); } /** @@ -217,9 +217,9 @@ public function testFollowings(): void $this->client->expects(static::once()) ->method('apiRequest') ->with('GET', 'me/followings') - ->willReturn('{}'); + ->willReturn([]); - self::assertEquals('{}', $this->api->getFollowings()); + self::assertEquals([], $this->api->getFollowings()); } /** @@ -230,9 +230,48 @@ public function testResolveUrl(): void $this->client->expects(static::once()) ->method('apiRequest') ->with('GET', 'resolve?url=https://soundcloud.com/test') - ->willReturn('{}'); + ->willReturn([]); - self::assertEquals('{}', $this->api->resolveUrl('https://soundcloud.com/test')); + self::assertEquals([], $this->api->resolveUrl('https://soundcloud.com/test')); + } + + /** + * @throws SoundCloudAPIException + */ + public function testSearchTracks(): void + { + $this->client->expects(static::once()) + ->method('apiRequest') + ->with('GET', 'tracks?q=searchquery') + ->willReturn([]); + + self::assertIsArray($this->api->searchTracks('searchquery')); + } + + /** + * @throws SoundCloudAPIException + */ + public function testSearchPlaylists(): void + { + $this->client->expects(static::once()) + ->method('apiRequest') + ->with('GET', 'playlists?q=searchquery') + ->willReturn([]); + + self::assertIsArray($this->api->searchPlaylists('searchquery')); + } + + /** + * @throws SoundCloudAPIException + */ + public function testSearchUsers(): void + { + $this->client->expects(static::once()) + ->method('apiRequest') + ->with('GET', 'users?q=searchquery') + ->willReturn([]); + + self::assertIsArray($this->api->searchUsers('searchquery')); } /** @@ -247,9 +286,9 @@ public function testAuthenticate(): void 'oauth2/token', ['Content-Type' => 'application/x-www-form-urlencoded'], 'client_id=&client_secret=secret&grant_type=client_credentials' - )->willReturn('{}'); + )->willReturn([]); - self::assertEquals('{}', $this->api->authenticate('secret')); + self::assertEquals([], $this->api->authenticate('secret')); } /** @@ -264,8 +303,8 @@ public function testRefreshToken(): void 'oauth2/token', ['Content-Type' => 'application/x-www-form-urlencoded'], 'client_id=&client_secret=secret&grant_type=refresh_token&refresh_token=refresh' - )->willReturn('{}'); + )->willReturn([]); - self::assertEquals('{}', $this->api->refreshToken('secret', 'refresh')); + self::assertEquals([], $this->api->refreshToken('secret', 'refresh')); } }