summaryrefslogtreecommitdiffstats
path: root/lib/SparkPost/SparkPostPromise.php
diff options
context:
space:
mode:
authorAvi Goldman <avrahamymgoldman@gmail.com>2016-06-20 11:26:43 -0400
committerGitHub <noreply@github.com>2016-06-20 11:26:43 -0400
commit87552cc2766f4ea8c8a2d6ff6c70dd74faecb687 (patch)
tree0d37219ce699bffd8adc0cd5fb7c5ec012c80df0 /lib/SparkPost/SparkPostPromise.php
parenta5847be109dc6350fb0bc8beb8d0ba8bc23b75b6 (diff)
downloadphp-sparkpost-87552cc2766f4ea8c8a2d6ff6c70dd74faecb687.zip
php-sparkpost-87552cc2766f4ea8c8a2d6ff6c70dd74faecb687.tar.gz
php-sparkpost-87552cc2766f4ea8c8a2d6ff6c70dd74faecb687.tar.bz2
Merged FAD-3148 into 2.x
* FAD-3148 basic non-functioning base class * FAD-3148 a bit more functionality in the base class * FAD-3148 added custom promise class and custom response class. Did good things to SparkPost class * Updated Transmission.php for new refactor, still WIP. Created Resource.php as a parent class for all future resources. * Removed test functions * Cleaned up Transmissions.php according to PSR-2, deleted more test code and comments. * added sync and aysnc, cleaned up code * added support for async/sync option * added support for async/sync option * Added interns to authors * simplified the request function * added comments * added user agent * added comments in SparkPostPromise * added comments in SparkPostException * added comments in SparkPostResponse * cleaning up test * updated composer.json and contributing files for testing * Ran php-cs-fixer * testing for sparkpost response class * updated to newer version of guzzle * updated to newer version of guzzle * Cleaned up getUrl and other functions * cleaned up constructor and overrode getCode with getResponse()->getStatusCode() * fixed up then method * cleaned up getBody * deleted old test * Wrote total coverage tests for SparkPost class * commented out setting up transmissions endpoint until merging with FAD-3146
Diffstat (limited to 'lib/SparkPost/SparkPostPromise.php')
-rw-r--r--lib/SparkPost/SparkPostPromise.php69
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/SparkPost/SparkPostPromise.php b/lib/SparkPost/SparkPostPromise.php
new file mode 100644
index 0000000..15f129f
--- /dev/null
+++ b/lib/SparkPost/SparkPostPromise.php
@@ -0,0 +1,69 @@
+<?php
+
+namespace SparkPost;
+
+use Http\Promise\Promise as HttpPromise;
+
+class SparkPostPromise implements HttpPromise
+{
+ /**
+ * HttpPromise to be wrapped by SparkPostPromise.
+ */
+ private $promise;
+
+ /**
+ * set the promise to be wrapped.
+ *
+ * @param HttpPromise $promise
+ */
+ public function __construct(HttpPromise $promise)
+ {
+ $this->promise = $promise;
+ }
+
+ /**
+ * Hand off the response functions to the original promise and return a custom response or exception.
+ *
+ * @param callable $onFulfilled - function to be called if the promise is fulfilled
+ * @param callable $onRejected - function to be called if the promise is rejected
+ */
+ public function then(callable $onFulfilled = null, callable $onRejected = null)
+ {
+ return $this->promise->then(function($response) use ($onFulfilled) {
+ if (isset($onFulfilled))
+ $onFulfilled(new SparkPostResponse($response));
+ }, function($exception) use ($onRejected) {
+ if (isset($onRejected))
+ $onRejected(new SparkPostException($exception));
+ });
+ }
+
+ /**
+ * Hand back the state.
+ *
+ * @return $state - returns the state of the promise
+ */
+ public function getState()
+ {
+ return $this->promise->getState();
+ }
+
+ /**
+ * Wraps the wait function and returns a custom response or throws a custom exception.
+ *
+ * @param bool $unwrap
+ *
+ * @return SparkPostResponse
+ *
+ * @throws SparkPostException
+ */
+ public function wait($unwrap = true)
+ {
+ try {
+ $response = $this->promise->wait($unwrap);
+ return $response ? new SparkPostResponse($response) : $response;
+ } catch (\Exception $exception) {
+ throw new SparkPostException($exception);
+ }
+ }
+}