diff --git a/src/Mailgun/HttpClient/Plugin/History.php b/src/Mailgun/HttpClient/Plugin/History.php new file mode 100644 index 0000000..ff2de8f --- /dev/null +++ b/src/Mailgun/HttpClient/Plugin/History.php @@ -0,0 +1,45 @@ + + */ +class History implements Journal +{ + /** + * @var ResponseInterface + */ + private $lastResponse; + + /** + * @return ResponseInterface|null + */ + public function getLastResponse() + { + return $this->lastResponse; + } + + public function addSuccess(RequestInterface $request, ResponseInterface $response) + { + $this->lastResponse = $response; + } + + public function addFailure(RequestInterface $request, Exception $exception) + { + } +} diff --git a/src/Mailgun/HttpClientConfigurator.php b/src/Mailgun/HttpClientConfigurator.php index 9ec3f3f..e1a7524 100644 --- a/src/Mailgun/HttpClientConfigurator.php +++ b/src/Mailgun/HttpClientConfigurator.php @@ -15,6 +15,7 @@ use Http\Discovery\HttpClientDiscovery; use Http\Discovery\UriFactoryDiscovery; use Http\Message\UriFactory; use Http\Client\Common\Plugin; +use Mailgun\HttpClient\Plugin\History; /** * Configure a HTTP client. @@ -43,6 +44,16 @@ class HttpClientConfigurator */ private $httpClient; + /** + * @var History + */ + private $responseHistory; + + public function __construct() + { + $this->responseHistory = new History(); + } + /** * @return PluginClient */ @@ -54,6 +65,7 @@ class HttpClientConfigurator 'User-Agent' => 'mailgun-sdk-php/v2 (https://github.com/mailgun/mailgun-php)', 'Authorization' => 'Basic '.base64_encode(sprintf('api:%s', $this->getApiKey())), ]), + new Plugin\HistoryPlugin($this->responseHistory), ]; return new PluginClient($this->getHttpClient(), $plugins); @@ -146,4 +158,12 @@ class HttpClientConfigurator return $this; } + + /** + * @return History + */ + public function getResponseHistory() + { + return $this->responseHistory; + } } diff --git a/src/Mailgun/Mailgun.php b/src/Mailgun/Mailgun.php index bccb7b5..c4d42fb 100644 --- a/src/Mailgun/Mailgun.php +++ b/src/Mailgun/Mailgun.php @@ -13,12 +13,14 @@ use Http\Client\Common\HttpMethodsClient; use Http\Client\HttpClient; use Mailgun\Connection\RestClient; use Mailgun\Constants\ExceptionMessages; +use Mailgun\HttpClient\Plugin\History; use Mailgun\Lists\OptInHandler; use Mailgun\Messages\BatchMessage; use Mailgun\Messages\Exceptions; use Mailgun\Messages\MessageBuilder; use Mailgun\Hydrator\ModelHydrator; use Mailgun\Hydrator\Hydrator; +use Psr\Http\Message\ResponseInterface; /** * This class is the base class for the Mailgun SDK. @@ -52,6 +54,13 @@ class Mailgun */ private $requestBuilder; + /** + * This is a object that holds the last response from the API. + * + * @var History + */ + private $responseHistory = null; + /** * @param string|null $apiKey * @param HttpClient|null $httpClient @@ -189,6 +198,14 @@ class Mailgun } } + /** + * @return ResponseInterface|null + */ + public function getLastResponse() + { + return $this->responseHistory->getLastResponse(); + } + /** * @param string $endpointUrl * @param array $postData