From 6b9f372cd2952a4c1f364f9079f94c8ad5556352 Mon Sep 17 00:00:00 2001 From: alphayax Date: Fri, 24 Jun 2016 16:18:37 +0200 Subject: [PATCH] Add FreeboxApiException when a service fail --- composer.json | 2 +- freebox/Exception/FreeboxApiException.php | 78 +++++++++++++++++++++++ freebox/utils/rest/Rest.php | 39 ++++++++---- freebox/utils/rest/RestAuth.php | 59 +++++------------ 4 files changed, 121 insertions(+), 57 deletions(-) create mode 100644 freebox/Exception/FreeboxApiException.php diff --git a/composer.json b/composer.json index 77e84e4..d2e9956 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "require" : { "php": ">=5.5.0", "ext-curl": "*", - "alphayax/php_utils" : "^1.3.0", + "alphayax/php_utils" : "^1.3.1", "monolog/monolog": "^1.9.1" }, "require-dev" : { diff --git a/freebox/Exception/FreeboxApiException.php b/freebox/Exception/FreeboxApiException.php new file mode 100644 index 0000000..c31044a --- /dev/null +++ b/freebox/Exception/FreeboxApiException.php @@ -0,0 +1,78 @@ +apiMessage; + } + + /** + * @return string + */ + public function getApiErrorCode() { + return $this->apiErrorCode; + } + + /** + * @return string + */ + public function getApiRequest() { + return $this->apiRequest; + } + + /** + * @return string + */ + public function getApiHost() { + return $this->apiHost; + } + + /** + * @param $message + */ + public function setApiMessage( $message) { + $this->apiMessage = $message; + } + + /** + * @param $apiErrorCode + */ + public function setApiErrorCode( $apiErrorCode) { + $this->apiErrorCode = $apiErrorCode; + } + + /** + * @param $apiRequest + */ + public function setApiRequest( $apiRequest) { + $this->apiRequest = $apiRequest; + } + + /** + * @param $apiHost + */ + public function setApiHost( $apiHost) { + $this->apiHost = $apiHost; + } + +} diff --git a/freebox/utils/rest/Rest.php b/freebox/utils/rest/Rest.php index 6190d3f..7686302 100644 --- a/freebox/utils/rest/Rest.php +++ b/freebox/utils/rest/Rest.php @@ -11,38 +11,38 @@ use alphayax; class Rest extends alphayax\utils\Rest { /** - * @param null $curl_post_data + * @param null $curlPostData * @param bool $checkResponse * @throws \Exception */ - public function GET( $curl_post_data = null, $checkResponse = true){ - parent::GET( $curl_post_data); + public function GET( $curlPostData = null, $checkResponse = true){ + parent::GET( $curlPostData); if( $checkResponse){ $this->checkResponse(); } } /** - * @param $curl_post_data + * @param $curlPostData */ - public function POST( $curl_post_data = null){ - parent::POST( $curl_post_data); + public function POST( $curlPostData = null){ + parent::POST( $curlPostData); $this->checkResponse(); } /** - * @param $curl_post_data + * @param $curlPostData */ - public function PUT( $curl_post_data = null){ - parent::PUT( $curl_post_data); + public function PUT( $curlPostData = null){ + parent::PUT( $curlPostData); $this->checkResponse(); } /** - * @param $curl_post_data + * @param $curlPostData */ - public function DELETE( $curl_post_data = null){ - parent::DELETE( $curl_post_data); + public function DELETE( $curlPostData = null){ + parent::DELETE( $curlPostData); $this->checkResponse(); } @@ -50,9 +50,20 @@ class Rest extends alphayax\utils\Rest { * @throws \Exception */ protected function checkResponse(){ + $request = explode( "\r\n", $this->curlGetInfo['request_header'])[0]; + $url = $this->curlGetInfo['url']; + // echo ">> $request ($url)" . PHP_EOL; + if( false === $this->getSuccess()){ - $response = $this->getCurlResponse(); - throw new \Exception( $response['msg'] . ' ('. $response['error_code'] . ')'); + + $response = $this->getCurlResponse(); + $Exception = new alphayax\freebox\Exception\FreeboxApiException( $response['msg'] . ' ('. $response['error_code'] . ')'); + $Exception->setApiMessage( $response['msg']); + $Exception->setApiErrorCode( $response['error_code']); + $Exception->setApiRequest( $request); + $Exception->setApiHost( $url); + + throw $Exception; } } diff --git a/freebox/utils/rest/RestAuth.php b/freebox/utils/rest/RestAuth.php index 44c2e53..14566a4 100644 --- a/freebox/utils/rest/RestAuth.php +++ b/freebox/utils/rest/RestAuth.php @@ -11,79 +11,54 @@ use alphayax; class RestAuth extends Rest { /** @var string */ - protected $session_token = ''; + protected $sessionToken = ''; /** - * @param null $curl_post_data + * @param null $curlPostData * @param bool $checkResponse * @throws \Exception */ - public function GET( $curl_post_data = null, $checkResponse = true){ + public function GET( $curlPostData = null, $checkResponse = true){ $this->add_XFbxAppAuth_Header(); - parent::GET( $curl_post_data, $checkResponse); + parent::GET( $curlPostData, $checkResponse); } /** - * @param $curl_post_data + * @param $curlPostData */ - public function POST( $curl_post_data = null){ + public function POST( $curlPostData = null){ $this->add_XFbxAppAuth_Header(); - parent::POST( $curl_post_data); + parent::POST( $curlPostData); } /** - * @param $curl_post_data + * @param $curlPostData */ - public function PUT( $curl_post_data = null){ + public function PUT( $curlPostData = null){ $this->add_XFbxAppAuth_Header(); - parent::PUT( $curl_post_data); + parent::PUT( $curlPostData); } /** - * @param $curl_post_data + * @param $curlPostData */ - public function DELETE( $curl_post_data = null){ + public function DELETE( $curlPostData = null){ $this->add_XFbxAppAuth_Header(); - parent::DELETE( $curl_post_data); + parent::DELETE( $curlPostData); } /** * Add the session token in the X-Fbx-App-Auth Header */ protected function add_XFbxAppAuth_Header(){ - $this->addHeader( 'X-Fbx-App-Auth', $this->session_token); + $this->addHeader( 'X-Fbx-App-Auth', $this->sessionToken); } /** - * @param $session_token + * @param $sessionToken */ - public function setSessionToken( $session_token){ - $this->session_token = $session_token; - } - - /** - * @throws \Exception - */ - protected function checkResponse(){ - $response = $this->getCurlResponse(); - $request = explode( "\r\n", $this->curlGetInfo['request_header'])[0] . PHP_EOL; - // echo ">> $request"; - - if( false === $this->getSuccess()){ - /* - switch( $response['error_code']){ - case 'invalid_request' : - $a = new alphayax\freebox\Exception\InvalidRequestException(); - $a->setHttpRequestHeader( $this->_curl_getinfo['request_header']); - $a->setHttpUrl( $this->_curl_getinfo['url']); - echo PHP_EOL . '---' . PHP_EOL; - echo $this->_curl_getinfo['request_header']; - echo PHP_EOL . '---' . PHP_EOL; - throw $a; - } - */ - throw new \Exception( $request .' - '. $response['msg'] . ' ('. $response['error_code'] . ')'); - } + public function setSessionToken( $sessionToken){ + $this->sessionToken = $sessionToken; } }