From 0f5440c3688682335f245cb52ad592e0784e45fa Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 26 Mar 2017 20:42:36 +0200 Subject: [PATCH] Fix new constructor (#314) * Bugfix * Updated docs * cs --- README.md | 37 ++++++++++++++++--------------------- src/Mailgun/Mailgun.php | 36 +++++++++--------------------------- 2 files changed, 25 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 51f96fd..5cbe123 100644 --- a/README.md +++ b/README.md @@ -57,36 +57,26 @@ use Mailgun\Mailgun; Here's how to send a message using the SDK: ```php -# First, instantiate the SDK with your API credentials and define your domain. -$mg = new Mailgun("key-example"); -$domain = "example.com"; +# First, instantiate the SDK with your API credentials +$mg = Mailgun::create('key-example'); # 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.')); -``` - -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)); +$mg->message()->send('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.' +]); ``` ### Response 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 -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 -$mg = new Mailgun("key-example"); +$mg = Mailgun::create('key-example'); $dns = $mg->domains()->show('example.com')->getInboundDNSRecords(); foreach ($dns as $record) { @@ -100,7 +90,10 @@ to the Mailgun class. ```php 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'); 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 the API calls. +**Warning: When using `NoopHydrator` there will be no exceptions on a non-200 response.** + ### Debugging Debugging the PHP SDK can be really helpful when things aren't working quite right. diff --git a/src/Mailgun/Mailgun.php b/src/Mailgun/Mailgun.php index e5bdd85..98e6552 100644 --- a/src/Mailgun/Mailgun.php +++ b/src/Mailgun/Mailgun.php @@ -62,41 +62,23 @@ class Mailgun private $responseHistory = null; /** - * @param string|null $apiKey - * @param HttpClient|null $httpClient - * @param string $apiEndpoint - * @param Hydrator|null $hydrator - * @param HttpClientConfigurator|null $clientConfigurator - * @param RequestBuilder|null $requestBuilder + * @param string|null $apiKey + * @param HttpClient|null $httpClient + * @param string $apiEndpoint + * @param Hydrator|null $hydrator + * @param RequestBuilder|null $requestBuilder */ public function __construct( - $apiKey = null, - HttpClient $httpClient = null, /* Deprecated, will be removed in 3.0 */ + $apiKey = null, /* Deprecated, will be removed in 3.0 */ + HttpClient $httpClient = null, $apiEndpoint = 'api.mailgun.net', /* Deprecated, will be removed in 3.0 */ Hydrator $hydrator = null, - HttpClientConfigurator $clientConfigurator = null, RequestBuilder $requestBuilder = null ) { $this->apiKey = $apiKey; $this->restClient = new RestClient($apiKey, $apiEndpoint, $httpClient); - if (null === $clientConfigurator) { - $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->httpClient = $httpClient; $this->requestBuilder = $requestBuilder ?: new RequestBuilder(); $this->hydrator = $hydrator ?: new ModelHydrator(); } @@ -115,7 +97,7 @@ class Mailgun ) { $httpClient = $httpClientConfigurator->createConfiguredClient(); - return new self($httpClient, $hydrator, $requestBuilder); + return new self(null, $httpClient, 'api.mailgun.net', $hydrator, $requestBuilder); } /**