diff --git a/README.md b/README.md index f8c7d46..8972bfb 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ When creating a new `Mailgun` object you must provide an instance of the `HttpCl ```php $client = new \Http\Adapter\Guzzle6\Client(); -$mailgun = new \Mailgun\Mailgun('api_key', null, null, true, $client); +$mailgun = new \Mailgun\Mailgun('api_key', $client); ``` You could also rely on the [auto discovery feature of Httplug](http://docs.php-http.org/en/latest/discovery.html). This @@ -161,7 +161,9 @@ Go to http://bin.mailgun.net. The Postbin will generate a special URL. Save that ```php # First, instantiate the SDK with your API credentials and define your domain. -$mg = new Mailgun('key-example', 'bin.mailgun.net', 'aecf68de', $ssl = False); +$mg = new Mailgun('key-example', null, 'bin.mailgun.net'); +$mg->setApiVersion('aecf68de'); +$mg->setSslEnabled('false'); $domain = 'example.com'; # Now, compose and send your message. diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index e35f9da..1cdc2ee 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -20,6 +20,7 @@ use Psr\Http\Message\ResponseInterface; class RestClient { /** + * Your API key * @var string */ private $apiKey; @@ -32,20 +33,30 @@ class RestClient /** * @var string */ - protected $apiEndpoint; + protected $apiHost; + + /** + * The version of the API to use + * @var string + */ + protected $apiVersion = 'v2'; + + /** + * If we should use SSL or not + * @var bool + */ + protected $sslEnabled = true; /** * @param string $apiKey * @param string $apiHost - * @param string $apiVersion - * @param bool $ssl * @param HttpClient $httpClient */ - public function __construct($apiKey, $apiHost, $apiVersion, $ssl, HttpClient $httpClient = null) + public function __construct($apiKey, $apiHost, HttpClient $httpClient = null) { $this->apiKey = $apiKey; + $this->apiHost = $apiHost; $this->httpClient = $httpClient; - $this->apiEndpoint = $this->generateEndpoint($apiHost, $apiVersion, $ssl); } /** @@ -72,7 +83,7 @@ class RestClient $headers['Content-Type'] = 'multipart/form-data; boundary='.$body->getBoundary(); } - $request = new Request($method, $this->apiEndpoint.$uri, $headers, $body); + $request = new Request($method, $this->getApiUrl($uri), $headers, $body); $response = $this->getHttpClient()->sendRequest($request); return $this->responseHandler($response); @@ -221,22 +232,6 @@ class RestClient } } - /** - * @param string $apiEndpoint - * @param string $apiVersion - * @param bool $ssl - * - * @return string - */ - protected function generateEndpoint($apiEndpoint, $apiVersion, $ssl) - { - if (!$ssl) { - return 'http://'.$apiEndpoint.'/'.$apiVersion.'/'; - } else { - return 'https://'.$apiEndpoint.'/'.$apiVersion.'/'; - } - } - /** * Prepare a file for the postBody. * @@ -277,4 +272,55 @@ class RestClient return $this->httpClient; } + + /** + * @param $uri + * + * @return string + */ + private function getApiUrl($uri) + { + return $this->generateEndpoint($this->apiHost, $this->apiVersion, $this->sslEnabled).$uri; + } + + + /** + * @param string $apiEndpoint + * @param string $apiVersion + * @param bool $ssl + * + * @return string + */ + private function generateEndpoint($apiEndpoint, $apiVersion, $ssl) + { + if (!$ssl) { + return 'http://'.$apiEndpoint.'/'.$apiVersion.'/'; + } else { + return 'https://'.$apiEndpoint.'/'.$apiVersion.'/'; + } + } + + /** + * @param string $apiVersion + * + * @return RestClient + */ + public function setApiVersion($apiVersion) + { + $this->apiVersion = $apiVersion; + + return $this; + } + + /** + * @param boolean $sslEnabled + * + * @return RestClient + */ + public function setSslEnabled($sslEnabled) + { + $this->sslEnabled = $sslEnabled; + + return $this; + } } diff --git a/src/Mailgun/Mailgun.php b/src/Mailgun/Mailgun.php index 2b8ebf8..6777801 100644 --- a/src/Mailgun/Mailgun.php +++ b/src/Mailgun/Mailgun.php @@ -30,28 +30,16 @@ class Mailgun{ /** * @param string|null $apiKey - * @param string $apiEndpoint - * @param string $apiVersion - * @param bool $ssl * @param HttpClient $httpClient + * @param string $apiEndpoint */ public function __construct( $apiKey = null, - $apiEndpoint = null, - $apiVersion = null, - $ssl = true, - HttpClient $httpClient = null + HttpClient $httpClient = null, + $apiEndpoint = 'api.mailgun.net' ) { - if (empty($apiEndpoint)) { - $apiEndpoint = "api.mailgun.net"; - } - - if (empty($apiVersion)) { - $apiVersion = "v3"; - } - $this->apiKey = $apiKey; - $this->restClient = new RestClient($apiKey, $apiEndpoint, $apiVersion, $ssl, $httpClient); + $this->restClient = new RestClient($apiKey, $apiEndpoint, $httpClient); } /** @@ -148,6 +136,30 @@ class Mailgun{ return $this->restClient->put($endpointUrl, $putData); } + /** + * @param string $apiVersion + * + * @return Mailgun + */ + public function setApiVersion($apiVersion) + { + $this->restClient->setApiVersion($apiVersion); + + return $this; + } + + /** + * @param boolean $sslEnabled + * + * @return Mailgun + */ + public function setSslEnabled($sslEnabled) + { + $this->restClient->setSslEnabled($sslEnabled); + + return $this; + } + /** * @return MessageBuilder */ diff --git a/tests/Mailgun/Tests/Connection/ConnectionTest.php b/tests/Mailgun/Tests/Connection/ConnectionTest.php deleted file mode 100644 index aed26de..0000000 --- a/tests/Mailgun/Tests/Connection/ConnectionTest.php +++ /dev/null @@ -1,19 +0,0 @@ -client = new Mailgun("My-Super-Awesome-API-Key", "samples.mailgun.org", false); - } -} diff --git a/tests/Mailgun/Tests/Messages/MessageBuilderTest.php b/tests/Mailgun/Tests/Messages/MessageBuilderTest.php index 4a46eea..8daf2e3 100644 --- a/tests/Mailgun/Tests/Messages/MessageBuilderTest.php +++ b/tests/Mailgun/Tests/Messages/MessageBuilderTest.php @@ -9,7 +9,7 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase public function setUp() { - $this->client = new Mailgun("My-Super-Awesome-API-Key", "samples.mailgun.org", false); + $this->client = new Mailgun(); } public function testBlankInstantiation()