Removed unused tests and some parameters from the constructor

This commit is contained in:
Tobias Nyholm 2016-03-05 11:13:28 +01:00
parent b961cfb4eb
commit b85f1461d4
5 changed files with 101 additions and 60 deletions

View File

@ -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.

View File

@ -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;
}
}

View File

@ -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
*/

View File

@ -1,19 +0,0 @@
<?PHP
namespace Mailgun\Tests\Connection;
use Mailgun\Tests\Mock\Mailgun;
class ConnectionTest extends \Mailgun\Tests\MailgunTestCase
{
private $client;
public function setUp()
{
}
public function testNewClientInstantiation()
{
$this->client = new Mailgun("My-Super-Awesome-API-Key", "samples.mailgun.org", false);
}
}

View File

@ -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()