Do not validate API responses. Create Response objects that have part… (#230)

* Do not validate API responses. Create Response objects that have partial data instead

Fix issue #225

* Code style
This commit is contained in:
Tobias Nyholm 2016-12-06 07:44:59 +01:00 committed by Sean Johnson
parent adfc1d7bd2
commit d12ea9f456
17 changed files with 152 additions and 162 deletions

View File

@ -7,9 +7,9 @@
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Assert;
use Mailgun\Resource\ApiResponse;
/**
@ -34,16 +34,14 @@ final class ConnectionResponse implements ApiResponse
*/
public static function create(array $data)
{
Assert::keyExists($data, 'connection');
Assert::isArray($data['connection']);
if (!isset($data['connection'])) {
return;
}
$connSettings = $data['connection'];
Assert::keyExists($connSettings, 'skip_verification');
Assert::keyExists($connSettings, 'require_tls');
return new self(
$connSettings['skip_verification'],
$connSettings['require_tls']
isset($connSettings['skip_verification']) ? $connSettings['skip_verification'] : null,
isset($connSettings['require_tls']) ? $connSettings['require_tls'] : null
);
}

View File

@ -7,6 +7,7 @@
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Resource\ApiResponse;
@ -36,7 +37,7 @@ final class CreateCredentialResponse implements ApiResponse
*/
public static function create(array $data)
{
return new self($data['message']);
return new self(isset($data['message']) ? $data['message'] : null);
}
/**

View File

@ -7,9 +7,9 @@
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Assert;
use Mailgun\Resource\ApiResponse;
/**
@ -44,23 +44,32 @@ final class CreateResponse implements ApiResponse
*/
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 = [];
$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, $data['message']);
return new self($domain, $rx, $tx, $message);
}
/**

View File

@ -7,11 +7,10 @@
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Assert;
use Mailgun\Resource\ApiResponse;
use Mailgun\Resource\Api\ErrorResponse;
/**
* @author Sean Johnson <sean@mailgun.com>
@ -35,17 +34,20 @@ final class CredentialResponse implements ApiResponse
*/
public static function create(array $data)
{
if (array_key_exists('items', $data) && array_key_exists('total_count', $data)) {
$items = [];
if (isset($data['items'])) {
foreach ($data['items'] as $item) {
$items[] = CredentialResponseItem::create($item);
}
return new self($data['total_count'], $items);
} else {
return ErrorResponse::create($data);
}
if (isset($data['total_count'])) {
$count = $data['total_count'];
} else {
$count = count($items);
}
return new self($count, $items);
}
/**
@ -54,10 +56,6 @@ final class CredentialResponse implements ApiResponse
*/
private function __construct($totalCount, array $items)
{
Assert::integer($totalCount);
Assert::isArray($items);
Assert::allIsInstanceOf($items, 'Mailgun\Resource\Api\Domain\CredentialResponseItem');
$this->totalCount = $totalCount;
$this->items = $items;
}

View File

@ -7,9 +7,8 @@
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Assert;
namespace Mailgun\Resource\Api\Domain;
/**
* @author Sean Johnson <sean@mailgun.com>
@ -43,26 +42,12 @@ final class CredentialResponseItem
*/
public static function create(array $data)
{
Assert::keyExists($data, 'created_at');
Assert::keyExists($data, 'mailbox');
Assert::keyExists($data, 'login');
$sizeBytes = isset($data['size_bytes']) ? $data['size_bytes'] : null;
$mailbox = isset($data['mailbox']) ? $data['mailbox'] : null;
$login = isset($data['login']) ? $data['login'] : null;
$createdAt = isset($data['created_at']) ? new \DateTime($data['created_at']) : null;
$sizeBytes = array_key_exists('size_bytes', $data) ? $data['size_bytes'] : null;
$createdAt = new \DateTime($data['created_at']);
$mailbox = $data['mailbox'];
$login = $data['login'];
Assert::nullOrInteger($sizeBytes);
Assert::isInstanceOf($createdAt, '\DateTime');
Assert::string($mailbox);
Assert::string($login);
return new self(
$sizeBytes,
$createdAt,
$mailbox,
$login
);
return new self($sizeBytes, $createdAt, $mailbox, $login);
}
/**

View File

@ -7,6 +7,7 @@
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Resource\ApiResponse;
@ -22,7 +23,7 @@ final class DeleteCredentialResponse implements ApiResponse
private $message;
/**
* @var error
* @var string
*/
private $error;
@ -33,6 +34,8 @@ final class DeleteCredentialResponse implements ApiResponse
/**
* @param string $message
* @param string $error
* @param string $spec
*/
private function __construct($message, $error, $spec)
{

View File

@ -7,9 +7,8 @@
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Assert;
namespace Mailgun\Resource\Api\Domain;
/**
* Represents a single DNS record for a domain.
@ -52,14 +51,11 @@ final class DnsRecord
{
$name = isset($data['name']) ? $data['name'] : null;
$priority = isset($data['priority']) ? $data['priority'] : null;
$recordType = isset($data['record_type']) ? $data['record_type'] : null;
$value = isset($data['value']) ? $data['value'] : null;
$valid = isset($data['valid']) ? $data['valid'] : null;
Assert::nullOrString($name);
Assert::string($data['record_type']);
Assert::string($data['value']);
Assert::nullOrString($priority);
Assert::string($data['valid']);
return new self($name, $data['record_type'], $data['value'], $priority, $data['valid']);
return new self($name, $recordType, $value, $priority, $valid);
}
/**

View File

@ -7,9 +7,8 @@
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Assert;
namespace Mailgun\Resource\Api\Domain;
/**
* Represents domain information in its simplest form.
@ -60,22 +59,14 @@ final class Domain
*/
public static function create(array $data)
{
Assert::keyExists($data, 'name');
Assert::keyExists($data, 'smtp_login');
Assert::keyExists($data, 'smtp_password');
Assert::keyExists($data, 'wildcard');
Assert::keyExists($data, 'spam_action');
Assert::keyExists($data, 'state');
Assert::keyExists($data, 'created_at');
return new self(
$data['name'],
$data['smtp_login'],
$data['smtp_password'],
$data['wildcard'],
$data['spam_action'],
$data['state'],
new \DateTime($data['created_at'])
isset($data['name']) ? $data['name'] : null,
isset($data['smtp_login']) ? $data['smtp_login'] : null,
isset($data['smtp_password']) ? $data['smtp_password'] : null,
isset($data['wildcard']) ? $data['wildcard'] : null,
isset($data['spam_action']) ? $data['spam_action'] : null,
isset($data['state']) ? $data['state'] : null,
isset($data['created_at']) ? new \DateTime($data['created_at']) : null
);
}

View File

@ -10,7 +10,6 @@
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Assert;
use Mailgun\Resource\ApiResponse;
/**
@ -37,14 +36,19 @@ final class IndexResponse implements ApiResponse
{
$items = [];
Assert::keyExists($data, 'total_count');
Assert::keyExists($data, 'items');
if (isset($data['items'])) {
foreach ($data['items'] as $item) {
$items[] = Domain::create($item);
}
}
return new self($data['total_count'], $items);
if (isset($data['total_count'])) {
$count = $data['total_count'];
} else {
$count = count($items);
}
return new self($count, $items);
}
/**
@ -53,10 +57,6 @@ final class IndexResponse implements ApiResponse
*/
private function __construct($totalCount, array $items)
{
Assert::integer($totalCount);
Assert::isArray($items);
Assert::allIsInstanceOf($items, 'Mailgun\Resource\Api\Domain\Domain');
$this->totalCount = $totalCount;
$this->items = $items;
}

View File

@ -7,9 +7,9 @@
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Assert;
use Mailgun\Resource\ApiResponse;
/**
@ -39,20 +39,25 @@ final class ShowResponse implements ApiResponse
*/
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 = [];
$domain = null;
if (isset($data['domain'])) {
$domain = Domain::create($data['domain']);
}
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);
}

View File

@ -7,9 +7,9 @@
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Assert;
use Mailgun\Resource\ApiResponse;
/**
@ -39,23 +39,11 @@ final class UpdateConnectionResponse implements ApiResponse
*/
public static function create(array $data)
{
Assert::keyExists($data, 'message');
Assert::keyExists($data, 'skip_verification');
Assert::keyExists($data, 'require_tls');
$message = isset($data['message']) ? $data['message'] : null;
$noVerify = isset($data['skip_verification']) ? $data['skip_verification'] : null;
$requireTLS = isset($data['require_tls']) ? $data['require_tls'] : null;
$message = $data['message'];
$noVerify = $data['skip_verification'];
$requireTLS = $data['require_tls'];
Assert::nullOrString($message);
Assert::boolean($noVerify);
Assert::boolean($requireTLS);
return new self(
$message,
$noVerify,
$requireTLS
);
return new self($message, $noVerify, $requireTLS);
}
/**

View File

@ -7,6 +7,7 @@
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Domain;
use Mailgun\Resource\ApiResponse;
@ -36,7 +37,7 @@ final class UpdateCredentialResponse implements ApiResponse
*/
public static function create(array $data)
{
return new self($data['message']);
return new self(isset($data['message']) ? $data['message'] : null);
}
/**

View File

@ -7,6 +7,7 @@
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api;
use Mailgun\Resource\ApiResponse;
@ -33,7 +34,7 @@ final class ErrorResponse implements ApiResponse
}
/**
* @param array data
* @param array
*
* @return self
*/

View File

@ -7,6 +7,7 @@
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Stats;
use Mailgun\Resource\ApiResponse;
@ -44,11 +45,19 @@ final class AllResponse implements ApiResponse
public static function create(array $data)
{
$items = [];
if (isset($data['items'])) {
foreach ($data['items'] as $i) {
$items[] = AllResponseItem::create($i);
}
}
return new self($data['total_count'], $items);
if (isset($data['total_count'])) {
$count = $data['total_count'];
} else {
$count = count($items);
}
return new self($count, $items);
}
/**

View File

@ -7,9 +7,8 @@
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Stats;
use Mailgun\Assert;
namespace Mailgun\Resource\Api\Stats;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
@ -48,13 +47,13 @@ final class AllResponseItem
*/
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']));
return new self(
isset($data['id']) ? $data['id'] : null,
isset($data['event']) ? $data['event'] : null,
isset($data['total_count']) ? $data['total_count'] : null,
isset($data['tags']) ? $data['tags'] : null,
isset($data['created_at']) ? new \DateTime($data['created_at']) : null
);
}
/**

View File

@ -7,6 +7,7 @@
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Stats;
use Mailgun\Resource\ApiResponse;
@ -58,11 +59,17 @@ final class TotalResponse implements ApiResponse
public static function create(array $data)
{
$stats = [];
if (isset($data['status'])) {
foreach ($data['stats'] as $s) {
$stats[] = TotalResponseItem::create($s);
}
}
return new self(new \DateTime($data['start']), new \DateTime($data['end']), $data['resolution'], $stats);
$start = isset($data['start']) ? new \DateTime($data['start']) : null;
$end = isset($data['end']) ? new \DateTime($data['end']) : null;
$resolution = isset($data['resolution']) ? $data['resolution'] : null;
return new self($start, $end, $resolution, $stats);
}
/**

View File

@ -7,9 +7,8 @@
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Stats;
use Mailgun\Assert;
namespace Mailgun\Resource\Api\Stats;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
@ -43,12 +42,12 @@ class TotalResponseItem
*/
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']);
return new self(
isset($data['time']) ? new \DateTime($data['time']) : null,
isset($data['accepted']) ? $data['accepted'] : null,
isset($data['delivered']) ? $data['delivered'] : null,
isset($data['failed']) ? $data['failed'] : null
);
}
/**