summaryrefslogtreecommitdiffstats
path: root/lib/SparkPost
diff options
context:
space:
mode:
Diffstat (limited to 'lib/SparkPost')
-rw-r--r--lib/SparkPost/Resource.php88
-rw-r--r--lib/SparkPost/ResourceBase.php91
-rw-r--r--lib/SparkPost/SparkPost.php74
-rw-r--r--lib/SparkPost/SparkPostException.php19
-rw-r--r--lib/SparkPost/SparkPostPromise.php22
-rw-r--r--lib/SparkPost/SparkPostResponse.php18
-rw-r--r--lib/SparkPost/Transmission.php2
7 files changed, 208 insertions, 106 deletions
diff --git a/lib/SparkPost/Resource.php b/lib/SparkPost/Resource.php
index 7c88743..84fa20f 100644
--- a/lib/SparkPost/Resource.php
+++ b/lib/SparkPost/Resource.php
@@ -2,87 +2,11 @@
namespace SparkPost;
-class Resource
+/**
+ * Class Resource.
+ *
+ * @deprecated Soft reservations placed on name Resource (as of PHP7)
+ */
+class Resource extends ResourceBase
{
- /**
- * SparkPost object used to make requests.
- */
- protected $sparkpost;
-
- /**
- * The api endpoint that gets prepended to all requests send through this resource.
- */
- protected $endpoint;
-
- /**
- * Sets up the Resource.
- *
- * @param SparkPost $sparkpost - the sparkpost instance that this resource is attached to
- * @param string $endpoint - the endpoint that this resource wraps
- */
- public function __construct(SparkPost $sparkpost, $endpoint)
- {
- $this->sparkpost = $sparkpost;
- $this->endpoint = $endpoint;
- }
-
- /**
- * Sends get request to API at the set endpoint.
- *
- * @see SparkPost->request()
- */
- public function get($uri = '', $payload = [], $headers = [])
- {
- return $this->request('GET', $uri, $payload, $headers);
- }
-
- /**
- * Sends put request to API at the set endpoint.
- *
- * @see SparkPost->request()
- */
- public function put($uri = '', $payload = [], $headers = [])
- {
- return $this->request('PUT', $uri, $payload, $headers);
- }
-
- /**
- * Sends post request to API at the set endpoint.
- *
- * @see SparkPost->request()
- */
- public function post($payload = [], $headers = [])
- {
- return $this->request('POST', '', $payload, $headers);
- }
-
- /**
- * Sends delete request to API at the set endpoint.
- *
- * @see SparkPost->request()
- */
- public function delete($uri = '', $payload = [], $headers = [])
- {
- return $this->request('DELETE', $uri, $payload, $headers);
- }
-
- /**
- * Sends requests to SparkPost object to the resource endpoint.
- *
- * @see SparkPost->request()
- *
- * @return SparkPostPromise or SparkPostResponse depending on sync or async request
- */
- public function request($method = 'GET', $uri = '', $payload = [], $headers = [])
- {
- if (is_array($uri)) {
- $headers = $payload;
- $payload = $uri;
- $uri = '';
- }
-
- $uri = $this->endpoint.'/'.$uri;
-
- return $this->sparkpost->request($method, $uri, $payload, $headers);
- }
}
diff --git a/lib/SparkPost/ResourceBase.php b/lib/SparkPost/ResourceBase.php
new file mode 100644
index 0000000..3620192
--- /dev/null
+++ b/lib/SparkPost/ResourceBase.php
@@ -0,0 +1,91 @@
+<?php
+
+namespace SparkPost;
+
+/**
+ * Class ResourceBase.
+ */
+class ResourceBase
+{
+ /**
+ * SparkPost object used to make requests.
+ */
+ protected $sparkpost;
+
+ /**
+ * The api endpoint that gets prepended to all requests send through this resource.
+ */
+ protected $endpoint;
+
+ /**
+ * Sets up the Resource.
+ *
+ * @param SparkPost $sparkpost - the sparkpost instance that this resource is attached to
+ * @param string $endpoint - the endpoint that this resource wraps
+ */
+ public function __construct(SparkPost $sparkpost, $endpoint)
+ {
+ $this->sparkpost = $sparkpost;
+ $this->endpoint = $endpoint;
+ }
+
+ /**
+ * Sends get request to API at the set endpoint.
+ *
+ * @see SparkPost->request()
+ */
+ public function get($uri = '', $payload = [], $headers = [])
+ {
+ return $this->request('GET', $uri, $payload, $headers);
+ }
+
+ /**
+ * Sends put request to API at the set endpoint.
+ *
+ * @see SparkPost->request()
+ */
+ public function put($uri = '', $payload = [], $headers = [])
+ {
+ return $this->request('PUT', $uri, $payload, $headers);
+ }
+
+ /**
+ * Sends post request to API at the set endpoint.
+ *
+ * @see SparkPost->request()
+ */
+ public function post($payload = [], $headers = [])
+ {
+ return $this->request('POST', '', $payload, $headers);
+ }
+
+ /**
+ * Sends delete request to API at the set endpoint.
+ *
+ * @see SparkPost->request()
+ */
+ public function delete($uri = '', $payload = [], $headers = [])
+ {
+ return $this->request('DELETE', $uri, $payload, $headers);
+ }
+
+ /**
+ * Sends requests to SparkPost object to the resource endpoint.
+ *
+ * @see SparkPost->request()
+ *
+ * @return SparkPostPromise or SparkPostResponse depending on sync or async request
+ */
+ public function request($method = 'GET', $uri = '', $payload = [], $headers = [])
+ {
+ if (is_array($uri)) {
+ $headers = $payload;
+ $payload = $uri;
+ $uri = '';
+ }
+
+ $uri = $this->endpoint.'/'.$uri;
+
+ return $this->sparkpost->request($method, $uri, $payload, $headers);
+ }
+}
diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php
index c880940..05be7f4 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';
+ private $version = '2.1.0';
/**
- * @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);
}
/**
@@ -254,6 +288,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;
}
/**
diff --git a/lib/SparkPost/Transmission.php b/lib/SparkPost/Transmission.php
index 6e44ab5..8efc934 100644
--- a/lib/SparkPost/Transmission.php
+++ b/lib/SparkPost/Transmission.php
@@ -2,7 +2,7 @@
namespace SparkPost;
-class Transmission extends Resource
+class Transmission extends ResourceBase
{
public function __construct(SparkPost $sparkpost)
{