Skip to content

Commit

Permalink
Merge pull request #10 from watzenare/develop
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
watzenare committed Dec 16, 2015
2 parents 5a12fd8 + 4567748 commit 30d32b9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
37 changes: 27 additions & 10 deletions PushApi_Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
* updateUserPreference($idUser, $idTheme, $params) /user/$idUser/preference/$idTheme
* deleteUserPreference($idUser, $idTheme) /user/$idUser/preference/$idTheme
* getUserPreferences($idUser) /user/$idUser/preferences
* updateAllUserPreferences($idUser, $params) /user/$idUser/preferences
*
* CHANNEL METHODS:
* getChannel($idChannel) /channel/$idChannel
Expand All @@ -64,7 +65,7 @@
* updateTheme($idTheme, $params) /theme/$idTheme
* deleteTheme($idTheme) /theme/$idTheme
* getThemes($params) /themes
* getThemesByRange($range) /themes/range/$range
* getThemesByRange($range, $params) /themes/range/$range
* getThemeByName($params) /theme_name
*
* SUBJECT METHODS:
Expand Down Expand Up @@ -371,7 +372,7 @@ public function deleteUserDevice($idUser, $idDevice)
* Retrieves information about all registered users.
* @return array Response key => value array
*/
public function getUsers($params)
public function getUsers($params = [])
{
return $this->users(self::GET, $params);
}
Expand Down Expand Up @@ -445,7 +446,7 @@ public function deleteChannel($idChannel)
* Retrieves information about all registered channels.
* @return array Response key => value array
*/
public function getChannels($params)
public function getChannels($params = [])
{
return $this->channels(self::GET, $params);
}
Expand Down Expand Up @@ -509,7 +510,7 @@ public function deleteTheme($idTheme)
* Retrieves information about all registered themes.
* @return array Response key => value array
*/
public function getThemes($params)
public function getThemes($params = [])
{
return $this->themes(self::GET, $params);
}
Expand All @@ -519,7 +520,7 @@ public function getThemes($params)
* @param string $range The range that a theme can have.
* @return array Response key => value array
*/
public function getThemesByRange($range, $params)
public function getThemesByRange($range, $params = [])
{
return $this->themesByRange(self::GET, $range, $params);
}
Expand Down Expand Up @@ -583,7 +584,7 @@ public function deleteSubject($idSubject)
* Retrieves information about all registered subjects.
* @return array Response key => value array
*/
public function getSubjects($params)
public function getSubjects($params = [])
{
return $this->subjects(self::GET, $params);
}
Expand Down Expand Up @@ -695,6 +696,17 @@ public function getUserPreferences($idUser)
return $this->preferences(self::GET, $idUser);
}

/**
* Updates all preferences of the $idUser with the same value (even preferences of themes that has not been set yet).
* @param integer $idUser User identification value.
* @param array $params Array with the required params as keys (used with PUT && POST method).
* @return array Response key => value array
*/
public function updateAllUserPreferences($idUser, $params)
{
return $this->preferences(self::PUT, $idUser, $params);
}


//////////////////////////
// SEND CALL //
Expand All @@ -704,7 +716,7 @@ public function getUserPreferences($idUser)
* @param array $params Array with the required params as keys (used with PUT && POST method).
* @return array Response key => value array
*/
public function sendNotification($params)
public function sendNotification($params = [])
{
return $this->send(self::POST, $params);
}
Expand Down Expand Up @@ -1223,25 +1235,30 @@ private function preference($method, $idUser, $idTheme, $params = [])
* Prepares the API call given the different possibilities (depending on the $method).
* @param string $method HTTP method of the request.
* @param integer $idUser User identification value.
* @param array $params Array with the required params as keys (used with PUT && POST method).
* @return array Response key => value array
*
* @throws Exception If [Invalid @param $method set]
* @throws Exception If [There aren't required ids set]
*/
private function preferences($method, $idUser)
private function preferences($method, $idUser, $params = [])
{
if ($method == self::POST || $method == self::PUT || $method == self::DELETE) {
if ($method == self::POST || $method == self::DELETE) {
throw new Exception("Invalid call method", 1);
}

if ($method == self::PUT && empty($params)) {
throw new Exception("Trying to update data without params", 2);
}

if (!isset($idUser)) {
throw new Exception("Url can't be created, expecting referrer id", 3);
}

$url = "user/$idUser/preferences";
$request = $this->getRequestManager();
try {
return $request->sendRequest($method, $url);
return $request->sendRequest($method, $url, $params);
} catch (Exception $e) {
throw new Exception($e->getMessage(), $e->getCode());
}
Expand Down
30 changes: 20 additions & 10 deletions RequestManagers/GuzzleRequestManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,29 @@ public function sendRequest($method, $path, $params = [])
// Making the request
if ($this->getTransmission() == self::ASYNC) {
$this->client->send($request)->then(function ($response) {
if ($response->getStatusCode() == self::HTTP_RESPONSE_OK) {
return $response->json();
} else {
throw new \Exception($response->getReasonPhrase(), $response->getStatusCode());
}
});
$this->responseChecker($response);
})
;
} else {
$response = $this->client->send($request);
if ($response->getStatusCode() == self::HTTP_RESPONSE_OK) {
return $response->json();
} else {
throw new \Exception($response->getReasonPhrase(), $response->getStatusCode());
$this->responseChecker($response);
}
}

/**
* @param Response $response Guzzle customized response object.
* @return array Response key => value array
* @throws Exception If connection failed
*/
private function responseChecker($response)
{
if ($response->getStatusCode() == self::HTTP_RESPONSE_OK) {
return $response->json();
} else {
if ($response->hasHeader('X-Status-Reason')) {
throw new \Exception($response->getHeader('X-Status-Reason'), $response->getStatusCode());
}
throw new \Exception($response->getReasonPhrase(), $response->getStatusCode());
}
}
}

0 comments on commit 30d32b9

Please sign in to comment.