From e800038f21ce093690e0ddc26cefd59cc207b5ac Mon Sep 17 00:00:00 2001 From: maximzasorin Date: Thu, 22 Jun 2017 20:35:42 +0300 Subject: [PATCH] Add domain verification (#370) --- doc/index.md | 6 + src/Mailgun/Api/Domain.php | 17 +++ src/Mailgun/Model/Domain/VerifyResponse.php | 119 ++++++++++++++++++++ tests/Integration/DomainApiTest.php | 15 +++ 4 files changed, 157 insertions(+) create mode 100644 src/Mailgun/Model/Domain/VerifyResponse.php diff --git a/doc/index.md b/doc/index.md index 885d483..91134f9 100644 --- a/doc/index.md +++ b/doc/index.md @@ -25,6 +25,12 @@ $mailgun->domains()->index(); $mailgun->domains()->show('example.com'); ``` +#### Verify a domain + +```php +$mailgun->domains()->verify('example.com'); +``` + #### Create a new domain ```php diff --git a/src/Mailgun/Api/Domain.php b/src/Mailgun/Api/Domain.php index 5f208c9..a63c796 100644 --- a/src/Mailgun/Api/Domain.php +++ b/src/Mailgun/Api/Domain.php @@ -20,6 +20,7 @@ use Mailgun\Model\Domain\IndexResponse; use Mailgun\Model\Domain\ShowResponse; use Mailgun\Model\Domain\UpdateConnectionResponse; use Mailgun\Model\Domain\UpdateCredentialResponse; +use Mailgun\Model\Domain\VerifyResponse; use Psr\Http\Message\ResponseInterface; /** @@ -263,4 +264,20 @@ class Domain extends HttpApi return $this->hydrateResponse($response, UpdateConnectionResponse::class); } + + /** + * Returns a single domain. + * + * @param string $domain Name of the domain. + * + * @return VerifyResponse|array|ResponseInterface + */ + public function verify($domain) + { + Assert::stringNotEmpty($domain); + + $response = $this->httpPut(sprintf('/v3/domains/%s/verify', $domain)); + + return $this->hydrateResponse($response, VerifyResponse::class); + } } diff --git a/src/Mailgun/Model/Domain/VerifyResponse.php b/src/Mailgun/Model/Domain/VerifyResponse.php new file mode 100644 index 0000000..b522840 --- /dev/null +++ b/src/Mailgun/Model/Domain/VerifyResponse.php @@ -0,0 +1,119 @@ + + */ +final class VerifyResponse implements ApiResponse +{ + /** + * @var Domain + */ + private $domain; + + /** + * @var string + */ + private $message; + + /** + * @var DnsRecord[] + */ + private $inboundDnsRecords; + + /** + * @var DnsRecord[] + */ + private $outboundDnsRecords; + + /** + * @param array $data + * + * @return self + */ + public static function create(array $data) + { + $rx = []; + $tx = []; + $domain = null; + $message = null; + + if (isset($data['domain'])) { + $domain = Domain::create($data['domain']); + } + + if (isset($data['message'])) { + $message = $data['message']; + } + + if (isset($data['receiving_dns_records'])) { + foreach ($data['receiving_dns_records'] as $item) { + $rx[] = DnsRecord::create($item); + } + } + + if (isset($data['sending_dns_records'])) { + foreach ($data['sending_dns_records'] as $item) { + $tx[] = DnsRecord::create($item); + } + } + + return new self($domain, $rx, $tx, $message); + } + + /** + * @param Domain $domainInfo + * @param DnsRecord[] $rxRecords + * @param DnsRecord[] $txRecords + * @param string $message + */ + private function __construct(Domain $domainInfo, array $rxRecords, array $txRecords, $message) + { + $this->domain = $domainInfo; + $this->inboundDnsRecords = $rxRecords; + $this->outboundDnsRecords = $txRecords; + $this->message = $message; + } + + /** + * @return Domain + */ + public function getDomain() + { + return $this->domain; + } + + /** + * @return DnsRecord[] + */ + public function getInboundDNSRecords() + { + return $this->inboundDnsRecords; + } + + /** + * @return DnsRecord[] + */ + public function getOutboundDNSRecords() + { + return $this->outboundDnsRecords; + } + + /** + * @return string + */ + public function getMessage() + { + return $this->message; + } +} diff --git a/tests/Integration/DomainApiTest.php b/tests/Integration/DomainApiTest.php index da3b391..ab87e01 100644 --- a/tests/Integration/DomainApiTest.php +++ b/tests/Integration/DomainApiTest.php @@ -19,6 +19,7 @@ use Mailgun\Model\Domain\CredentialResponse; use Mailgun\Model\Domain\ConnectionResponse; use Mailgun\Model\Domain\UpdateConnectionResponse; use Mailgun\Model\Domain\UpdateCredentialResponse; +use Mailgun\Model\Domain\VerifyResponse; use Mailgun\Tests\Api\TestCase; /** @@ -75,6 +76,20 @@ class DomainApiTest extends TestCase $this->assertEquals($domain->getDomain()->getState(), 'active'); } + /** + * Performs `PUT /v3/domains//verify` for verify domain. + */ + public function testDomainVerify() + { + $mg = $this->getMailgunClient(); + + $ret = $mg->domains()->verify($this->testDomain); + + $this->assertNotNull($ret); + $this->assertInstanceOf(VerifyResponse::class, $ret); + $this->assertEquals('Domain DNS records have been updated', $ret->getMessage()); + } + /** * Performs `DELETE /v3/domains/` on a non-existent domain. *