From b76e2ff898b23745d4c9aaee49eeb7d88f2896ab Mon Sep 17 00:00:00 2001 From: Carson Fleming Date: Thu, 2 Mar 2017 22:49:24 -0500 Subject: Updated mailgun plugin --- .../php-http/curl-client/src/CurlPromise.php | 108 +++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 inc/mailgun/php-http/curl-client/src/CurlPromise.php (limited to 'inc/mailgun/php-http/curl-client/src/CurlPromise.php') diff --git a/inc/mailgun/php-http/curl-client/src/CurlPromise.php b/inc/mailgun/php-http/curl-client/src/CurlPromise.php new file mode 100644 index 0000000..68a775c --- /dev/null +++ b/inc/mailgun/php-http/curl-client/src/CurlPromise.php @@ -0,0 +1,108 @@ + + */ +class CurlPromise implements Promise +{ + /** + * Shared promise core + * + * @var PromiseCore + */ + private $core; + + /** + * Requests runner + * + * @var MultiRunner + */ + private $runner; + + /** + * Create new promise. + * + * @param PromiseCore $core Shared promise core + * @param MultiRunner $runner Simultaneous requests runner + */ + public function __construct(PromiseCore $core, MultiRunner $runner) + { + $this->core = $core; + $this->runner = $runner; + } + + /** + * Add behavior for when the promise is resolved or rejected. + * + * If you do not care about one of the cases, you can set the corresponding callable to null + * The callback will be called when the response or exception arrived and never more than once. + * + * @param callable $onFulfilled Called when a response will be available. + * @param callable $onRejected Called when an error happens. + * + * You must always return the Response in the interface or throw an Exception. + * + * @return Promise Always returns a new promise which is resolved with value of the executed + * callback (onFulfilled / onRejected). + */ + public function then(callable $onFulfilled = null, callable $onRejected = null) + { + if ($onFulfilled) { + $this->core->addOnFulfilled($onFulfilled); + } + if ($onRejected) { + $this->core->addOnRejected($onRejected); + } + + return new self($this->core, $this->runner); + } + + /** + * Get the state of the promise, one of PENDING, FULFILLED or REJECTED. + * + * @return string + */ + public function getState() + { + return $this->core->getState(); + } + + /** + * Wait for the promise to be fulfilled or rejected. + * + * When this method returns, the request has been resolved and the appropriate callable has terminated. + * + * When called with the unwrap option + * + * @param bool $unwrap Whether to return resolved value / throw reason or not + * + * @return \Psr\Http\Message\ResponseInterface|null Resolved value, null if $unwrap is set to false + * + * @throws \Http\Client\Exception The rejection reason. + */ + public function wait($unwrap = true) + { + $this->runner->wait($this->core); + + if ($unwrap) { + if ($this->core->getState() === self::REJECTED) { + throw $this->core->getException(); + } + + return $this->core->getResponse(); + } + return null; + } +} -- cgit v1.2.3