Fix new constructor (#314)

* Bugfix

* Updated docs

* cs
This commit is contained in:
Tobias Nyholm 2017-03-26 20:42:36 +02:00 committed by Sean Johnson
parent 7d27717473
commit 0f5440c368
2 changed files with 25 additions and 48 deletions

View File

@ -57,36 +57,26 @@ use Mailgun\Mailgun;
Here's how to send a message using the SDK: Here's how to send a message using the SDK:
```php ```php
# First, instantiate the SDK with your API credentials and define your domain. # First, instantiate the SDK with your API credentials
$mg = new Mailgun("key-example"); $mg = Mailgun::create('key-example');
$domain = "example.com";
# Now, compose and send your message. # Now, compose and send your message.
$mg->sendMessage($domain, array('from' => 'bob@example.com', $mg->message()->send('example.com', [
'to' => 'sally@example.com', 'from' => 'bob@example.com',
'subject' => 'The PHP SDK is awesome!', 'to' => 'sally@example.com',
'text' => 'It is so simple to send a message.')); 'subject' => 'The PHP SDK is awesome!',
``` 'text' => 'It is so simple to send a message.'
]);
Or obtain the last 25 log items:
```php
# First, instantiate the SDK with your API credentials and define your domain.
$mg = new Mailgun("key-example");
$domain = "example.com";
# Now, issue a GET against the Logs endpoint.
$mg->get("$domain/log", array('limit' => 25,
'skip' => 0));
``` ```
### Response ### Response
The results of a API call is, by default, a domain object. This will make it easy The results of a API call is, by default, a domain object. This will make it easy
to understand the response without reading the documentation. One can just read the to understand the response without reading the documentation. One can just read the
doc blocks on the response classes. This provide an excellet IDE integration. doc blocks on the response classes. This provide an excellent IDE integration.
```php ```php
$mg = new Mailgun("key-example"); $mg = Mailgun::create('key-example');
$dns = $mg->domains()->show('example.com')->getInboundDNSRecords(); $dns = $mg->domains()->show('example.com')->getInboundDNSRecords();
foreach ($dns as $record) { foreach ($dns as $record) {
@ -100,7 +90,10 @@ to the Mailgun class.
```php ```php
use Mailgun\Hydrator\ArrayHydator; use Mailgun\Hydrator\ArrayHydator;
$mg = new Mailgun("key-example", null, null, new ArrayHydator()); $configurator = new HttpClientConfigurator();
$configurator->setApiKey('key-example');
$mg = Mailgun::configure($configurator, new ArrayHydator());
$data = $mg->domains()->show('example.com'); $data = $mg->domains()->show('example.com');
foreach ($data['receiving_dns_records'] as $record) { foreach ($data['receiving_dns_records'] as $record) {
@ -111,6 +104,8 @@ foreach ($data['receiving_dns_records'] as $record) {
You could also use the `NoopHydrator` to get a PSR7 Response returned from You could also use the `NoopHydrator` to get a PSR7 Response returned from
the API calls. the API calls.
**Warning: When using `NoopHydrator` there will be no exceptions on a non-200 response.**
### Debugging ### Debugging
Debugging the PHP SDK can be really helpful when things aren't working quite right. Debugging the PHP SDK can be really helpful when things aren't working quite right.

View File

@ -62,41 +62,23 @@ class Mailgun
private $responseHistory = null; private $responseHistory = null;
/** /**
* @param string|null $apiKey * @param string|null $apiKey
* @param HttpClient|null $httpClient * @param HttpClient|null $httpClient
* @param string $apiEndpoint * @param string $apiEndpoint
* @param Hydrator|null $hydrator * @param Hydrator|null $hydrator
* @param HttpClientConfigurator|null $clientConfigurator * @param RequestBuilder|null $requestBuilder
* @param RequestBuilder|null $requestBuilder
*/ */
public function __construct( public function __construct(
$apiKey = null, $apiKey = null, /* Deprecated, will be removed in 3.0 */
HttpClient $httpClient = null, /* Deprecated, will be removed in 3.0 */ HttpClient $httpClient = null,
$apiEndpoint = 'api.mailgun.net', /* Deprecated, will be removed in 3.0 */ $apiEndpoint = 'api.mailgun.net', /* Deprecated, will be removed in 3.0 */
Hydrator $hydrator = null, Hydrator $hydrator = null,
HttpClientConfigurator $clientConfigurator = null,
RequestBuilder $requestBuilder = null RequestBuilder $requestBuilder = null
) { ) {
$this->apiKey = $apiKey; $this->apiKey = $apiKey;
$this->restClient = new RestClient($apiKey, $apiEndpoint, $httpClient); $this->restClient = new RestClient($apiKey, $apiEndpoint, $httpClient);
if (null === $clientConfigurator) { $this->httpClient = $httpClient;
$clientConfigurator = new HttpClientConfigurator();
/*
* To be backward compatible
*/
if ($apiEndpoint !== 'api.mailgun.net') {
$clientConfigurator->setEndpoint($apiEndpoint);
}
if ($httpClient !== null) {
$clientConfigurator->setHttpClient($httpClient);
}
}
$clientConfigurator->setApiKey($apiKey);
$this->httpClient = $clientConfigurator->createConfiguredClient();
$this->requestBuilder = $requestBuilder ?: new RequestBuilder(); $this->requestBuilder = $requestBuilder ?: new RequestBuilder();
$this->hydrator = $hydrator ?: new ModelHydrator(); $this->hydrator = $hydrator ?: new ModelHydrator();
} }
@ -115,7 +97,7 @@ class Mailgun
) { ) {
$httpClient = $httpClientConfigurator->createConfiguredClient(); $httpClient = $httpClientConfigurator->createConfiguredClient();
return new self($httpClient, $hydrator, $requestBuilder); return new self(null, $httpClient, 'api.mailgun.net', $hydrator, $requestBuilder);
} }
/** /**