diff --git a/composer.json b/composer.json index baea060..8e7fef3 100644 --- a/composer.json +++ b/composer.json @@ -11,11 +11,7 @@ ], "require": { "php": ">=5.4.0", - "guzzlehttp/guzzle": "6.0.*" - }, - "require-dev": { - "phpunit/phpunit": "~4.0", - "phpspec/phpspec": "~2.1" + "guzzlehttp/guzzle": "^6.0" }, "autoload": { "psr-0": { diff --git a/src/Prettus/Moip/Subscription/Contracts/Renderable.php b/src/Prettus/Moip/Subscription/Contracts/Renderable.php new file mode 100644 index 0000000..2800d5a --- /dev/null +++ b/src/Prettus/Moip/Subscription/Contracts/Renderable.php @@ -0,0 +1,17 @@ +setCredential(['token'=>$apiToken,'key'=>$apiKey]); $this->setEnvironment($environment); @@ -84,6 +93,7 @@ public function __construct( $apiToken, $apiKey, $environment = MoipHttpClient:: public function setCredential($credentials = []){ $this->apiKey = $credentials['key']; $this->apiToken = $credentials['token']; + return $this; } @@ -121,13 +131,23 @@ public function getApiVersion(){ * * @param null $url * @param array $options + * * @throws ClientException - * @return string + * @return array */ public function get($url = null, $options = []) { - $response = $this->client->get($url, $this->getOptions($options)); - return $response->getBody()->getContents(); + try { + $response = $this->client->get($url, $this->getOptions($options)); + + return Utils::formatInJson( $response ); + } catch(RequestException $e) { + if ($e->hasResponse()) { + return $this->composeError($e->getResponse()); + } + + throw $e; + } } /** @@ -135,13 +155,23 @@ public function get($url = null, $options = []) * * @param null $url * @param array $options + * * @throws ClientException - * @return string + * @return string|array */ public function post($url = null, $options = []) { - $response = $this->client->post($url, $this->getOptions($options)); - return $response->getBody()->getContents(); + try { + $response = $this->client->post($url, $this->getOptions($options)); + + return $response->getBody()->getContents(); + } catch(RequestException $e) { + if ($e->hasResponse()) { + return $this->composeError($e->getResponse()); + } + + throw $e; + } } /** @@ -149,13 +179,23 @@ public function post($url = null, $options = []) * * @param null $url * @param array $options + * * @throws ClientException - * @return string + * @return string|array */ public function put($url = null, $options = []) { - $response = $this->client->put($url, $this->getOptions($options) ); - return $response->getBody()->getContents(); + try { + $response = $this->client->put($url, $this->getOptions($options) ); + + return $response->getBody()->getContents(); + } catch(RequestException $e) { + if ($e->hasResponse()) { + return $this->composeError($e->getResponse()); + } + + throw $e; + } } /** @@ -163,13 +203,23 @@ public function put($url = null, $options = []) * * @param null $url * @param array $options + * * @throws ClientException - * @return string + * @return string|array */ public function delete($url = null, $options = []) { - $response = $this->client->delete($url, $this->getOptions($options)); - return $response->getBody()->getContents(); + try { + $response = $this->client->delete($url, $this->getOptions($options)); + + return $response->getBody()->getContents(); + } catch(RequestException $e) { + if ($e->hasResponse()) { + return $this->composeError($e->getResponse()); + } + + throw $e; + } } /** @@ -179,4 +229,28 @@ public function delete($url = null, $options = []) public function getOptions($options = []){ return array_merge($this->requestOptions, $options); } + + /** + * Monta um array contendo os erros da requisição + * + * @param ResponseInterface $response + * + * @return array + */ + private function composeError(ResponseInterface $response) + { + $error = array( + 'error' => true, + 'http_code' => $response->getStatusCode(), + 'http_reason' => $response->getReasonPhrase() + ); + + $message = Utils::formatInJson($response); + + if ($message) { + $error = $error + $message; + } + + return $error; + } } \ No newline at end of file diff --git a/src/Prettus/Moip/Subscription/Resources/Customers.php b/src/Prettus/Moip/Subscription/Resources/Customers.php index c58ef47..b824388 100644 --- a/src/Prettus/Moip/Subscription/Resources/Customers.php +++ b/src/Prettus/Moip/Subscription/Resources/Customers.php @@ -35,7 +35,7 @@ public function __construct(MoipHttpClient $client){ * @param bool $new_vault * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function create(array $data, $new_vault = false, array $options = []){ @@ -55,7 +55,7 @@ public function create(array $data, $new_vault = false, array $options = []){ * * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function all(array $options = []){ @@ -73,7 +73,7 @@ public function all(array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function find($code, array $options = []){ @@ -93,7 +93,7 @@ public function find($code, array $options = []){ * @param array $data * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function update($code, array $data, array $options = []){ @@ -106,7 +106,6 @@ public function update($code, array $data, array $options = []){ $options = array_merge($options,['body'=>json_encode($data)]); return $this->client->put($url, $options); - } /** @@ -114,7 +113,7 @@ public function update($code, array $data, array $options = []){ * @param array $data * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function updateBillingInfo($code, array $data, array $options = []){ diff --git a/src/Prettus/Moip/Subscription/Resources/Invoices.php b/src/Prettus/Moip/Subscription/Resources/Invoices.php index 72364a1..cdb7d3f 100644 --- a/src/Prettus/Moip/Subscription/Resources/Invoices.php +++ b/src/Prettus/Moip/Subscription/Resources/Invoices.php @@ -3,7 +3,6 @@ use GuzzleHttp\Exception\ClientException; use Prettus\Moip\Subscription\Contracts\MoipHttpClient; use Prettus\Moip\Subscription\ResourceUtils; -use Psr\Http\Message\ResponseInterface; /** * Class Invoices @@ -31,7 +30,7 @@ public function __construct(MoipHttpClient $client){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function find($code, array $options = []){ @@ -50,7 +49,7 @@ public function find($code, array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function payments($code, array $options = []){ @@ -61,7 +60,6 @@ public function payments($code, array $options = []){ ]); return $this->client->get($url, $options); - } /** @@ -70,7 +68,7 @@ public function payments($code, array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function retryPayment($code, array $options = []){ @@ -81,6 +79,5 @@ public function retryPayment($code, array $options = []){ ]); return $this->client->get($url, $options); - } } \ No newline at end of file diff --git a/src/Prettus/Moip/Subscription/Resources/Payments.php b/src/Prettus/Moip/Subscription/Resources/Payments.php index ee0ead5..2b72be1 100644 --- a/src/Prettus/Moip/Subscription/Resources/Payments.php +++ b/src/Prettus/Moip/Subscription/Resources/Payments.php @@ -3,7 +3,6 @@ use GuzzleHttp\Exception\ClientException; use Prettus\Moip\Subscription\Contracts\MoipHttpClient; use Prettus\Moip\Subscription\ResourceUtils; -use Psr\Http\Message\ResponseInterface; /** * Class Payments @@ -34,7 +33,7 @@ public function __construct(MoipHttpClient $client){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function find($code, array $options = []){ @@ -46,5 +45,4 @@ public function find($code, array $options = []){ return $this->client->get($url, $options); } - } \ No newline at end of file diff --git a/src/Prettus/Moip/Subscription/Resources/Plans.php b/src/Prettus/Moip/Subscription/Resources/Plans.php index 9ade03a..ab2a047 100644 --- a/src/Prettus/Moip/Subscription/Resources/Plans.php +++ b/src/Prettus/Moip/Subscription/Resources/Plans.php @@ -3,7 +3,7 @@ use GuzzleHttp\Exception\ClientException; use Prettus\Moip\Subscription\Contracts\MoipHttpClient; use Prettus\Moip\Subscription\ResourceUtils; -use Psr\Http\Message\ResponseInterface; +use Symfony\Component\HttpFoundation\Response; /** * Class Plans @@ -31,7 +31,7 @@ public function __construct(MoipHttpClient $client){ * @param array $data * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function create(array $data, array $options = []){ @@ -50,7 +50,7 @@ public function create(array $data, array $options = []){ * * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function all(array $options = []){ @@ -68,7 +68,7 @@ public function all(array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function find($code, array $options = []){ @@ -88,7 +88,7 @@ public function find($code, array $options = []){ * @param array $data * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function update($code, array $data, array $options = []){ @@ -101,7 +101,6 @@ public function update($code, array $data, array $options = []){ $options = array_merge($options,['body'=>json_encode($data)]); return $this->client->put($url, $options); - } /** @@ -110,7 +109,7 @@ public function update($code, array $data, array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function active($code, array $options = []){ return $this->toogleActive($code, "activate", $options); @@ -122,7 +121,7 @@ public function active($code, array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function deactivate($code, array $options = []){ return $this->toogleActive($code, "inactivate", $options); @@ -135,7 +134,7 @@ public function deactivate($code, array $options = []){ * @param $status [activate, inactivate] * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ protected function toogleActive($code, $status, array $options = []){ diff --git a/src/Prettus/Moip/Subscription/Resources/Preferences.php b/src/Prettus/Moip/Subscription/Resources/Preferences.php index f9331a5..0d3c63a 100644 --- a/src/Prettus/Moip/Subscription/Resources/Preferences.php +++ b/src/Prettus/Moip/Subscription/Resources/Preferences.php @@ -3,7 +3,6 @@ use GuzzleHttp\Exception\ClientException; use Prettus\Moip\Subscription\Contracts\MoipHttpClient; use Prettus\Moip\Subscription\ResourceUtils; -use Psr\Http\Message\ResponseInterface; /** * Class Preferences @@ -31,7 +30,7 @@ public function __construct(MoipHttpClient $client){ * @param array $data * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function setPreferences(array $data, array $options = []){ @@ -52,7 +51,7 @@ public function setPreferences(array $data, array $options = []){ * @param array $data * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function setPreferencesRetry(array $data, array $options = []){ diff --git a/src/Prettus/Moip/Subscription/Resources/Subscriptions.php b/src/Prettus/Moip/Subscription/Resources/Subscriptions.php index fefed36..853761e 100644 --- a/src/Prettus/Moip/Subscription/Resources/Subscriptions.php +++ b/src/Prettus/Moip/Subscription/Resources/Subscriptions.php @@ -3,7 +3,6 @@ use GuzzleHttp\Exception\ClientException; use Prettus\Moip\Subscription\Contracts\MoipHttpClient; use Prettus\Moip\Subscription\ResourceUtils; -use Psr\Http\Message\ResponseInterface; /** * Class Subscriptions @@ -31,7 +30,7 @@ public function __construct(MoipHttpClient $client){ * @param array $data * @param bool $new_customer * @param array $options - * @return ResponseInterface + * @return mixed */ public function create(array $data, $new_customer = false, array $options = []){ @@ -51,7 +50,7 @@ public function create(array $data, $new_customer = false, array $options = []){ * * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function all(array $options = []){ @@ -69,7 +68,7 @@ public function all(array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function find($code, array $options = []){ @@ -89,7 +88,7 @@ public function find($code, array $options = []){ * @param array $data * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function update($code, array $data, array $options = []){ @@ -102,7 +101,6 @@ public function update($code, array $data, array $options = []){ $options = array_merge($options,['body'=>json_encode($data)]); return $this->client->put($url, $options); - } /** @@ -111,7 +109,7 @@ public function update($code, array $data, array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function invoices($code, array $options = []){ @@ -122,7 +120,6 @@ public function invoices($code, array $options = []){ ]); return $this->client->get($url, $options); - } /** @@ -131,7 +128,7 @@ public function invoices($code, array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function suspend($code, array $options = []){ return $this->toogleStatus($code, "suspend", $options); @@ -143,7 +140,7 @@ public function suspend($code, array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function activate($code, array $options = []){ return $this->toogleStatus($code, "activate", $options); @@ -155,7 +152,7 @@ public function activate($code, array $options = []){ * @param $code * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ public function cancel($code, array $options = []){ return $this->toogleStatus($code, "cancel", $options); @@ -168,7 +165,7 @@ public function cancel($code, array $options = []){ * @param $status [activate, inactivate] * @param array $options * @throws ClientException - * @return ResponseInterface + * @return mixed */ protected function toogleStatus($code, $status, array $options = []){ diff --git a/src/Prettus/Moip/Subscription/Webservice/FormatInJson.php b/src/Prettus/Moip/Subscription/Webservice/FormatInJson.php new file mode 100644 index 0000000..ec95a28 --- /dev/null +++ b/src/Prettus/Moip/Subscription/Webservice/FormatInJson.php @@ -0,0 +1,20 @@ +renderer->format(), true); + } +} \ No newline at end of file diff --git a/src/Prettus/Moip/Subscription/Webservice/Formatter.php b/src/Prettus/Moip/Subscription/Webservice/Formatter.php new file mode 100644 index 0000000..1a9b8ba --- /dev/null +++ b/src/Prettus/Moip/Subscription/Webservice/Formatter.php @@ -0,0 +1,24 @@ +renderer = $renderer; + } +} \ No newline at end of file diff --git a/src/Prettus/Moip/Subscription/Webservice/ResourceUtils.php b/src/Prettus/Moip/Subscription/Webservice/ResourceUtils.php new file mode 100644 index 0000000..2cc1985 --- /dev/null +++ b/src/Prettus/Moip/Subscription/Webservice/ResourceUtils.php @@ -0,0 +1,25 @@ +getBody()->getContents() ); + $renderTo = new FormatInJson( $webservice ); + + return $renderTo->format(); + } +} \ No newline at end of file diff --git a/src/Prettus/Moip/Subscription/Webservice/Webservice.php b/src/Prettus/Moip/Subscription/Webservice/Webservice.php new file mode 100644 index 0000000..7dfe1dc --- /dev/null +++ b/src/Prettus/Moip/Subscription/Webservice/Webservice.php @@ -0,0 +1,34 @@ +data = $data; + } + + /** + * Collection to be formatted + * + * @return mixed + */ + public function format() + { + return $this->data; + } +} \ No newline at end of file