Added PHP7 code and did some refactoring.

This commit is contained in:
Nyholm 2019-01-12 09:54:00 +01:00 committed by David Garcia
parent 2b60fba811
commit 2ec2c16227
13 changed files with 312 additions and 467 deletions

View File

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
@ -13,8 +15,9 @@ use Mailgun\Assert;
use Mailgun\Exception\HttpClientException;
use Mailgun\Exception\HttpServerException;
use Mailgun\Exception\InvalidArgumentException;
use Mailgun\Model\EmailValidation\Response\ParseResponse;
use Mailgun\Model\EmailValidation\Response\ValidateResponse;
use Mailgun\Model\EmailValidation\ParseResponse;
use Mailgun\Model\EmailValidation\ValidateResponse;
use Psr\Http\Message\ResponseInterface;
/**
* @see https://documentation.mailgun.com/en/latest/api-email-validation.html
@ -28,21 +31,20 @@ class EmailValidation extends HttpApi
*
* This operation is only accessible with the private API key and not subject to the daily usage limits.
*
* @param string $address An email address to validate. Maximum: 512 characters.
* @param bool|false $mailboxVerification If set to true, a mailbox verification check will be performed
* against the address. The default is False.
* @param string $address An email address to validate. Maximum: 512 characters.
* @param bool $mailboxVerification If set to true, a mailbox verification check will be performed
* against the address. The default is False.
*
* @throws InvalidArgumentException Thrown when local validation returns an error
* @throws HttpClientException Thrown when there's an error on Client side
* @throws HttpServerException Thrown when there's an error on Server side
* @throws \Exception Thrown when we don't catch a Client or Server side Exception
*
* @return ValidateResponse
* @return ValidateResponse|ResponseInterface
*/
public function validate(string $address, $mailboxVerification = false)
public function validate(string $address, bool $mailboxVerification = false)
{
Assert::stringNotEmpty($address);
Assert::boolean($mailboxVerification);
$params = [
'address' => $address,
@ -67,25 +69,22 @@ class EmailValidation extends HttpApi
*
* This operation is only accessible with the private API key and not subject to the daily usage limits.
*
* @param string $addresses A delimiter separated list of addresses. Maximum: 8000 characters.
* @param bool|false $syntaxOnly Perform only syntax checks or DNS and ESP specific validation as well.
* The default is True.
* @param string $addresses A delimiter separated list of addresses. Maximum: 8000 characters.
* @param bool| $syntaxOnly Perform only syntax checks or DNS and ESP specific validation as well.
* The default is True.
*
* @throws InvalidArgumentException Thrown when local validation returns an error
* @throws HttpClientException Thrown when there's an error on Client side
* @throws HttpServerException Thrown when there's an error on Server side
* @throws \Exception Thrown when we don't catch a Client or Server side Exception
*
* @return ParseResponse
* @return ParseResponse|ResponseInterface
*/
public function parse($addresses, $syntaxOnly = true)
public function parse(string $addresses, bool $syntaxOnly = true)
{
Assert::stringNotEmpty($addresses);
Assert::maxLength($addresses, 8000);
// Validates the Syntax Only verification.
Assert::boolean($syntaxOnly);
$params = [
'addresses' => $addresses,
'syntax_only' => $syntaxOnly,

View File

@ -1,171 +0,0 @@
<?php
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidation;
/**
* @author David Garcia <me@davidgarcia.cat>
*/
final class EmailValidation
{
/**
* @var string|null
*/
private $address;
/**
* @var string|null
*/
private $didYouMean;
/**
* @var bool
*/
private $isDisposableAddress;
/**
* @var bool
*/
private $isRoleAddress;
/**
* @var bool
*/
private $isValid;
/**
* @var bool
*/
private $mailboxVerification;
/**
* @var Parts
*/
private $parts;
/**
* @var string|null
*/
private $reason;
/**
* EmailValidation constructor.
*
* @param string|null $address
* @param string|null $didYouMean
* @param bool $isDisposableAddress
* @param bool $isRoleAddress
* @param bool $isValid
* @param string|null $mailboxVerification
* @param array $parts
* @param string|null $reason
*/
private function __construct(
$address,
$didYouMean,
$isDisposableAddress,
$isRoleAddress,
$isValid,
$mailboxVerification,
$parts,
$reason
) {
$this->address = $address;
$this->didYouMean = $didYouMean;
$this->isDisposableAddress = $isDisposableAddress;
$this->isRoleAddress = $isRoleAddress;
$this->isValid = $isValid;
$this->mailboxVerification = 'true' === $mailboxVerification ? true : false;
$this->parts = Parts::create($parts);
$this->reason = $reason;
}
/**
* @param array $data
*
* @return EmailValidation
*/
public static function create(array $data)
{
return new self(
(isset($data['address']) ? $data['address'] : null),
(isset($data['did_you_mean']) ? $data['did_you_mean'] : null),
(isset($data['is_disposable_address']) ? $data['is_disposable_address'] : false),
(isset($data['is_role_address']) ? $data['is_role_address'] : false),
(isset($data['is_valid']) ? $data['is_valid'] : false),
(isset($data['mailbox_verification']) ? $data['mailbox_verification'] : null),
(isset($data['parts']) ? $data['parts'] : []),
(isset($data['reason']) ? $data['reason'] : null)
);
}
/**
* @return null|string
*/
public function getAddress()
{
return $this->address;
}
/**
* @return null|string
*/
public function getDidYouMean()
{
return $this->didYouMean;
}
/**
* @return bool
*/
public function isDisposableAddress()
{
return $this->isDisposableAddress;
}
/**
* @return bool
*/
public function isRoleAddress()
{
return $this->isRoleAddress;
}
/**
* @return bool
*/
public function isValid()
{
return $this->isValid;
}
/**
* @return bool
*/
public function isMailboxVerification()
{
return $this->mailboxVerification;
}
/**
* @return Parts
*/
public function getParts()
{
return $this->parts;
}
/**
* @return null|string
*/
public function getReason()
{
return $this->reason;
}
}

View File

@ -1,67 +0,0 @@
<?php
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidation;
/**
* @author David Garcia <me@davidgarcia.cat>
*/
final class Parse
{
/**
* @var array
*/
private $parsed;
/**
* @var array
*/
private $unparseable;
/**
* Parse constructor.
*
* @param array $parsed
* @param array $unparseable
*/
private function __construct(array $parsed, array $unparseable)
{
$this->parsed = $parsed;
$this->unparseable = $unparseable;
}
/**
* @param array $data
*
* @return Parse
*/
public static function create(array $data)
{
return new self(
((isset($data['parsed']) && is_array($data['parsed'])) ? $data['parsed'] : []),
((isset($data['unparseable']) && is_array($data['unparseable'])) ? $data['unparseable'] : [])
);
}
/**
* @return array
*/
public function getParsed()
{
return $this->parsed;
}
/**
* @return array
*/
public function getUnparseable()
{
return $this->unparseable;
}
}

View File

@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidation;
use Mailgun\Model\ApiResponse;
/**
* @author David Garcia <me@davidgarcia.cat>
*/
final class ParseResponse implements ApiResponse
{
/**
* @var array
*/
private $parsed;
/**
* @var array
*/
private $unparseable;
private function __construct()
{
}
public static function create(array $data): self
{
$model = new self();
$model->parsed = (isset($data['parsed']) && is_array($data['parsed'])) ? $data['parsed'] : [];
$model->unparseable = (isset($data['unparseable']) && is_array($data['unparseable'])) ? $data['unparseable'] : [];
return $model;
}
public function getParsed(): array
{
return $this->parsed;
}
public function getUnparseable(): array
{
return $this->unparseable;
}
}

View File

@ -30,19 +30,13 @@ final class Parts
private $localPart;
/**
* Parts constructor.
*
* @param string|null $displayName
* @param string|null $domain
* @param string|null $localPart
*/
private function __construct($displayName, $domain, $localPart)
private function __construct()
{
$this->displayName = $displayName;
$this->domain = $domain;
$this->localPart = $localPart;
}
/**
* @param array $data
*
@ -50,17 +44,18 @@ final class Parts
*/
public static function create(array $data)
{
return new self(
(isset($data['display_name']) ? $data['display_name'] : null),
(isset($data['domain']) ? $data['domain'] : null),
(isset($data['local_part']) ? $data['local_part'] : null)
);
$model = new self();
$model->displayName = $data['display_name'] ?? null;
$model->domain = $data['domain'] ?? null;
$model->localPart = $data['local_part'] ?? null;
return $model;
}
/**
* @return null|string
*/
public function getDisplayName()
public function getDisplayName(): ?string
{
return $this->displayName;
}
@ -68,7 +63,7 @@ final class Parts
/**
* @return null|string
*/
public function getDomain()
public function getDomain(): ?string
{
return $this->domain;
}
@ -76,7 +71,7 @@ final class Parts
/**
* @return null|string
*/
public function getLocalPart()
public function getLocalPart(): ?string
{
return $this->localPart;
}

View File

@ -1,68 +0,0 @@
<?php
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidation\Response;
use Mailgun\Model\ApiResponse;
use Mailgun\Model\EmailValidation\Parse;
/**
* @author David Garcia <me@davidgarcia.cat>
*/
final class ParseResponse implements ApiResponse
{
/**
* @var string
*/
private $message;
/**
* @var Parse
*/
private $parse;
/**
* {@inheritdoc}
*/
public static function create(array $data)
{
$message = isset($data['message']) ? $data['message'] : null;
$parse = Parse::create($data);
return new self($message, $parse);
}
/**
* ParseResponse Private Constructor.
*
* @param string|null $message
* @param Parse|null $parse
*/
private function __construct($message = null, Parse $parse = null)
{
$this->message = $message;
$this->parse = $parse;
}
/**
* @return string
*/
public function getMessage()
{
return $this->message;
}
/**
* @return Parse
*/
public function getParse()
{
return $this->parse;
}
}

View File

@ -1,68 +0,0 @@
<?php
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidation\Response;
use Mailgun\Model\ApiResponse;
use Mailgun\Model\EmailValidation\EmailValidation;
/**
* @author David Garcia <me@davidgarcia.cat>
*/
final class ValidateResponse implements ApiResponse
{
/**
* @var string
*/
private $message;
/**
* @var EmailValidation
*/
private $emailValidation;
/**
* {@inheritdoc}
*/
public static function create(array $data)
{
$message = isset($data['message']) ? $data['message'] : null;
$emailValidation = EmailValidation::create($data);
return new self($message, $emailValidation);
}
/**
* CreateResponse Private Constructor.
*
* @param string|null $message
* @param EmailValidation|null $emailValidation
*/
private function __construct($message = null, EmailValidation $emailValidation = null)
{
$this->message = $message;
$this->emailValidation = $emailValidation;
}
/**
* @return string
*/
public function getMessage()
{
return $this->message;
}
/**
* @return EmailValidation
*/
public function getEmailValidation()
{
return $this->emailValidation;
}
}

View File

@ -0,0 +1,120 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidation;
use Mailgun\Model\ApiResponse;
use Mailgun\Model\EmailValidation\EmailValidation;
/**
* @author David Garcia <me@davidgarcia.cat>
*/
final class ValidateResponse implements ApiResponse
{
/**
* @var string|null
*/
private $address;
/**
* @var string|null
*/
private $didYouMean;
/**
* @var bool
*/
private $isDisposableAddress;
/**
* @var bool
*/
private $isRoleAddress;
/**
* @var bool
*/
private $isValid;
/**
* @var bool
*/
private $mailboxVerification;
/**
* @var Parts
*/
private $parts;
/**
* @var string|null
*/
private $reason;
private function __construct()
{
}
public static function create(array $data): self
{
$model = new self();
$model->address = $data['address'] ?? null;
$model->didYouMean = $data['did_you_mean'] ?? null;
$model->isDisposableAddress = $data['is_disposable_address'] ?? false;
$model->isRoleAddress = $data['is_role_address'] ?? false;
$model->isValid = $data['is_valid'] ?? false;
$model->mailboxVerification = isset($data['mailbox_verification']) ? 'true' === $data['mailbox_verification'] : false;
$model->parts = Parts::create($data['parts'] ?? []);
$model->reason = $data['reason'] ?? null;
return $model;
}
public function getAddress(): ?string
{
return $this->address;
}
public function getDidYouMean(): ?string
{
return $this->didYouMean;
}
public function isDisposableAddress(): bool
{
return $this->isDisposableAddress;
}
public function isRoleAddress(): bool
{
return $this->isRoleAddress;
}
public function isValid(): bool
{
return $this->isValid;
}
public function isMailboxVerification(): bool
{
return $this->mailboxVerification;
}
public function getParts(): Parts
{
return $this->parts;
}
public function getReason(): ?string
{
return $this->reason;
}
}

View File

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
@ -24,8 +26,6 @@ class EmailValidationTest extends TestCase
public function testValidEmail()
{
$this->markTestIncomplete('WIP');
$params = [
'address' => 'me@davidgarcia.cat',
'mailbox_verification' => true,
@ -43,8 +43,6 @@ class EmailValidationTest extends TestCase
public function testParseEmail()
{
$this->markTestIncomplete('WIP');
$params = [
'addresses' => 'me@davidgarcia.cat',
'syntax_only' => true,

View File

@ -1,44 +0,0 @@
<?php
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Tests\Model\EmailValidation;
use Mailgun\Model\EmailValidation\EmailValidation;
use Mailgun\Model\EmailValidation\Parts;
use Mailgun\Tests\Model\BaseModelTest;
class EmailValidationTest extends BaseModelTest
{
public function testEmailValidation()
{
$this->markTestIncomplete('WIP');
$data = [
'address' => 'foo@mailgun.net',
'did_you_mean' => null,
'is_disposable_address' => false,
'is_role_address' => false,
'is_valid' => true,
'mailbox_verification' => null,
'parts' => ['display_name' => null, 'domain' => 'mailgun.net', 'local_part' => 'foo'],
'reason' => null,
];
$parts = EmailValidation::create($data);
$this->assertEquals($data['address'], $parts->getAddress());
$this->assertEquals($data['did_you_mean'], $parts->getDidYouMean());
$this->assertEquals($data['is_disposable_address'], $parts->isDisposableAddress());
$this->assertEquals($data['is_role_address'], $parts->isRoleAddress());
$this->assertEquals($data['is_valid'], $parts->isValid());
$this->assertEquals($data['mailbox_verification'], $parts->isMailboxVerification());
$this->assertInstanceOf(Parts::class, $parts->getParts());
$this->assertEquals($data['reason'], $parts->getReason());
}
}

View File

@ -1,29 +1,43 @@
<?php
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
declare(strict_types=1);
namespace Mailgun\Tests\Model\EmailValidation;
use Mailgun\Model\EmailValidation\Parse;
use Mailgun\Model\EmailValidation\ParseResponse;
use Mailgun\Model\EmailValidation\ValidateResponse;
use Mailgun\Tests\Model\BaseModelTest;
class ParseTest extends BaseModelTest
class ParseResponseTest extends BaseModelTest
{
public function testCreate()
{
$json =
<<<'JSON'
{
"parsed": [
"Alice <alice@example.com>",
"bob@example.com"
],
"unparseable": [
]
}
JSON;
$model = ParseResponse::create(json_decode($json, true));
$this->assertNotEmpty($model->getParsed());
$this->assertCount(2, $model->getParsed());
$this->assertEmpty($model->getUnparseable());
}
public function testParseConstructorWithValidData()
{
$this->markTestIncomplete('WIP');
$data = [
'parsed' => ['parsed data'],
'unparseable' => ['unparseable data'],
];
$parts = Parse::create($data);
$parts = ParseResponse::create($data);
$this->assertEquals($data['parsed'], $parts->getParsed());
$this->assertEquals($data['unparseable'], $parts->getUnparseable());
@ -31,16 +45,16 @@ class ParseTest extends BaseModelTest
public function testParseConstructorWithInvalidData()
{
$this->markTestIncomplete('WIP');
$data = [
'parsed' => null,
'unparseable' => null,
];
$parts = Parse::create($data);
$parts = ParseResponse::create($data);
$this->assertEquals([], $parts->getParsed());
$this->assertEquals([], $parts->getUnparseable());
}
}

View File

@ -16,7 +16,6 @@ class PartsTest extends BaseModelTest
{
public function testPartsConstructor()
{
$this->markTestIncomplete('WIP');
$data = [
'display_name' => ' Display name',

View File

@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
namespace Mailgun\Tests\Model\EmailValidation;
use Mailgun\Model\EmailValidation\Parts;
use Mailgun\Model\EmailValidation\ValidateResponse;
use Mailgun\Tests\Model\BaseModelTest;
class ValidateResponseTest extends BaseModelTest
{
public function testCreate()
{
$json =
<<<'JSON'
{
"address": "foo@mailgun.net",
"did_you_mean": null,
"is_disposable_address": false,
"is_role_address": true,
"is_valid": true,
"mailbox_verification": "true",
"parts": {
"display_name": null,
"domain": "mailgun.net",
"local_part": "foo"
}
}
JSON;
$model = ValidateResponse::create(json_decode($json, true));
$this->assertTrue($model->isMailboxVerification());
}
public function testCreateWithoutMailboxVerification()
{
$json =
<<<'JSON'
{
"address": "foo@mailgun.net",
"did_you_mean": null,
"is_disposable_address": false,
"is_role_address": false,
"is_valid": true,
"mailbox_verification": null,
"parts": {
"display_name": null,
"domain": "mailgun.net",
"local_part": "foo"
},
"reason": null
}
JSON;
$model = ValidateResponse::create(json_decode($json, true));
$this->assertFalse($model->isMailboxVerification());
}
public function testEmailValidation()
{
$data = [
'address' => 'foo@mailgun.net',
'did_you_mean' => null,
'is_disposable_address' => false,
'is_role_address' => false,
'is_valid' => true,
'mailbox_verification' => null,
'parts' => ['display_name' => null, 'domain' => 'mailgun.net', 'local_part' => 'foo'],
'reason' => null,
];
$parts = ValidateResponse::create($data);
$this->assertEquals($data['address'], $parts->getAddress());
$this->assertEquals($data['did_you_mean'], $parts->getDidYouMean());
$this->assertEquals($data['is_disposable_address'], $parts->isDisposableAddress());
$this->assertEquals($data['is_role_address'], $parts->isRoleAddress());
$this->assertEquals($data['is_valid'], $parts->isValid());
$this->assertEquals($data['mailbox_verification'], $parts->isMailboxVerification());
$this->assertInstanceOf(Parts::class, $parts->getParts());
$this->assertEquals($data['reason'], $parts->getReason());
}
}