summaryrefslogtreecommitdiffstats
path: root/lib/SparkPost/SparkPost.php
diff options
context:
space:
mode:
authorAvi Goldman <avrahamymgoldman@gmail.com>2017-01-04 09:14:28 -0500
committerAvi Goldman <avrahamymgoldman@gmail.com>2017-01-04 09:14:28 -0500
commit264fa95625382ee546465e892b2b9bb5b5090d24 (patch)
treeca9b20246881bf1a1d8a2f5eb2884a9a660020d3 /lib/SparkPost/SparkPost.php
parentc54abe82f41d7a6f1b857adf4610d82dba020426 (diff)
downloadphp-sparkpost-264fa95625382ee546465e892b2b9bb5b5090d24.zip
php-sparkpost-264fa95625382ee546465e892b2b9bb5b5090d24.tar.gz
php-sparkpost-264fa95625382ee546465e892b2b9bb5b5090d24.tar.bz2
broke building request values into their own func and added debug option
Diffstat (limited to 'lib/SparkPost/SparkPost.php')
-rw-r--r--lib/SparkPost/SparkPost.php49
1 files changed, 40 insertions, 9 deletions
diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php
index 0d3c6b5..8f61910 100644
--- a/lib/SparkPost/SparkPost.php
+++ b/lib/SparkPost/SparkPost.php
@@ -40,6 +40,7 @@ class SparkPost
'key' => '',
'version' => 'v1',
'async' => true,
+ 'debug' => false
];
/**
@@ -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 = $this->buildRequest($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, 'buildRequest'), $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,23 @@ 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 buildRequest($method, $uri, $headers, $body)
+ {
+ return $this->getMessageFactory()->createRequest($method, $uri, $headers, $body);
}
/**
@@ -246,6 +266,17 @@ 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()