Removed inheritence, Renamed classes, Mark all classes as final (#212)

* Removed inheritence, Renamed classes, Mark all classes as final

* code style

* Updated docs

* Code style

* Be consistant with the naming of functions

* Fixed tests
This commit is contained in:
Tobias Nyholm 2016-11-11 21:53:26 +01:00 committed by Sean Johnson
parent c875890720
commit 1a71c14097
28 changed files with 613 additions and 416 deletions

View File

@ -71,7 +71,7 @@ abstract class AbstractApi
*
* @return ResponseInterface
*/
protected function get($path, array $parameters = [], array $requestHeaders = [])
protected function httpGet($path, array $parameters = [], array $requestHeaders = [])
{
if (count($parameters) > 0) {
$path .= '?'.http_build_query($parameters);
@ -95,9 +95,9 @@ abstract class AbstractApi
*
* @return ResponseInterface
*/
protected function post($path, array $parameters = [], array $requestHeaders = [])
protected function httpPost($path, array $parameters = [], array $requestHeaders = [])
{
return $this->postRaw($path, $this->createJsonBody($parameters), $requestHeaders);
return $this->httpPostRaw($path, $this->createJsonBody($parameters), $requestHeaders);
}
/**
@ -123,7 +123,7 @@ abstract class AbstractApi
*
* @return ResponseInterface
*/
protected function postRaw($path, $body, array $requestHeaders = [])
protected function httpPostRaw($path, $body, array $requestHeaders = [])
{
try {
$response = $this->httpClient->post($path, $requestHeaders, $body);
@ -143,7 +143,7 @@ abstract class AbstractApi
*
* @return ResponseInterface
*/
protected function put($path, array $parameters = [], array $requestHeaders = [])
protected function httpPut($path, array $parameters = [], array $requestHeaders = [])
{
try {
$response = $this->httpClient->put($path, $requestHeaders, $this->createJsonBody($parameters));
@ -177,7 +177,7 @@ abstract class AbstractApi
*
* @return ResponseInterface
*/
protected function delete($path, array $parameters = [], array $requestHeaders = [])
protected function httpDelete($path, array $parameters = [], array $requestHeaders = [])
{
try {
$response = $this->httpClient->delete($path, $requestHeaders, $this->createJsonBody($parameters));

View File

@ -10,13 +10,17 @@
namespace Mailgun\Api;
use Mailgun\Assert;
use Mailgun\Resource\Api\Domain\ComplexDomain;
use Mailgun\Resource\Api\Domain\Credential;
use Mailgun\Resource\Api\Domain\CredentialListResponse;
use Mailgun\Resource\Api\Domain\DeliverySettingsResponse;
use Mailgun\Resource\Api\Domain\DeliverySettingsUpdateResponse;
use Mailgun\Resource\Api\Domain\DomainListResponse;
use Mailgun\Resource\Api\SimpleResponse;
use Mailgun\Resource\Api\Domain\CreateCredentialResponse;
use Mailgun\Resource\Api\Domain\CreateResponse;
use Mailgun\Resource\Api\Domain\DeleteCredentialResponse;
use Mailgun\Resource\Api\Domain\DeleteResponse;
use Mailgun\Resource\Api\Domain\ShowResponse;
use Mailgun\Resource\Api\Domain\CredentialResponse;
use Mailgun\Resource\Api\Domain\ConnectionResponse;
use Mailgun\Resource\Api\Domain\UpdateConnectionResponse;
use Mailgun\Resource\Api\Domain\IndexResponse;
use Mailgun\Resource\Api\Domain\UpdateCredentialResponse;
use Psr\Http\Message\ResponseInterface;
/**
* {@link https://documentation.mailgun.com/api-domains.html}.
@ -31,9 +35,9 @@ class Domain extends AbstractApi
* @param int $limit
* @param int $skip
*
* @return DomainListResponse
* @return IndexResponse
*/
public function listAll($limit = 100, $skip = 0)
public function index($limit = 100, $skip = 0)
{
Assert::integer($limit);
Assert::integer($skip);
@ -43,9 +47,9 @@ class Domain extends AbstractApi
'skip' => $skip,
];
$response = $this->get('/v3/domains', $params);
$response = $this->httpGet('/v3/domains', $params);
return $this->serializer->deserialize($response, DomainListResponse::class);
return $this->serializer->deserialize($response, IndexResponse::class);
}
/**
@ -53,15 +57,15 @@ class Domain extends AbstractApi
*
* @param string $domain Name of the domain.
*
* @return ComplexDomain|array|ResponseInterface
* @return ShowResponse|array|ResponseInterface
*/
public function info($domain)
public function show($domain)
{
Assert::stringNotEmpty($domain);
$response = $this->get(sprintf('/v3/domains/%s', $domain));
$response = $this->httpGet(sprintf('/v3/domains/%s', $domain));
return $this->serializer->deserialize($response, ComplexDomain::class);
return $this->serializer->deserialize($response, ShowResponse::class);
}
/**
@ -74,7 +78,7 @@ class Domain extends AbstractApi
* @param string $spamAction `disable` or `tag` - inbound spam filtering.
* @param bool $wildcard Domain will accept email for subdomains.
*
* @return ComplexDomain|array|ResponseInterface
* @return CreateResponse|array|ResponseInterface
*/
public function create($domain, $smtpPass, $spamAction, $wildcard)
{
@ -93,7 +97,7 @@ class Domain extends AbstractApi
$response = $this->postMultipart('/v3/domains', $params);
return $this->safeDeserialize($response, ComplexDomain::class);
return $this->safeDeserialize($response, CreateResponse::class);
}
/**
@ -102,15 +106,15 @@ class Domain extends AbstractApi
*
* @param string $domain Name of the domain.
*
* @return SimpleResponse|array|ResponseInterface
* @return DeleteResponse|array|ResponseInterface
*/
public function remove($domain)
public function delete($domain)
{
Assert::stringNotEmpty($domain);
$response = $this->delete(sprintf('/v3/domains/%s', $domain));
$response = $this->httpDelete(sprintf('/v3/domains/%s', $domain));
return $this->serializer->deserialize($response, SimpleResponse::class);
return $this->serializer->deserialize($response, DeleteResponse::class);
}
/**
@ -120,9 +124,9 @@ class Domain extends AbstractApi
* @param int $limit Number of credentials to return
* @param int $skip Number of credentials to omit from the list
*
* @return CredentialsListResponse
* @return CredentialResponse
*/
public function listCredentials($domain, $limit = 100, $skip = 0)
public function credentials($domain, $limit = 100, $skip = 0)
{
Assert::stringNotEmpty($domain);
Assert::integer($limit);
@ -133,9 +137,9 @@ class Domain extends AbstractApi
'skip' => $skip,
];
$response = $this->get(sprintf('/v3/domains/%s/credentials', $domain), $params);
$response = $this->httpGet(sprintf('/v3/domains/%s/credentials', $domain), $params);
return $this->safeDeserialize($response, CredentialListResponse::class);
return $this->safeDeserialize($response, CredentialResponse::class);
}
/**
@ -145,9 +149,9 @@ class Domain extends AbstractApi
* @param string $login SMTP Username.
* @param string $password SMTP Password. Length min 5, max 32.
*
* @return Credential|array|ResponseInterface
* @return CreateCredentialResponse|array|ResponseInterface
*/
public function newCredential($domain, $login, $password)
public function createCredential($domain, $login, $password)
{
Assert::stringNotEmpty($domain);
Assert::stringNotEmpty($login);
@ -161,7 +165,7 @@ class Domain extends AbstractApi
$response = $this->postMultipart(sprintf('/v3/domains/%s/credentials', $domain), $params);
return $this->serializer->deserialize($response, SimpleResponse::class);
return $this->serializer->deserialize($response, CreateCredentialResponse::class);
}
/**
@ -171,7 +175,7 @@ class Domain extends AbstractApi
* @param string $login SMTP Username.
* @param string $pass New SMTP Password. Length min 5, max 32.
*
* @return SimpleResponse|array|ResponseInterface
* @return UpdateCredentialResponse|array|ResponseInterface
*/
public function updateCredential($domain, $login, $pass)
{
@ -193,7 +197,7 @@ class Domain extends AbstractApi
$params
);
return $this->serializer->deserialize($response, SimpleResponse::class);
return $this->serializer->deserialize($response, UpdateCredentialResponse::class);
}
/**
@ -202,7 +206,7 @@ class Domain extends AbstractApi
* @param string $domain Name of the domain.
* @param string $login SMTP Username.
*
* @return SimpleResponse|array|ResponseInterface
* @return DeleteCredentialResponse|array|ResponseInterface
*/
public function deleteCredential($domain, $login)
{
@ -217,7 +221,7 @@ class Domain extends AbstractApi
)
);
return $this->serializer->deserialize($response, SimpleResponse::class);
return $this->serializer->deserialize($response, DeleteCredentialResponse::class);
}
/**
@ -225,15 +229,15 @@ class Domain extends AbstractApi
*
* @param string $domain Name of the domain.
*
* @return DeliverySettingsResponse|array|ResponseInterface
* @return ConnectionResponse|array|ResponseInterface
*/
public function getDeliverySettings($domain)
public function connection($domain)
{
Assert::stringNotEmpty($domain);
$response = $this->get(sprintf('/v3/domains/%s/connection', $domain));
$response = $this->httpGet(sprintf('/v3/domains/%s/connection', $domain));
return $this->serializer->deserialize($response, DeliverySettingsResponse::class);
return $this->serializer->deserialize($response, ConnectionResponse::class);
}
/**
@ -244,9 +248,9 @@ class Domain extends AbstractApi
* @param bool|null $requireTLS Enforces that messages are sent only over a TLS connection.
* @param bool|null $noVerify Disables TLS certificate and hostname verification.
*
* @return DeliverySettingsResponse|array|ResponseInterface
* @return UpdateConnectionResponse|array|ResponseInterface
*/
public function updateDeliverySettings($domain, $requireTLS, $noVerify)
public function updateConnection($domain, $requireTLS, $noVerify)
{
Assert::stringNotEmpty($domain);
Assert::nullOrBoolean($requireTLS);
@ -264,6 +268,6 @@ class Domain extends AbstractApi
$response = $this->putMultipart(sprintf('/v3/domains/%s/connection', $domain), $params);
return $this->serializer->deserialize($response, DeliverySettingsUpdateResponse::class);
return $this->serializer->deserialize($response, UpdateConnectionResponse::class);
}
}

View File

@ -23,7 +23,7 @@ class Stats extends AbstractApi
{
Assert::stringNotEmpty($domain);
$response = $this->get(sprintf('/v3/%s/stats/total', rawurlencode($domain)), $params);
$response = $this->httpGet(sprintf('/v3/%s/stats/total', rawurlencode($domain)), $params);
return $this->serializer->deserialize($response, TotalResponse::class);
}
@ -38,7 +38,7 @@ class Stats extends AbstractApi
{
Assert::stringNotEmpty($domain);
$response = $this->get(sprintf('/v3/%s/stats', rawurlencode($domain)), $params);
$response = $this->httpGet(sprintf('/v3/%s/stats', rawurlencode($domain)), $params);
return $this->serializer->deserialize($response, AllResponse::class);
}

View File

@ -1,95 +0,0 @@
<?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\Resource\Api\Domain;
use Mailgun\Assert;
use Mailgun\Resource\CreatableFromArray;
/**
* ComplexDomain uses DomainTrait and exposes a "complex" constructor
* where an array or \stdClass can be passed in to find the appropriate
* fields.
*
* @author Sean Johnson <sean@mailgun.com>
*/
class ComplexDomain implements CreatableFromArray
{
/**
* @var SimpleDomain
*/
private $domain;
/**
* @var DomainDnsRecord[]
*/
private $inboundDnsRecords;
/**
* @var DomainDnsRecord[]
*/
private $outboundDnsRecords;
/**
* @param array $data
*
* @return ComplexDomain
*/
public static function createFromArray(array $data)
{
Assert::keyExists($data, 'domain');
Assert::keyExists($data, 'receiving_dns_records');
Assert::keyExists($data, 'sending_dns_records');
// Let DomainDnsRecord::createFromArray() handle validation of
// the `receiving_dns_records` and `sending_dns_records` data.
// Also let SimpleDomain::createFromArray() handle validation of
// the `domain` fields.
return new static(
SimpleDomain::createFromArray($data['domain']),
DomainDnsRecord::createFromArray($data['receiving_dns_records']),
DomainDnsRecord::createFromArray($data['sending_dns_records'])
);
}
/**
* @param SimpleDomain $domainInfo
* @param array $rxRecords Array of DomainDnsRecord instances
* @param array $txRecords Array of DomainDnsRecord instances
*/
public function __construct(SimpleDomain $domainInfo, array $rxRecords, array $txRecords)
{
$this->domain = $domainInfo;
$this->inboundDnsRecords = $rxRecords;
$this->outboundDnsRecords = $txRecords;
}
/**
* @return SimpleDomain
*/
public function getDomain()
{
return $this->domain;
}
/**
* @return DomainDnsRecord[]
*/
public function getInboundDNSRecords()
{
return $this->inboundDnsRecords;
}
/**
* @return DomainDnsRecord[]
*/
public function getOutboundDNSRecords()
{
return $this->outboundDnsRecords;
}
}

View File

@ -9,12 +9,12 @@
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Assert;
use Mailgun\Resource\CreatableFromArray;
use Mailgun\Resource\ApiResponse;
/**
* @author Sean Johnson <sean@mailgun.com>
*/
class DeliverySettingsResponse implements CreatableFromArray
final class ConnectionResponse implements ApiResponse
{
/**
* @var bool
@ -29,9 +29,9 @@ class DeliverySettingsResponse implements CreatableFromArray
/**
* @param array $data
*
* @return DeliverySettingsResponse
* @return self
*/
public static function createFromArray(array $data)
public static function create(array $data)
{
Assert::keyExists($data, 'connection');
Assert::isArray($data['connection']);
@ -40,7 +40,7 @@ class DeliverySettingsResponse implements CreatableFromArray
Assert::keyExists($connSettings, 'skip_verification');
Assert::keyExists($connSettings, 'require_tls');
return new static(
return new self(
$connSettings['skip_verification'],
$connSettings['require_tls']
);
@ -50,7 +50,7 @@ class DeliverySettingsResponse implements CreatableFromArray
* @param bool $noVerify Disable remote TLS certificate verification
* @param bool $requireTLS Requires TLS for all outbound communication
*/
public function __construct($noVerify, $requireTLS)
private function __construct($noVerify, $requireTLS)
{
$this->noVerify = $noVerify;
$this->requireTLS = $requireTLS;

View File

@ -0,0 +1,42 @@
<?php
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Resource\ApiResponse;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class CreateCredentialResponse implements ApiResponse
{
/**
* @var string
*/
private $message;
/**
* @param string $message
*/
private function __construct($message)
{
$this->message = $message;
}
/**
* @param array $data
*
* @return self
*/
public static function create(array $data)
{
return new self($data['message']);
}
/**
* @return string
*/
public function getMessage()
{
return $this->message;
}
}

View File

@ -0,0 +1,103 @@
<?php
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Assert;
use Mailgun\Resource\ApiResponse;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class CreateResponse implements ApiResponse
{
/**
* @var string
*/
private $message;
/**
* @var Domain
*/
private $domain;
/**
* @var DnsRecord[]
*/
private $inboundDnsRecords;
/**
* @var DnsRecord[]
*/
private $outboundDnsRecords;
/**
* @param array $data
*
* @return self
*/
public static function create(array $data)
{
Assert::keyExists($data, 'domain');
Assert::keyExists($data, 'message');
Assert::keyExists($data, 'receiving_dns_records');
Assert::keyExists($data, 'sending_dns_records');
$domain = Domain::create($data['domain']);
$rx = [];
$tx = [];
foreach ($data['receiving_dns_records'] as $item) {
$rx[] = DnsRecord::create($item);
}
foreach ($data['sending_dns_records'] as $item) {
$tx[] = DnsRecord::create($item);
}
return new self($domain, $rx, $tx, $data['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;
}
/**
* @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

@ -10,12 +10,12 @@
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Assert;
use Mailgun\Resource\CreatableFromArray;
use Mailgun\Resource\ApiResponse;
/**
* @author Sean Johnson <sean@mailgun.com>
*/
class CredentialListResponse implements CreatableFromArray
final class CredentialResponse implements ApiResponse
{
/**
* @var int
@ -23,16 +23,16 @@ class CredentialListResponse implements CreatableFromArray
private $totalCount;
/**
* @var Credential[]
* @var CredentialResponseItem[]
*/
private $items;
/**
* @param array $data
*
* @return CredentialListResponse|array|ResponseInterface
* @return self
*/
public static function createFromArray(array $data)
public static function create(array $data)
{
$items = [];
@ -40,17 +40,17 @@ class CredentialListResponse implements CreatableFromArray
Assert::keyExists($data, 'items');
foreach ($data['items'] as $item) {
$items[] = Credential::createFromArray($item);
$items[] = CredentialResponseItem::create($item);
}
return new self($data['total_count'], $items);
}
/**
* @param int $totalCount
* @param Credential[] $items
* @param int $totalCount
* @param CredentialResponseItem[] $items
*/
public function __construct($totalCount, array $items)
private function __construct($totalCount, array $items)
{
Assert::integer($totalCount);
Assert::isArray($items);
@ -69,7 +69,7 @@ class CredentialListResponse implements CreatableFromArray
}
/**
* @return Credential[]
* @return CredentialResponseItem[]
*/
public function getCredentials()
{

View File

@ -9,12 +9,11 @@
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Assert;
use Mailgun\Resource\CreatableFromArray;
/**
* @author Sean Johnson <sean@mailgun.com>
*/
class Credential implements CreatableFromArray
final class CredentialResponseItem
{
/**
* @var int|null
@ -39,9 +38,9 @@ class Credential implements CreatableFromArray
/**
* @param array $data
*
* @return Credential
* @return self
*/
public static function createFromArray(array $data)
public static function create(array $data)
{
Assert::keyExists($data, 'created_at');
Assert::keyExists($data, 'mailbox');
@ -57,7 +56,7 @@ class Credential implements CreatableFromArray
Assert::string($mailbox);
Assert::string($login);
return new static(
return new self(
$sizeBytes,
$createdAt,
$mailbox,
@ -71,7 +70,7 @@ class Credential implements CreatableFromArray
* @param string $mailbox
* @param string $login
*/
public function __construct($sizeBytes, \DateTime $createdAt, $mailbox, $login)
private function __construct($sizeBytes, \DateTime $createdAt, $mailbox, $login)
{
$this->sizeBytes = $sizeBytes;
$this->createdAt = $createdAt;

View File

@ -0,0 +1,56 @@
<?php
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Resource\ApiResponse;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class DeleteCredentialResponse implements ApiResponse
{
/**
* @var string
*/
private $message;
/**
* @var string
*/
private $spec;
/**
* @param string $message
*/
private function __construct($message, $spec)
{
$this->message = $message;
$this->spec = $spec;
}
/**
* @param array $data
*
* @return self
*/
public static function create(array $data)
{
return new self($data['message'], $data['spec']);
}
/**
* @return string
*/
public function getMessage()
{
return $this->message;
}
/**
* @return string
*/
public function getSpec()
{
return $this->spec;
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Resource\ApiResponse;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class DeleteResponse implements ApiResponse
{
/**
* @var string
*/
private $message;
/**
* @param string $message
*/
private function __construct($message)
{
$this->message = $message;
}
/**
* @param array $data
*
* @return self
*/
public static function create(array $data)
{
return new self($data['message']);
}
/**
* @return string
*/
public function getMessage()
{
return $this->message;
}
}

View File

@ -9,14 +9,13 @@
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Assert;
use Mailgun\Resource\CreatableFromArray;
/**
* Represents a single DNS record for a domain.
*
* @author Sean Johnson <sean@mailgun.com>
*/
class DomainDnsRecord implements CreatableFromArray
final class DnsRecord
{
/**
* @var string|null
@ -46,27 +45,20 @@ class DomainDnsRecord implements CreatableFromArray
/**
* @param array $data
*
* @return DomainDnsRecord[]|array|ResponseInterface
* @return self
*/
public static function createFromArray(array $data)
public static function create(array $data)
{
$items = [];
$name = isset($data['name']) ? $data['name'] : null;
$priority = isset($data['priority']) ? $data['priority'] : null;
foreach ($data as $item) {
Assert::keyExists($item, 'record_type');
Assert::keyExists($item, 'value');
Assert::keyExists($item, 'valid');
Assert::nullOrString($name);
Assert::string($data['record_type']);
Assert::string($data['value']);
Assert::nullOrString($priority);
Assert::string($data['valid']);
$items[] = new static(
array_key_exists('name', $item) ? $item['name'] : null,
$item['record_type'],
$item['value'],
array_key_exists('priority', $item) ? $item['priority'] : null,
$item['valid']
);
}
return $items;
return new self($name, $data['record_type'], $data['value'], $priority, $data['valid']);
}
/**
@ -76,14 +68,8 @@ class DomainDnsRecord implements CreatableFromArray
* @param string|null $priority Record priority, used for MX
* @param string $valid DNS record has been added to domain DNS?
*/
public function __construct($name, $type, $value, $priority, $valid)
private function __construct($name, $type, $value, $priority, $valid)
{
Assert::nullOrString($name);
Assert::string($type);
Assert::string($value);
Assert::nullOrString($priority);
Assert::string($valid);
$this->name = $name;
$this->type = $type;
$this->value = $value;
@ -108,7 +94,7 @@ class DomainDnsRecord implements CreatableFromArray
}
/**
* @return value
* @return string
*/
public function getValue()
{

View File

@ -9,14 +9,13 @@
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Assert;
use Mailgun\Resource\CreatableFromArray;
/**
* Represents domain information in its simplest form.
*
* @author Sean Johnson <sean@ramcloud.io>
*/
class SimpleDomain implements CreatableFromArray
final class Domain
{
/**
* @var \DateTime
@ -56,12 +55,10 @@ class SimpleDomain implements CreatableFromArray
/**
* @param array $data
*
* @return SimpleDomain
* @return self
*/
public static function createFromArray(array $data)
public static function create(array $data)
{
Assert::isArray($data);
Assert::keyExists($data, 'name');
Assert::keyExists($data, 'smtp_login');
Assert::keyExists($data, 'smtp_password');
@ -70,7 +67,7 @@ class SimpleDomain implements CreatableFromArray
Assert::keyExists($data, 'state');
Assert::keyExists($data, 'created_at');
return new static(
return new self(
$data['name'],
$data['smtp_login'],
$data['smtp_password'],
@ -90,16 +87,8 @@ class SimpleDomain implements CreatableFromArray
* @param string $state
* @param \DateTime $createdAt
*/
public function __construct($name, $smtpLogin, $smtpPassword, $wildcard, $spamAction, $state, \DateTime $createdAt)
private function __construct($name, $smtpLogin, $smtpPassword, $wildcard, $spamAction, $state, \DateTime $createdAt)
{
Assert::string($name);
Assert::string($smtpLogin);
Assert::string($smtpPassword);
Assert::boolean($wildcard);
Assert::string($spamAction);
Assert::string($state);
Assert::isInstanceOf($createdAt, '\DateTime');
$this->name = $name;
$this->smtpLogin = $smtpLogin;
$this->smtpPassword = $smtpPassword;

View File

@ -10,12 +10,12 @@
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Assert;
use Mailgun\Resource\CreatableFromArray;
use Mailgun\Resource\ApiResponse;
/**
* @author Sean Johnson <sean@mailgun.com>
*/
class DomainListResponse implements CreatableFromArray
final class IndexResponse implements ApiResponse
{
/**
* @var int
@ -30,9 +30,9 @@ class DomainListResponse implements CreatableFromArray
/**
* @param array $data
*
* @return DomainListResponse|array|ResponseInterface
* @return self
*/
public static function createFromArray(array $data)
public static function create(array $data)
{
$items = [];
@ -40,24 +40,7 @@ class DomainListResponse implements CreatableFromArray
Assert::keyExists($data, 'items');
foreach ($data['items'] as $item) {
Assert::keyExists($item, 'name');
Assert::keyExists($item, 'smtp_login');
Assert::keyExists($item, 'smtp_password');
Assert::keyExists($item, 'wildcard');
Assert::keyExists($item, 'spam_action');
Assert::keyExists($item, 'state');
Assert::keyExists($item, 'created_at');
$items[] = SimpleDomain::createFromArray($item);
$items[] = new SimpleDomain(
$item['name'],
$item['smtp_login'],
$item['smtp_password'],
$item['wildcard'],
$item['spam_action'],
$item['state'],
new \DateTime($item['created_at'])
);
$items[] = Domain::create($item);
}
return new self($data['total_count'], $items);
@ -67,7 +50,7 @@ class DomainListResponse implements CreatableFromArray
* @param int $totalCount
* @param SimpleDomain[] $items
*/
public function __construct($totalCount, array $items)
private function __construct($totalCount, array $items)
{
Assert::integer($totalCount);
Assert::isArray($items);

View File

@ -0,0 +1,94 @@
<?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\Resource\Api\Domain;
use Mailgun\Assert;
use Mailgun\Resource\ApiResponse;
/**
* @author Sean Johnson <sean@mailgun.com>
*/
final class ShowResponse implements ApiResponse
{
/**
* @var Domain
*/
private $domain;
/**
* @var DnsRecord[]
*/
private $inboundDnsRecords;
/**
* @var DnsRecord[]
*/
private $outboundDnsRecords;
/**
* @param array $data
*
* @return self
*/
public static function create(array $data)
{
Assert::keyExists($data, 'domain');
Assert::keyExists($data, 'receiving_dns_records');
Assert::keyExists($data, 'sending_dns_records');
$domain = Domain::create($data['domain']);
$rx = [];
$tx = [];
foreach ($data['receiving_dns_records'] as $item) {
$rx[] = DnsRecord::create($item);
}
foreach ($data['sending_dns_records'] as $item) {
$tx[] = DnsRecord::create($item);
}
return new self($domain, $rx, $tx);
}
/**
* @param Domain $domainInfo
* @param DnsRecord[] $rxRecords
* @param DnsRecord[] $txRecords
*/
private function __construct(Domain $domainInfo, array $rxRecords, array $txRecords)
{
$this->domain = $domainInfo;
$this->inboundDnsRecords = $rxRecords;
$this->outboundDnsRecords = $txRecords;
}
/**
* @return Domain
*/
public function getDomain()
{
return $this->domain;
}
/**
* @return DnsRecord[]
*/
public function getInboundDNSRecords()
{
return $this->inboundDnsRecords;
}
/**
* @return DnsRecord[]
*/
public function getOutboundDNSRecords()
{
return $this->outboundDnsRecords;
}
}

View File

@ -9,14 +9,18 @@
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Assert;
use Mailgun\Resource\CreatableFromArray;
use Mailgun\Resource\Api\SimpleResponse;
use Mailgun\Resource\ApiResponse;
/**
* @author Sean Johnson <sean@mailgun.com>
*/
class DeliverySettingsUpdateResponse extends SimpleResponse implements CreatableFromArray
final class UpdateConnectionResponse implements ApiResponse
{
/**
* @var string
*/
private $message;
/**
* @var bool
*/
@ -30,9 +34,9 @@ class DeliverySettingsUpdateResponse extends SimpleResponse implements Creatable
/**
* @param array $data
*
* @return SettingsUpdateResponse
* @return self
*/
public static function createFromArray(array $data)
public static function create(array $data)
{
Assert::keyExists($data, 'message');
Assert::keyExists($data, 'skip_verification');
@ -46,7 +50,7 @@ class DeliverySettingsUpdateResponse extends SimpleResponse implements Creatable
Assert::boolean($noVerify);
Assert::boolean($requireTLS);
return new static(
return new self(
$message,
$noVerify,
$requireTLS
@ -58,7 +62,7 @@ class DeliverySettingsUpdateResponse extends SimpleResponse implements Creatable
* @param bool $noVerify
* @param bool $requireTLS
*/
public function __construct($message, $noVerify, $requireTLS)
private function __construct($message, $noVerify, $requireTLS)
{
$this->message = $message;
$this->noVerify = $noVerify;

View File

@ -0,0 +1,42 @@
<?php
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Resource\ApiResponse;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class UpdateCredentialResponse implements ApiResponse
{
/**
* @var string
*/
private $message;
/**
* @param string $message
*/
private function __construct($message)
{
$this->message = $message;
}
/**
* @param array $data
*
* @return self
*/
public static function create(array $data)
{
return new self($data['message']);
}
/**
* @return string
*/
public function getMessage()
{
return $this->message;
}
}

View File

@ -1,99 +0,0 @@
<?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\Resource\Api;
use Mailgun\Assert;
use Mailgun\Resource\CreatableFromArray;
/**
* @author Sean Johnson <sean@mailgun.com>
*/
class SimpleResponse implements CreatableFromArray
{
/**
* @var string
*/
private $message;
/**
* Only set when API rate limit is hit and a rate limit response is returned.
*
* @var int
*/
private $retrySeconds = null;
/**
* Only set on calls such as DELETE /v3/domains/.../credentials/<user>.
*
* @var string
*/
private $spec = null;
/**
* @param array $data
*
* @return SimpleResponse
*/
public static function createFromArray(array $data)
{
$message = array_key_exists('message', $data) ? $data['message'] : null;
$retrySeconds = array_key_exists('retry_seconds', $data) ? $data['retry_seconds'] : null;
$spec = array_key_exists('spec', $data) ? $data['spec'] : null;
return new static($message, $retrySeconds, $spec);
}
/**
* @param string|null $message
* @param int|null $retrySeconds
* @param string|null $spec
*/
public function __construct($message, $retrySeconds, $spec)
{
Assert::nullOrString($message);
Assert::nullOrInteger($retrySeconds);
Assert::nullOrString($spec);
$this->message = $message;
$this->retrySeconds = $retrySeconds;
$this->spec = $spec;
}
/**
* @return string
*/
public function getMessage()
{
return $this->message;
}
/**
* @return string
*/
public function getSpec()
{
return $this->spec;
}
/**
* @return bool
*/
public function isRateLimited()
{
return null !== $this->retrySeconds;
}
/**
* @return int
*/
public function getRetrySeconds()
{
return $this->retrySeconds;
}
}

View File

@ -2,12 +2,12 @@
namespace Mailgun\Resource\Api\Stats;
use Mailgun\Resource\CreatableFromArray;
use Mailgun\Resource\ApiResponse;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class AllResponse implements CreatableFromArray
final class AllResponse implements ApiResponse
{
/**
* @var int
@ -15,15 +15,15 @@ class AllResponse implements CreatableFromArray
private $totalCount;
/**
* @var Item[]
* @var AllResponseItem[]
*/
private $items;
/**
* @param int $totalCount
* @param Item[] $items
* @param int $totalCount
* @param AllResponseItem[] $items
*/
public function __construct($totalCount, array $items)
private function __construct($totalCount, array $items)
{
$this->totalCount = $totalCount;
$this->items = $items;
@ -32,13 +32,13 @@ class AllResponse implements CreatableFromArray
/**
* @param array $data
*
* @return AllResponse
* @return self
*/
public static function createFromArray(array $data)
public static function create(array $data)
{
$items = [];
foreach ($data['items'] as $i) {
$items[] = new Item($i['id'], $i['event'], $i['total_count'], $i['tags'], new \DateTime($i['created_at']));
$items[] = AllResponseItem::create($i);
}
return new self($data['total_count'], $items);
@ -53,7 +53,7 @@ class AllResponse implements CreatableFromArray
}
/**
* @return Item[]
* @return AllResponseItem[]
*/
public function getItems()
{

View File

@ -2,7 +2,12 @@
namespace Mailgun\Resource\Api\Stats;
class Item
use Mailgun\Assert;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class AllResponseItem
{
/**
* @var string
@ -29,6 +34,22 @@ class Item
*/
private $createdAt;
/**
* @param array $data
*
* @return self
*/
public static function create(array $data)
{
Assert::string($data['id']);
Assert::string($data['event']);
Assert::string($data['total_count']);
Assert::isArray($data['tags']);
Assert::string($data['created_at']);
return new self($data['id'], $data['event'], $data['total_count'], $data['tags'], new \DateTime($data['created_at']));
}
/**
* @param string $id
* @param string $event
@ -36,7 +57,7 @@ class Item
* @param \string[] $tags
* @param \DateTime $createdAt
*/
public function __construct($id, $event, $totalCount, array $tags, \DateTime $createdAt)
private function __construct($id, $event, $totalCount, array $tags, \DateTime $createdAt)
{
$this->id = $id;
$this->event = $event;

View File

@ -2,12 +2,12 @@
namespace Mailgun\Resource\Api\Stats;
use Mailgun\Resource\CreatableFromArray;
use Mailgun\Resource\ApiResponse;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class TotalResponse implements CreatableFromArray
final class TotalResponse implements ApiResponse
{
/**
* @var \DateTime
@ -25,17 +25,17 @@ class TotalResponse implements CreatableFromArray
private $resolution;
/**
* @var TotalStats[]
* @var TotalResponseItem[]
*/
private $stats;
/**
* @param \DateTime $start
* @param \DateTime $end
* @param string $resolution
* @param TotalStats[] $stats
* @param \DateTime $start
* @param \DateTime $end
* @param string $resolution
* @param TotalResponseItem[] $stats
*/
public function __construct(\DateTime $start, \DateTime $end, $resolution, array $stats)
private function __construct(\DateTime $start, \DateTime $end, $resolution, array $stats)
{
$this->start = $start;
$this->end = $end;
@ -46,13 +46,13 @@ class TotalResponse implements CreatableFromArray
/**
* @param array $data
*
* @return TotalResponse
* @return self
*/
public static function createFromArray(array $data)
public static function create(array $data)
{
$stats = [];
foreach ($data['stats'] as $s) {
$stats[] = new TotalStats(new \DateTime($s['time']), $s['accepted'], $s['delivered'], $s['failed']);
$stats[] = TotalResponseItem::create($s);
}
return new self(new \DateTime($data['start']), new \DateTime($data['end']), $data['resolution'], $stats);
@ -83,7 +83,7 @@ class TotalResponse implements CreatableFromArray
}
/**
* @return TotalStats[]
* @return TotalResponseItem[]
*/
public function getStats()
{

View File

@ -2,10 +2,12 @@
namespace Mailgun\Resource\Api\Stats;
use Mailgun\Assert;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class TotalStats
class TotalResponseItem
{
/**
* @var \DateTime
@ -27,13 +29,28 @@ class TotalStats
*/
private $failed;
/**
* @param array $data
*
* @return self
*/
public static function create(array $data)
{
Assert::string($data['time']);
Assert::isArray($data['accepted']);
Assert::isArray($data['delivered']);
Assert::isArray($data['failed']);
return new self(new \DateTime($data['time']), $data['accepted'], $data['delivered'], $data['failed']);
}
/**
* @param \DateTime $time
* @param array $accepted
* @param array $delivered
* @param array $failed
*/
public function __construct(\DateTime $time, array $accepted, array $delivered, array $failed)
private function __construct(\DateTime $time, array $accepted, array $delivered, array $failed)
{
$this->time = $time;
$this->accepted = $accepted;

View File

@ -0,0 +1,18 @@
<?php
namespace Mailgun\Resource;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
interface ApiResponse
{
/**
* Create an API response object from the HTTP response from the API server.
*
* @param array $data
*
* @return self
*/
public static function create(array $data);
}

View File

@ -1,13 +0,0 @@
<?php
namespace Mailgun\Resource;
interface CreatableFromArray
{
/**
* @param array $data
*
* @return self
*/
public static function createFromArray(array $data);
}

View File

@ -3,7 +3,7 @@
namespace Mailgun\Serializer;
use Mailgun\Exception\SerializeException;
use Mailgun\Resource\CreatableFromArray;
use Mailgun\Resource\ApiResponse;
use Psr\Http\Message\ResponseInterface;
/**
@ -31,7 +31,7 @@ class ObjectSerializer implements ResponseDeserializer
throw new SerializeException(sprintf('Error (%d) when trying to json_decode response', json_last_error()));
}
if (is_subclass_of($class, CreatableFromArray::class)) {
if (is_subclass_of($class, ApiResponse::class)) {
$object = call_user_func($class.'::createFromArray', $data);
} else {
$object = new $class($data);

View File

@ -22,7 +22,7 @@ class StatsTest extends TestCase
$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->method('httpGet')
->with('/v3/domain/stats/total', $data)
->willReturn(new Response());
@ -47,7 +47,7 @@ class StatsTest extends TestCase
$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->method('httpGet')
->with('/v3/domain/stats', $data)
->willReturn(new Response());

View File

@ -58,10 +58,10 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
return $this->getMockBuilder($this->getApiClass())
->setMethods(
[
'get',
'post', 'postRaw', 'postMultipart',
'delete', 'deleteMultipart',
'put', 'putMultipart',
'httpGet',
'httpPost', 'httpPostRaw', 'postMultipart',
'httpDelete', 'deleteMultipart',
'httPut', 'putMultipart',
]
)
->setConstructorArgs([$httpClient, $requestClient, $serializer])

View File

@ -9,13 +9,17 @@
namespace Mailgun\Tests\Integration;
use Mailgun\Api\Domain;
use Mailgun\Resource\Api\Domain\CreateCredentialResponse;
use Mailgun\Resource\Api\Domain\CreateResponse;
use Mailgun\Resource\Api\Domain\DeleteCredentialResponse;
use Mailgun\Resource\Api\Domain\DeleteResponse;
use Mailgun\Resource\Api\Domain\UpdateCredentialResponse;
use Mailgun\Tests\Api\TestCase;
use Mailgun\Resource\Api\SimpleResponse;
use Mailgun\Resource\Api\Domain\Credential;
use Mailgun\Resource\Api\Domain\CredentialListResponse;
use Mailgun\Resource\Api\Domain\DeliverySettingsResponse;
use Mailgun\Resource\Api\Domain\DeliverySettingsUpdateResponse;
use Mailgun\Resource\Api\Domain\SimpleDomain;
use Mailgun\Resource\Api\Domain\CredentialResponseItem;
use Mailgun\Resource\Api\Domain\CredentialResponse;
use Mailgun\Resource\Api\Domain\ConnectionResponse;
use Mailgun\Resource\Api\Domain\UpdateConnectionResponse;
/**
* @author Sean Johnson <sean@mailgun.com>
@ -31,11 +35,11 @@ class DomainApiTest extends TestCase
* Performs `GET /v3/domains` and ensures $this->testDomain exists
* in the returned list.
*/
public function testDomainsList()
public function testIndex()
{
$mg = $this->getMailgunClient();
$domainList = $mg->getDomainApi()->listAll();
$domainList = $mg->getDomainApi()->index();
$found = false;
foreach ($domainList->getDomains() as $domain) {
if ($domain->getName() === $this->testDomain) {
@ -43,7 +47,7 @@ class DomainApiTest extends TestCase
}
}
$this->assertContainsOnlyInstancesOf(SimpleDomain::class, $domainList->getDomains());
$this->assertContainsOnlyInstancesOf(Domain::class, $domainList->getDomains());
$this->assertTrue($found);
}
@ -55,7 +59,7 @@ class DomainApiTest extends TestCase
{
$mg = $this->getMailgunClient();
$domain = $mg->getDomainApi()->info($this->testDomain);
$domain = $mg->getDomainApi()->show($this->testDomain);
$this->assertNotNull($domain);
$this->assertNotNull($domain->getDomain());
$this->assertNotNull($domain->getInboundDNSRecords());
@ -70,9 +74,9 @@ class DomainApiTest extends TestCase
{
$mg = $this->getMailgunClient();
$ret = $mg->getDomainApi()->remove('example.notareal.tld');
$ret = $mg->getDomainApi()->delete('example.notareal.tld');
$this->assertNotNull($ret);
$this->assertInstanceOf(SimpleResponse::class, $ret);
$this->assertInstanceOf(DeleteResponse::class, $ret);
$this->assertEquals('Domain not found', $ret->getMessage());
}
@ -111,7 +115,7 @@ class DomainApiTest extends TestCase
false // wildcard domain?
);
$this->assertNotNull($domain);
$this->assertInstanceOf(SimpleResponse::class, $domain);
$this->assertInstanceOf(CreateResponse::class, $domain);
$this->assertEquals('This domain name is already taken', $domain->getMessage());
}
@ -122,9 +126,9 @@ class DomainApiTest extends TestCase
{
$mg = $this->getMailgunClient();
$ret = $mg->getDomainApi()->remove('example.notareal.tld');
$ret = $mg->getDomainApi()->delete('example.notareal.tld');
$this->assertNotNull($ret);
$this->assertInstanceOf(SimpleResponse::class, $ret);
$this->assertInstanceOf(DeleteResponse::class, $ret);
$this->assertEquals('Domain has been deleted', $ret->getMessage());
}
@ -136,13 +140,13 @@ class DomainApiTest extends TestCase
{
$mg = $this->getMailgunClient();
$ret = $mg->getDomainApi()->newCredential(
$ret = $mg->getDomainApi()->createCredential(
$this->testDomain,
'user-test@'.$this->testDomain,
'Password.01!'
);
$this->assertNotNull($ret);
$this->assertInstanceOf(SimpleResponse::class, $ret);
$this->assertInstanceOf(CreateResponse::class, $ret);
$this->assertEquals('Created 1 credentials pair(s)', $ret->getMessage());
}
@ -156,13 +160,13 @@ class DomainApiTest extends TestCase
{
$mg = $this->getMailgunClient();
$ret = $mg->getDomainApi()->newCredential(
$ret = $mg->getDomainApi()->createCredential(
$this->testDomain,
'user-test',
'ExtremelyLongPasswordThatCertainlyWillNotBeAccepted'
);
$this->assertNotNull($ret);
$this->assertInstanceOf(SimpleResponse::class, $ret);
$this->assertInstanceOf(CreateCredentialResponse::class, $ret);
}
/**
@ -175,13 +179,13 @@ class DomainApiTest extends TestCase
{
$mg = $this->getMailgunClient();
$ret = $mg->getDomainApi()->newCredential(
$ret = $mg->getDomainApi()->createCredential(
$this->testDomain,
'user-test',
'no'
);
$this->assertNotNull($ret);
$this->assertInstanceOf(SimpleResponse::class, $ret);
$this->assertInstanceOf(CreateCredentialResponse::class, $ret);
}
/**
@ -193,10 +197,10 @@ class DomainApiTest extends TestCase
$found = false;
$ret = $mg->getDomainApi()->listCredentials($this->testDomain);
$ret = $mg->getDomainApi()->credentials($this->testDomain);
$this->assertNotNull($ret);
$this->assertInstanceOf(CredentialListResponse::class, $ret);
$this->assertContainsOnlyInstancesOf(Credential::class, $ret->getCredentials());
$this->assertInstanceOf(CredentialResponse::class, $ret);
$this->assertContainsOnlyInstancesOf(CredentialResponseItem::class, $ret->getCredentials());
foreach ($ret->getCredentials() as $cred) {
if ($cred->getLogin() === 'user-test@'.$this->testDomain) {
@ -214,9 +218,9 @@ class DomainApiTest extends TestCase
{
$mg = $this->getMailgunClient();
$ret = $mg->getDomainApi()->listCredentials('mailgun.org');
$ret = $mg->getDomainApi()->credentials('mailgun.org');
$this->assertNotNull($ret);
$this->assertInstanceOf(SimpleResponse::class, $ret);
$this->assertInstanceOf(CredentialResponse::class, $ret);
$this->assertEquals('Domain not found: mailgun.org', $ret->getMessage());
}
@ -236,7 +240,7 @@ class DomainApiTest extends TestCase
'Password..02!'
);
$this->assertNotNull($ret);
$this->assertInstanceOf(SimpleResponse::class, $ret);
$this->assertInstanceOf(UpdateCredentialResponse::class, $ret);
$this->assertEquals('Password changed', $ret->getMessage());
}
@ -293,7 +297,7 @@ class DomainApiTest extends TestCase
$login
);
$this->assertNotNull($ret);
$this->assertInstanceOf(SimpleResponse::class, $ret);
$this->assertInstanceOf(DeleteCredentialResponse::class, $ret);
$this->assertEquals('Credentials have been deleted', $ret->getMessage());
$this->assertEquals($login, $ret->getSpec());
}
@ -313,7 +317,7 @@ class DomainApiTest extends TestCase
$login
);
$this->assertNotNull($ret);
$this->assertInstanceOf(SimpleResponse::class, $ret);
$this->assertInstanceOf(DeleteCredentialResponse::class, $ret);
$this->assertEquals('Credentials not found', $ret->getMessage());
}
@ -324,9 +328,9 @@ class DomainApiTest extends TestCase
{
$mg = $this->getMailgunClient();
$ret = $mg->getDomainApi()->getDeliverySettings($this->testDomain);
$ret = $mg->getDomainApi()->connection($this->testDomain);
$this->assertNotNull($ret);
$this->assertInstanceOf(DeliverySettingsResponse::class, $ret);
$this->assertInstanceOf(ConnectionResponse::class, $ret);
$this->assertTrue(is_bool($ret->getSkipVerification()));
$this->assertTrue(is_bool($ret->getRequireTLS()));
}
@ -338,13 +342,13 @@ class DomainApiTest extends TestCase
{
$mg = $this->getMailgunClient();
$ret = $mg->getDomainApi()->updateDeliverySettings(
$ret = $mg->getDomainApi()->updateConnection(
$this->testDomain,
true,
false
);
$this->assertNotNull($ret);
$this->assertInstanceOf(DeliverySettingsUpdateResponse::class, $ret);
$this->assertInstanceOf(UpdateConnectionResponse::class, $ret);
$this->assertEquals('Domain connection settings have been updated, may take 10 minutes to fully propagate', $ret->getMessage());
$this->assertEquals(true, $ret->getRequireTLS());
$this->assertEquals(false, $ret->getSkipVerification());