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 --- inc/mailgun/php-http/httplug/src/Exception.php | 12 ++++ .../httplug/src/Exception/HttpException.php | 74 ++++++++++++++++++++++ .../httplug/src/Exception/NetworkException.php | 14 ++++ .../httplug/src/Exception/RequestException.php | 43 +++++++++++++ .../httplug/src/Exception/TransferException.php | 14 ++++ .../php-http/httplug/src/HttpAsyncClient.php | 27 ++++++++ inc/mailgun/php-http/httplug/src/HttpClient.php | 28 ++++++++ .../httplug/src/Promise/HttpFulfilledPromise.php | 57 +++++++++++++++++ .../httplug/src/Promise/HttpRejectedPromise.php | 56 ++++++++++++++++ 9 files changed, 325 insertions(+) create mode 100644 inc/mailgun/php-http/httplug/src/Exception.php create mode 100644 inc/mailgun/php-http/httplug/src/Exception/HttpException.php create mode 100644 inc/mailgun/php-http/httplug/src/Exception/NetworkException.php create mode 100644 inc/mailgun/php-http/httplug/src/Exception/RequestException.php create mode 100644 inc/mailgun/php-http/httplug/src/Exception/TransferException.php create mode 100644 inc/mailgun/php-http/httplug/src/HttpAsyncClient.php create mode 100644 inc/mailgun/php-http/httplug/src/HttpClient.php create mode 100644 inc/mailgun/php-http/httplug/src/Promise/HttpFulfilledPromise.php create mode 100644 inc/mailgun/php-http/httplug/src/Promise/HttpRejectedPromise.php (limited to 'inc/mailgun/php-http/httplug/src') diff --git a/inc/mailgun/php-http/httplug/src/Exception.php b/inc/mailgun/php-http/httplug/src/Exception.php new file mode 100644 index 0000000..e7382c3 --- /dev/null +++ b/inc/mailgun/php-http/httplug/src/Exception.php @@ -0,0 +1,12 @@ + + */ +interface Exception +{ +} diff --git a/inc/mailgun/php-http/httplug/src/Exception/HttpException.php b/inc/mailgun/php-http/httplug/src/Exception/HttpException.php new file mode 100644 index 0000000..f4f32a4 --- /dev/null +++ b/inc/mailgun/php-http/httplug/src/Exception/HttpException.php @@ -0,0 +1,74 @@ + + */ +class HttpException extends RequestException +{ + /** + * @var ResponseInterface + */ + protected $response; + + /** + * @param string $message + * @param RequestInterface $request + * @param ResponseInterface $response + * @param \Exception|null $previous + */ + public function __construct( + $message, + RequestInterface $request, + ResponseInterface $response, + \Exception $previous = null + ) { + parent::__construct($message, $request, $previous); + + $this->response = $response; + $this->code = $response->getStatusCode(); + } + + /** + * Returns the response. + * + * @return ResponseInterface + */ + public function getResponse() + { + return $this->response; + } + + /** + * Factory method to create a new exception with a normalized error message. + * + * @param RequestInterface $request + * @param ResponseInterface $response + * @param \Exception|null $previous + * + * @return HttpException + */ + public static function create( + RequestInterface $request, + ResponseInterface $response, + \Exception $previous = null + ) { + $message = sprintf( + '[url] %s [http method] %s [status code] %s [reason phrase] %s', + $request->getRequestTarget(), + $request->getMethod(), + $response->getStatusCode(), + $response->getReasonPhrase() + ); + + return new self($message, $request, $response, $previous); + } +} diff --git a/inc/mailgun/php-http/httplug/src/Exception/NetworkException.php b/inc/mailgun/php-http/httplug/src/Exception/NetworkException.php new file mode 100644 index 0000000..f2198e5 --- /dev/null +++ b/inc/mailgun/php-http/httplug/src/Exception/NetworkException.php @@ -0,0 +1,14 @@ + + */ +class NetworkException extends RequestException +{ +} diff --git a/inc/mailgun/php-http/httplug/src/Exception/RequestException.php b/inc/mailgun/php-http/httplug/src/Exception/RequestException.php new file mode 100644 index 0000000..cdce14b --- /dev/null +++ b/inc/mailgun/php-http/httplug/src/Exception/RequestException.php @@ -0,0 +1,43 @@ + + */ +class RequestException extends TransferException +{ + /** + * @var RequestInterface + */ + private $request; + + /** + * @param string $message + * @param RequestInterface $request + * @param \Exception|null $previous + */ + public function __construct($message, RequestInterface $request, \Exception $previous = null) + { + $this->request = $request; + + parent::__construct($message, 0, $previous); + } + + /** + * Returns the request. + * + * @return RequestInterface + */ + public function getRequest() + { + return $this->request; + } +} diff --git a/inc/mailgun/php-http/httplug/src/Exception/TransferException.php b/inc/mailgun/php-http/httplug/src/Exception/TransferException.php new file mode 100644 index 0000000..a858cf5 --- /dev/null +++ b/inc/mailgun/php-http/httplug/src/Exception/TransferException.php @@ -0,0 +1,14 @@ + + */ +class TransferException extends \RuntimeException implements Exception +{ +} diff --git a/inc/mailgun/php-http/httplug/src/HttpAsyncClient.php b/inc/mailgun/php-http/httplug/src/HttpAsyncClient.php new file mode 100644 index 0000000..492e511 --- /dev/null +++ b/inc/mailgun/php-http/httplug/src/HttpAsyncClient.php @@ -0,0 +1,27 @@ + + */ +interface HttpAsyncClient +{ + /** + * Sends a PSR-7 request in an asynchronous way. + * + * Exceptions related to processing the request are available from the returned Promise. + * + * @param RequestInterface $request + * + * @return Promise Resolves a PSR-7 Response or fails with an Http\Client\Exception. + * + * @throws \Exception If processing the request is impossible (eg. bad configuration). + */ + public function sendAsyncRequest(RequestInterface $request); +} diff --git a/inc/mailgun/php-http/httplug/src/HttpClient.php b/inc/mailgun/php-http/httplug/src/HttpClient.php new file mode 100644 index 0000000..0e51749 --- /dev/null +++ b/inc/mailgun/php-http/httplug/src/HttpClient.php @@ -0,0 +1,28 @@ + + * @author Márk Sági-Kazár + * @author David Buchmann + */ +interface HttpClient +{ + /** + * Sends a PSR-7 request. + * + * @param RequestInterface $request + * + * @return ResponseInterface + * + * @throws \Http\Client\Exception If an error happens during processing the request. + * @throws \Exception If processing the request is impossible (eg. bad configuration). + */ + public function sendRequest(RequestInterface $request); +} diff --git a/inc/mailgun/php-http/httplug/src/Promise/HttpFulfilledPromise.php b/inc/mailgun/php-http/httplug/src/Promise/HttpFulfilledPromise.php new file mode 100644 index 0000000..6779e44 --- /dev/null +++ b/inc/mailgun/php-http/httplug/src/Promise/HttpFulfilledPromise.php @@ -0,0 +1,57 @@ +response = $response; + } + + /** + * {@inheritdoc} + */ + public function then(callable $onFulfilled = null, callable $onRejected = null) + { + if (null === $onFulfilled) { + return $this; + } + + try { + return new self($onFulfilled($this->response)); + } catch (Exception $e) { + return new HttpRejectedPromise($e); + } + } + + /** + * {@inheritdoc} + */ + public function getState() + { + return Promise::FULFILLED; + } + + /** + * {@inheritdoc} + */ + public function wait($unwrap = true) + { + if ($unwrap) { + return $this->response; + } + } +} diff --git a/inc/mailgun/php-http/httplug/src/Promise/HttpRejectedPromise.php b/inc/mailgun/php-http/httplug/src/Promise/HttpRejectedPromise.php new file mode 100644 index 0000000..bfb0738 --- /dev/null +++ b/inc/mailgun/php-http/httplug/src/Promise/HttpRejectedPromise.php @@ -0,0 +1,56 @@ +exception = $exception; + } + + /** + * {@inheritdoc} + */ + public function then(callable $onFulfilled = null, callable $onRejected = null) + { + if (null === $onRejected) { + return $this; + } + + try { + return new HttpFulfilledPromise($onRejected($this->exception)); + } catch (Exception $e) { + return new self($e); + } + } + + /** + * {@inheritdoc} + */ + public function getState() + { + return Promise::REJECTED; + } + + /** + * {@inheritdoc} + */ + public function wait($unwrap = true) + { + if ($unwrap) { + throw $this->exception; + } + } +} -- cgit v1.2.3