diff --git a/README.md b/README.md index 15c6aad..51f96fd 100644 --- a/README.md +++ b/README.md @@ -125,20 +125,22 @@ Go to http://bin.mailgun.net. The Postbin will generate a special URL. Save that **Step 2 - Instantiate the Mailgun client using Postbin.** -*Tip: The bin id will be the URL part after bin.mailgun.net. It will be random generated letters and numbers. For example, the bin id in this URL, http://bin.mailgun.net/aecf68de, is "aecf68de".* +*Tip: The bin id will be the URL part after bin.mailgun.net. It will be random generated letters and numbers. +For example, the bin id in this URL, http://bin.mailgun.net/aecf68de, is "aecf68de".* ```php -# First, instantiate the SDK with your API credentials and define your domain. -$mg = new Mailgun('key-example', null, 'bin.mailgun.net'); -$mg->setApiVersion('aecf68de'); -$mg->setSslEnabled(false); -$domain = 'example.com'; +$configurator = new HttpClientConfigurator(); +$configurator->setEndpoint('http://bin.mailgun.net/aecf68de'); +$configurator->setDebug(true); +$mg = Mailgun::configure($configurator); # Now, compose and send your message. -$mg->sendMessage($domain, array('from' => 'bob@example.com', - 'to' => 'sally@example.com', - 'subject' => 'The PHP SDK is awesome!', - 'text' => 'It is so simple to send a message.')); +$mg->sendMessage('example.com', [ + 'from' => 'bob@example.com', + 'to' => 'sally@example.com', + 'subject' => 'The PHP SDK is awesome!', + 'text' => 'It is so simple to send a message.'] +); ``` ### Additional Info diff --git a/src/Mailgun/HttpClient/Plugin/ReplaceUriPlugin.php b/src/Mailgun/HttpClient/Plugin/ReplaceUriPlugin.php new file mode 100644 index 0000000..0ed1106 --- /dev/null +++ b/src/Mailgun/HttpClient/Plugin/ReplaceUriPlugin.php @@ -0,0 +1,45 @@ + + */ +final class ReplaceUriPlugin implements Plugin +{ + /** + * @var UriInterface + */ + private $uri; + + /** + * @param UriInterface $uri + */ + public function __construct(UriInterface $uri) + { + $this->uri = $uri; + } + + /** + * {@inheritdoc} + */ + public function handleRequest(RequestInterface $request, callable $next, callable $first) + { + $request = $request->withUri($this->uri); + + return $next($request); + } +} diff --git a/src/Mailgun/HttpClientConfigurator.php b/src/Mailgun/HttpClientConfigurator.php index 7438c0e..0313163 100644 --- a/src/Mailgun/HttpClientConfigurator.php +++ b/src/Mailgun/HttpClientConfigurator.php @@ -16,6 +16,7 @@ use Http\Discovery\UriFactoryDiscovery; use Http\Message\UriFactory; use Http\Client\Common\Plugin; use Mailgun\HttpClient\Plugin\History; +use Mailgun\HttpClient\Plugin\ReplaceUriPlugin; /** * Configure a HTTP client. @@ -29,6 +30,13 @@ final class HttpClientConfigurator */ private $endpoint = 'https://api.mailgun.net'; + /** + * If debug is true we will send all the request to the endpoint without appending any path. + * + * @var bool + */ + private $debug = false; + /** * @var string */ @@ -60,7 +68,7 @@ final class HttpClientConfigurator public function createConfiguredClient() { $plugins = [ - new Plugin\AddHostPlugin($this->getUriFactory()->createUri($this->getEndpoint())), + new Plugin\AddHostPlugin($this->getUriFactory()->createUri($this->endpoint)), new Plugin\HeaderDefaultsPlugin([ 'User-Agent' => 'mailgun-sdk-php/v2 (https://github.com/mailgun/mailgun-php)', 'Authorization' => 'Basic '.base64_encode(sprintf('api:%s', $this->getApiKey())), @@ -68,15 +76,23 @@ final class HttpClientConfigurator new Plugin\HistoryPlugin($this->responseHistory), ]; + if ($this->debug) { + $plugins[] = new ReplaceUriPlugin($this->getUriFactory()->createUri($this->endpoint)); + } + return new PluginClient($this->getHttpClient(), $plugins); } /** - * @return string + * @param bool $debug + * + * @return HttpClientConfigurator */ - private function getEndpoint() + public function setDebug($debug) { - return $this->endpoint; + $this->debug = $debug; + + return $this; } /**