diff --git a/src/Exception/AdapterException.php b/src/Exception/AdapterException.php new file mode 100644 index 0000000..04d935d --- /dev/null +++ b/src/Exception/AdapterException.php @@ -0,0 +1,27 @@ +data; + } + + /** + * @param array $data + */ + public function setData(array $data) + { + $this->data = $data; + } +} diff --git a/src/HttpAdapter/BuzzHttpAdapter.php b/src/HttpAdapter/BuzzHttpAdapter.php index d7f57c3..2f4d5af 100644 --- a/src/HttpAdapter/BuzzHttpAdapter.php +++ b/src/HttpAdapter/BuzzHttpAdapter.php @@ -11,6 +11,7 @@ namespace SmsSender\HttpAdapter; use Buzz\Browser; +use SmsSender\Exception\AdapterException; /** * @author Kévin Gomez @@ -44,12 +45,15 @@ public function getContent($url, $method = 'GET', array $headers = array(), $dat } try { - $response = $this->browser->call($url, $method, $headers, $data); + if($response = $this->browser->call($url, $method, $headers, $data)){ + return $response->getContent(); + } } catch (\Exception $e) { - return null; + if(!empty($e->getMessage())){ + throw new AdapterException($e->getMessage(), $e->getCode(), $e); + } } - - return $response ? $response->getContent() : null; + throw new AdapterException((string)$this->browser->getLastResponse(), ($this->browser->getLastResponse()) ? $this->browser->getLastResponse()->getStatusCode() : 0); } /** @@ -59,6 +63,18 @@ public function getName() { return 'buzz'; } + + /** + * Return last request as string or object + * + * @param bool|false $string + * + * @return mixed|string + */ + public function getLastRequest($string = false) + { + return ($string) ? (string)$this->browser->getLastRequest() : $string; + } } // vim: set softtabstop=4 tabstop=4 shiftwidth=4 autoindent: diff --git a/src/HttpAdapter/CurlHttpAdapter.php b/src/HttpAdapter/CurlHttpAdapter.php index 9cca32f..4361955 100644 --- a/src/HttpAdapter/CurlHttpAdapter.php +++ b/src/HttpAdapter/CurlHttpAdapter.php @@ -10,11 +10,18 @@ namespace SmsSender\HttpAdapter; +use SmsSender\Exception\AdapterException; + /** * @author Kévin Gomez */ class CurlHttpAdapter extends AbstractHttpAdapter implements HttpAdapterInterface { + /** + * @var array + */ + protected $lastRequest; + /** * {@inheritDoc} */ @@ -48,6 +55,19 @@ public function getContent($url, $method = 'GET', array $headers = array(), $dat // execute the request $content = curl_exec($c); + $this->setLastRequest([ + 'url' => $url, + 'method' => $method, + 'headers' => $headers, + 'data' => $data, + ]); + + if(curl_errno($c)){ + $adapterException = new AdapterException(curl_error($c), curl_errno($c)); + $adapterException->setData(curl_getinfo($c)); + curl_close($c); + throw $adapterException; + } curl_close($c); if (false === $content) { @@ -64,6 +84,26 @@ public function getName() { return 'curl'; } + + /** + * @param bool|false $string + * + * @return mixed|string + */ + public function getLastRequest($string = false) + { + return ($string) ? json_encode($this->lastRequest) : $this->lastRequest; + } + + /** + * Return last request as string or object + * + * @param array $lastRequest + */ + protected function setLastRequest($lastRequest) + { + $this->lastRequest = $lastRequest; + } } // vim: set softtabstop=4 tabstop=4 shiftwidth=4 autoindent: diff --git a/src/HttpAdapter/HttpAdapterInterface.php b/src/HttpAdapter/HttpAdapterInterface.php index db69d77..2e29812 100644 --- a/src/HttpAdapter/HttpAdapterInterface.php +++ b/src/HttpAdapter/HttpAdapterInterface.php @@ -41,6 +41,15 @@ public function getContent($url, $method = 'GET', array $headers = array(), $dat * @return string */ public function getName(); + + /** + * Return last request as string or object + * + * @param bool|false $string + * + * @return mixed|string + */ + public function getLastRequest($string = false); } // vim: set softtabstop=4 tabstop=4 shiftwidth=4 autoindent: