Skip to content

Commit

Permalink
Merge pull request #2 from FlorianSW/issue/1
Browse files Browse the repository at this point in the history
Provide information about errors, if no userdata could be retrieved
  • Loading branch information
FlorianSW authored May 16, 2017
2 parents c6fc204 + c741e5d commit 51bfc90
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/XenForoBDClient/Users/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class User {
*/
const USERS_BASE_URL_AUTHENTICATED = self::USERS_BASE_URL . '&oauth_token=%s';

/**
* @var array|null An array of error messages, when a request of information failed and error
* messages are provided.
*/
private $errors;

/**
* @var Client The Client to use.
*/
Expand All @@ -29,7 +35,10 @@ public function __construct( Client $client ) {
}

/**
* Returns the infromation of the given user (if the user exists), otherwise retuns false.
* Returns the information of the given user (if the user exists), otherwise returns false.
* If false is returned, it's possible that more information about the error was provided by
* the XenForo API, which then can be retrieved with the getErrors method.
*
* @param string|integer $userId The user ID of the user to request. The value "me" retrieves
* the information of the currently authenticated user, if one is already authenticated.
* @return bool|array
Expand All @@ -46,6 +55,17 @@ public function get( $userId ) {
return $userInfo;
}

/**
* Returns an array of errors if there were some, otherwise an empty array.
* @return array
*/
public function getErrors() {
if ($this->errors === null ) {
return [];
}
return $this->errors;
}

private function fetchUserInfo( $userIdentifier ) {
if ( $this->client->isAuthenticated() ) {
$requestUrl = sprintf(
Expand All @@ -65,10 +85,15 @@ private function fetchUserInfo( $userIdentifier ) {
$httpClient = new \Net_Http_Client();
$httpClient->get( $requestUrl );

$json = json_decode( $httpClient->getBody(), true );
if ( $httpClient->getStatus() !== 200 ) {
if ( isset( $json['errors'] ) ) {
$this->errors = $json['errors'];
}

return false;
}
$json = json_decode( $httpClient->getBody(), true );

return $json;
}
}

0 comments on commit 51bfc90

Please sign in to comment.