diff options
author | Avi Goldman <avrahamymgoldman@gmail.com> | 2017-01-06 09:09:36 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-06 09:09:36 -0500 |
commit | f4cb5267c58cbd0b3a4bd06d22aca5c52c2ff765 (patch) | |
tree | 590ea1a6299fba349d0b720c0ac9f9309a7121dd /lib/SparkPost | |
parent | c54abe82f41d7a6f1b857adf4610d82dba020426 (diff) | |
parent | 1abdc8bde168614ce055f9709de5dc788a7a46b9 (diff) | |
download | php-sparkpost-f4cb5267c58cbd0b3a4bd06d22aca5c52c2ff765.zip php-sparkpost-f4cb5267c58cbd0b3a4bd06d22aca5c52c2ff765.tar.gz php-sparkpost-f4cb5267c58cbd0b3a4bd06d22aca5c52c2ff765.tar.bz2 |
Added debug option
Added debug option
Diffstat (limited to 'lib/SparkPost')
-rw-r--r-- | lib/SparkPost/Resource.php | 4 | ||||
-rw-r--r-- | lib/SparkPost/ResourceBase.php | 3 | ||||
-rw-r--r-- | lib/SparkPost/SparkPost.php | 72 | ||||
-rw-r--r-- | lib/SparkPost/SparkPostException.php | 19 | ||||
-rw-r--r-- | lib/SparkPost/SparkPostPromise.php | 22 | ||||
-rw-r--r-- | lib/SparkPost/SparkPostResponse.php | 18 |
6 files changed, 112 insertions, 26 deletions
diff --git a/lib/SparkPost/Resource.php b/lib/SparkPost/Resource.php index 4b3550d..84fa20f 100644 --- a/lib/SparkPost/Resource.php +++ b/lib/SparkPost/Resource.php @@ -3,8 +3,8 @@ namespace SparkPost; /** - * Class Resource - * @package SparkPost + * Class Resource. + * * @deprecated Soft reservations placed on name Resource (as of PHP7) */ class Resource extends ResourceBase diff --git a/lib/SparkPost/ResourceBase.php b/lib/SparkPost/ResourceBase.php index 4e11ac2..3620192 100644 --- a/lib/SparkPost/ResourceBase.php +++ b/lib/SparkPost/ResourceBase.php @@ -3,8 +3,7 @@ namespace SparkPost; /** - * Class ResourceBase - * @package SparkPost + * Class ResourceBase. */ class ResourceBase { diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php index 0d3c6b5..601d063 100644 --- a/lib/SparkPost/SparkPost.php +++ b/lib/SparkPost/SparkPost.php @@ -11,12 +11,12 @@ use Psr\Http\Message\RequestInterface; class SparkPost { /** - * @var string Library version, used for setting User-Agent. + * @var string Library version, used for setting User-Agent */ private $version = '2.0.3'; /** - * @var HttpClient|HttpAsyncClient used to make requests. + * @var HttpClient|HttpAsyncClient used to make requests */ private $httpClient; @@ -26,7 +26,7 @@ class SparkPost private $messageFactory; /** - * @var array Options for requests. + * @var array Options for requests */ private $options; @@ -40,10 +40,11 @@ class SparkPost 'key' => '', 'version' => 'v1', 'async' => true, + 'debug' => false, ]; /** - * @var Transmission Instance of Transmission class. + * @var Transmission Instance of Transmission class */ public $transmissions; @@ -93,11 +94,13 @@ class SparkPost */ public function syncRequest($method = 'GET', $uri = '', $payload = [], $headers = []) { - $request = $this->buildRequest($method, $uri, $payload, $headers); + $requestValues = $this->buildRequestValues($method, $uri, $payload, $headers); + $request = call_user_func_array(array($this, 'buildRequestInstance'), $requestValues); + try { - return new SparkPostResponse($this->httpClient->sendRequest($request)); + return new SparkPostResponse($this->httpClient->sendRequest($request), $this->ifDebug($requestValues)); } catch (\Exception $exception) { - throw new SparkPostException($exception); + throw new SparkPostException($exception, $this->ifDebug($requestValues)); } } @@ -114,25 +117,26 @@ class SparkPost public function asyncRequest($method = 'GET', $uri = '', $payload = [], $headers = []) { if ($this->httpClient instanceof HttpAsyncClient) { - $request = $this->buildRequest($method, $uri, $payload, $headers); + $requestValues = $this->buildRequestValues($method, $uri, $payload, $headers); + $request = call_user_func_array(array($this, 'buildRequestInstance'), $requestValues); - return new SparkPostPromise($this->httpClient->sendAsyncRequest($request)); + return new SparkPostPromise($this->httpClient->sendAsyncRequest($request), $this->ifDebug($requestValues)); } else { throw new \Exception('Your http client does not support asynchronous requests. Please use a different client or use synchronous requests.'); } } /** - * Builds request from given params. + * Builds request values from given params. * * @param string $method * @param string $uri * @param array $payload * @param array $headers * - * @return RequestInterface + * @return array $requestValues */ - public function buildRequest($method, $uri, $payload, $headers) + public function buildRequestValues($method, $uri, $payload, $headers) { $method = trim(strtoupper($method)); @@ -153,7 +157,37 @@ class SparkPost ]; $body = strtr(json_encode($body), $jsonReplace); - return $this->getMessageFactory()->createRequest($method, $url, $headers, $body); + return [ + 'method' => $method, + 'url' => $url, + 'headers' => $headers, + 'body' => $body, + ]; + } + + /** + * Build RequestInterface from given params. + * + * @param array $requestValues + * + * @return RequestInterface + */ + public function buildRequestInstance($method, $uri, $headers, $body) + { + return $this->getMessageFactory()->createRequest($method, $uri, $headers, $body); + } + + /** + * Build RequestInterface from given params. + * + * @param array $requestValues + * + * @return RequestInterface + */ + public function buildRequest($method, $uri, $payload, $headers) + { + $requestValues = $this->buildRequestValues($method, $uri, $payload, $headers); + return call_user_func_array(array($this, 'buildRequestInstance'), $requestValues); } /** @@ -246,6 +280,18 @@ class SparkPost } /** + * Returns the given value if debugging, an empty instance otherwise. + * + * @param any $param + * + * @return any $param + */ + private function ifDebug($param) + { + return $this->options['debug'] ? $param : null; + } + + /** * Sets up any endpoints to custom classes e.g. $this->transmissions. */ private function setupEndpoints() diff --git a/lib/SparkPost/SparkPostException.php b/lib/SparkPost/SparkPostException.php index ca92e24..bde2e5e 100644 --- a/lib/SparkPost/SparkPostException.php +++ b/lib/SparkPost/SparkPostException.php @@ -12,12 +12,19 @@ class SparkPostException extends \Exception private $body = null; /** + * Array with the request values sent. + */ + private $request; + + /** * Sets up the custom exception and copies over original exception values. * * @param Exception $exception - the exception to be wrapped */ - public function __construct(\Exception $exception) + public function __construct(\Exception $exception, $request = null) { + $this->request = $request; + $message = $exception->getMessage(); $code = $exception->getCode(); if ($exception instanceof HttpException) { @@ -30,6 +37,16 @@ class SparkPostException extends \Exception } /** + * Returns the request values sent. + * + * @return array $request + */ + public function getRequest() + { + return $this->request; + } + + /** * Returns the body. * * @return array $body - the json decoded body from the http response diff --git a/lib/SparkPost/SparkPostPromise.php b/lib/SparkPost/SparkPostPromise.php index df715d5..b7ded0c 100644 --- a/lib/SparkPost/SparkPostPromise.php +++ b/lib/SparkPost/SparkPostPromise.php @@ -12,13 +12,19 @@ class SparkPostPromise implements HttpPromise private $promise; /** + * Array with the request values sent. + */ + private $request; + + /** * set the promise to be wrapped. * * @param HttpPromise $promise */ - public function __construct(HttpPromise $promise) + public function __construct(HttpPromise $promise, $request = null) { $this->promise = $promise; + $this->request = $request; } /** @@ -29,13 +35,15 @@ class SparkPostPromise implements HttpPromise */ public function then(callable $onFulfilled = null, callable $onRejected = null) { - return $this->promise->then(function ($response) use ($onFulfilled) { + $request = $this->request; + + return $this->promise->then(function ($response) use ($onFulfilled, $request) { if (isset($onFulfilled)) { - $onFulfilled(new SparkPostResponse($response)); + $onFulfilled(new SparkPostResponse($response, $request)); } - }, function ($exception) use ($onRejected) { + }, function ($exception) use ($onRejected, $request) { if (isset($onRejected)) { - $onRejected(new SparkPostException($exception)); + $onRejected(new SparkPostException($exception, $request)); } }); } @@ -64,9 +72,9 @@ class SparkPostPromise implements HttpPromise try { $response = $this->promise->wait($unwrap); - return $response ? new SparkPostResponse($response) : $response; + return $response ? new SparkPostResponse($response, $this->request) : $response; } catch (\Exception $exception) { - throw new SparkPostException($exception); + throw new SparkPostException($exception, $this->request); } } } diff --git a/lib/SparkPost/SparkPostResponse.php b/lib/SparkPost/SparkPostResponse.php index e6a8fd1..402a2b2 100644 --- a/lib/SparkPost/SparkPostResponse.php +++ b/lib/SparkPost/SparkPostResponse.php @@ -13,13 +13,29 @@ class SparkPostResponse implements ResponseInterface private $response; /** + * Array with the request values sent. + */ + private $request; + + /** * set the response to be wrapped. * * @param ResponseInterface $response */ - public function __construct(ResponseInterface $response) + public function __construct(ResponseInterface $response, $request = null) { $this->response = $response; + $this->request = $request; + } + + /** + * Returns the request values sent. + * + * @return array $request + */ + public function getRequest() + { + return $this->request; } /** |