Add domain verification (#370)

This commit is contained in:
maximzasorin 2017-06-22 20:35:42 +03:00 committed by Tobias Nyholm
parent 8187a47fa0
commit e800038f21
4 changed files with 157 additions and 0 deletions

View File

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

View File

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

View File

@ -0,0 +1,119 @@
<?php
/*
* Copyright (C) 2013-2016 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\Domain;
use Mailgun\Model\ApiResponse;
/**
* @author Maxim Zasorin <maximzasorin@gmail.com>
*/
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;
}
}

View File

@ -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/<domain>/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/<domain>` on a non-existent domain.
*