Skip to content

Commit

Permalink
Merge pull request #3 from Martin1982/add-search
Browse files Browse the repository at this point in the history
Add search and apply strong types
  • Loading branch information
PouleR authored Dec 6, 2022
2 parents 8f1f105 + e23432f commit 3001df2
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 65 deletions.
82 changes: 65 additions & 17 deletions src/SoundCloudAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SoundCloudAPI
/**
* @param SoundCloudClient $client
*/
public function __construct(private SoundCloudClient $client)
public function __construct(readonly private SoundCloudClient $client)
{
}

Expand Down Expand Up @@ -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';

Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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 = [
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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');
}
Expand All @@ -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
*
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);

Expand Down
32 changes: 16 additions & 16 deletions src/SoundCloudClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
Loading

0 comments on commit 3001df2

Please sign in to comment.