2016-10-27 01:34:27 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2013-2016 Mailgun
|
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms
|
2016-11-24 09:59:05 +01:00
|
|
|
* of the MIT license. See the LICENSE file for details.
|
2016-10-27 01:34:27 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Mailgun\Api;
|
|
|
|
|
|
|
|
use Mailgun\Assert;
|
2016-11-11 19:50:47 -06:00
|
|
|
use Mailgun\Resource\Api\Domain\ConnectionResponse;
|
2016-11-11 21:53:26 +01:00
|
|
|
use Mailgun\Resource\Api\Domain\CreateCredentialResponse;
|
|
|
|
use Mailgun\Resource\Api\Domain\CreateResponse;
|
2016-11-11 19:50:47 -06:00
|
|
|
use Mailgun\Resource\Api\Domain\CredentialResponse;
|
2016-11-11 21:53:26 +01:00
|
|
|
use Mailgun\Resource\Api\Domain\DeleteCredentialResponse;
|
|
|
|
use Mailgun\Resource\Api\Domain\DeleteResponse;
|
2016-11-11 19:50:47 -06:00
|
|
|
use Mailgun\Resource\Api\Domain\IndexResponse;
|
2016-11-11 21:53:26 +01:00
|
|
|
use Mailgun\Resource\Api\Domain\ShowResponse;
|
|
|
|
use Mailgun\Resource\Api\Domain\UpdateConnectionResponse;
|
|
|
|
use Mailgun\Resource\Api\Domain\UpdateCredentialResponse;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2016-10-27 01:34:27 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@link https://documentation.mailgun.com/api-domains.html}.
|
|
|
|
*
|
|
|
|
* @author Sean Johnson <sean@mailgun.com>
|
|
|
|
*/
|
2016-11-11 22:25:32 +01:00
|
|
|
class Domain extends HttpApi
|
2016-10-27 01:34:27 -05:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Returns a list of domains on the account.
|
|
|
|
*
|
|
|
|
* @param int $limit
|
|
|
|
* @param int $skip
|
|
|
|
*
|
2016-11-11 21:53:26 +01:00
|
|
|
* @return IndexResponse
|
2016-10-27 01:34:27 -05:00
|
|
|
*/
|
2016-11-11 21:53:26 +01:00
|
|
|
public function index($limit = 100, $skip = 0)
|
2016-10-27 01:34:27 -05:00
|
|
|
{
|
|
|
|
Assert::integer($limit);
|
|
|
|
Assert::integer($skip);
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
'limit' => $limit,
|
|
|
|
'skip' => $skip,
|
|
|
|
];
|
|
|
|
|
2016-11-11 21:53:26 +01:00
|
|
|
$response = $this->httpGet('/v3/domains', $params);
|
2016-10-27 01:34:27 -05:00
|
|
|
|
2016-12-07 23:29:08 +01:00
|
|
|
return $this->safeDeserialize($response, IndexResponse::class);
|
2016-10-27 01:34:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a single domain.
|
|
|
|
*
|
|
|
|
* @param string $domain Name of the domain.
|
|
|
|
*
|
2016-11-11 21:53:26 +01:00
|
|
|
* @return ShowResponse|array|ResponseInterface
|
2016-10-27 01:34:27 -05:00
|
|
|
*/
|
2016-11-11 21:53:26 +01:00
|
|
|
public function show($domain)
|
2016-10-27 01:34:27 -05:00
|
|
|
{
|
|
|
|
Assert::stringNotEmpty($domain);
|
|
|
|
|
2016-11-11 21:53:26 +01:00
|
|
|
$response = $this->httpGet(sprintf('/v3/domains/%s', $domain));
|
2016-10-27 01:34:27 -05:00
|
|
|
|
2016-12-07 23:29:08 +01:00
|
|
|
return $this->safeDeserialize($response, ShowResponse::class);
|
2016-10-27 01:34:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new domain for the account.
|
|
|
|
* See below for spam filtering parameter information.
|
|
|
|
* {@link https://documentation.mailgun.com/user_manual.html#um-spam-filter}.
|
|
|
|
*
|
|
|
|
* @param string $domain Name of the domain.
|
|
|
|
* @param string $smtpPass Password for SMTP authentication.
|
|
|
|
* @param string $spamAction `disable` or `tag` - inbound spam filtering.
|
|
|
|
* @param bool $wildcard Domain will accept email for subdomains.
|
|
|
|
*
|
2016-11-11 21:53:26 +01:00
|
|
|
* @return CreateResponse|array|ResponseInterface
|
2016-10-27 01:34:27 -05:00
|
|
|
*/
|
|
|
|
public function create($domain, $smtpPass, $spamAction, $wildcard)
|
|
|
|
{
|
|
|
|
Assert::stringNotEmpty($domain);
|
|
|
|
Assert::stringNotEmpty($smtpPass);
|
|
|
|
// TODO(sean.johnson): Extended spam filter input validation.
|
|
|
|
Assert::stringNotEmpty($spamAction);
|
|
|
|
Assert::boolean($wildcard);
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
'name' => $domain,
|
|
|
|
'smtp_password' => $smtpPass,
|
|
|
|
'spam_action' => $spamAction,
|
|
|
|
'wildcard' => $wildcard,
|
|
|
|
];
|
|
|
|
|
2016-11-24 09:59:05 +01:00
|
|
|
$response = $this->httpPost('/v3/domains', $params);
|
2016-10-27 01:34:27 -05:00
|
|
|
|
2016-11-11 21:53:26 +01:00
|
|
|
return $this->safeDeserialize($response, CreateResponse::class);
|
2016-10-27 01:34:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a domain from the account.
|
|
|
|
* WARNING: This action is irreversible! Be cautious!
|
|
|
|
*
|
|
|
|
* @param string $domain Name of the domain.
|
|
|
|
*
|
2016-11-11 21:53:26 +01:00
|
|
|
* @return DeleteResponse|array|ResponseInterface
|
2016-10-27 01:34:27 -05:00
|
|
|
*/
|
2016-11-11 21:53:26 +01:00
|
|
|
public function delete($domain)
|
2016-10-27 01:34:27 -05:00
|
|
|
{
|
|
|
|
Assert::stringNotEmpty($domain);
|
|
|
|
|
2016-11-11 21:53:26 +01:00
|
|
|
$response = $this->httpDelete(sprintf('/v3/domains/%s', $domain));
|
2016-10-27 01:34:27 -05:00
|
|
|
|
2016-12-07 23:29:08 +01:00
|
|
|
return $this->safeDeserialize($response, DeleteResponse::class);
|
2016-10-27 01:34:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of SMTP credentials for the specified domain.
|
|
|
|
*
|
|
|
|
* @param string $domain Name of the domain.
|
|
|
|
* @param int $limit Number of credentials to return
|
|
|
|
* @param int $skip Number of credentials to omit from the list
|
|
|
|
*
|
2016-11-11 21:53:26 +01:00
|
|
|
* @return CredentialResponse
|
2016-10-27 01:34:27 -05:00
|
|
|
*/
|
2016-11-11 21:53:26 +01:00
|
|
|
public function credentials($domain, $limit = 100, $skip = 0)
|
2016-10-27 01:34:27 -05:00
|
|
|
{
|
|
|
|
Assert::stringNotEmpty($domain);
|
|
|
|
Assert::integer($limit);
|
|
|
|
Assert::integer($skip);
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
'limit' => $limit,
|
|
|
|
'skip' => $skip,
|
|
|
|
];
|
|
|
|
|
2016-11-11 21:53:26 +01:00
|
|
|
$response = $this->httpGet(sprintf('/v3/domains/%s/credentials', $domain), $params);
|
2016-10-27 01:34:27 -05:00
|
|
|
|
2016-11-11 21:53:26 +01:00
|
|
|
return $this->safeDeserialize($response, CredentialResponse::class);
|
2016-10-27 01:34:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new SMTP credential pair for the specified domain.
|
|
|
|
*
|
|
|
|
* @param string $domain Name of the domain.
|
|
|
|
* @param string $login SMTP Username.
|
|
|
|
* @param string $password SMTP Password. Length min 5, max 32.
|
|
|
|
*
|
2016-11-11 21:53:26 +01:00
|
|
|
* @return CreateCredentialResponse|array|ResponseInterface
|
2016-10-27 01:34:27 -05:00
|
|
|
*/
|
2016-11-11 21:53:26 +01:00
|
|
|
public function createCredential($domain, $login, $password)
|
2016-10-27 01:34:27 -05:00
|
|
|
{
|
|
|
|
Assert::stringNotEmpty($domain);
|
|
|
|
Assert::stringNotEmpty($login);
|
|
|
|
Assert::stringNotEmpty($password);
|
|
|
|
Assert::lengthBetween($password, 5, 32, 'SMTP password must be between 5 and 32 characters.');
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
'login' => $login,
|
|
|
|
'password' => $password,
|
|
|
|
];
|
|
|
|
|
2016-11-23 21:55:05 +01:00
|
|
|
$response = $this->httpPost(sprintf('/v3/domains/%s/credentials', $domain), $params);
|
2016-10-27 01:34:27 -05:00
|
|
|
|
2016-12-07 23:29:08 +01:00
|
|
|
return $this->safeDeserialize($response, CreateCredentialResponse::class);
|
2016-10-27 01:34:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a set of SMTP credentials for the specified domain.
|
|
|
|
*
|
|
|
|
* @param string $domain Name of the domain.
|
|
|
|
* @param string $login SMTP Username.
|
|
|
|
* @param string $pass New SMTP Password. Length min 5, max 32.
|
|
|
|
*
|
2016-11-11 21:53:26 +01:00
|
|
|
* @return UpdateCredentialResponse|array|ResponseInterface
|
2016-10-27 01:34:27 -05:00
|
|
|
*/
|
|
|
|
public function updateCredential($domain, $login, $pass)
|
|
|
|
{
|
|
|
|
Assert::stringNotEmpty($domain);
|
|
|
|
Assert::stringNotEmpty($login);
|
|
|
|
Assert::stringNotEmpty($pass);
|
|
|
|
Assert::lengthBetween($pass, 5, 32, 'SMTP password must be between 5 and 32 characters.');
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
'password' => $pass,
|
|
|
|
];
|
|
|
|
|
2016-11-23 21:55:05 +01:00
|
|
|
$response = $this->httpPut(sprintf('/v3/domains/%s/credentials/%s', $domain, $login), $params);
|
2016-10-27 01:34:27 -05:00
|
|
|
|
2016-12-07 23:29:08 +01:00
|
|
|
return $this->safeDeserialize($response, UpdateCredentialResponse::class);
|
2016-10-27 01:34:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a set of SMTP credentials from the specified domain.
|
|
|
|
*
|
|
|
|
* @param string $domain Name of the domain.
|
|
|
|
* @param string $login SMTP Username.
|
|
|
|
*
|
2016-11-11 21:53:26 +01:00
|
|
|
* @return DeleteCredentialResponse|array|ResponseInterface
|
2016-10-27 01:34:27 -05:00
|
|
|
*/
|
|
|
|
public function deleteCredential($domain, $login)
|
|
|
|
{
|
|
|
|
Assert::stringNotEmpty($domain);
|
|
|
|
Assert::stringNotEmpty($login);
|
|
|
|
|
2016-11-11 19:50:47 -06:00
|
|
|
$response = $this->httpDelete(
|
2016-10-27 01:34:27 -05:00
|
|
|
sprintf(
|
|
|
|
'/v3/domains/%s/credentials/%s',
|
|
|
|
$domain,
|
|
|
|
$login
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2016-12-07 23:29:08 +01:00
|
|
|
return $this->safeDeserialize($response, DeleteCredentialResponse::class);
|
2016-10-27 01:34:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns delivery connection settings for the specified domain.
|
|
|
|
*
|
|
|
|
* @param string $domain Name of the domain.
|
|
|
|
*
|
2016-11-11 21:53:26 +01:00
|
|
|
* @return ConnectionResponse|array|ResponseInterface
|
2016-10-27 01:34:27 -05:00
|
|
|
*/
|
2016-11-11 21:53:26 +01:00
|
|
|
public function connection($domain)
|
2016-10-27 01:34:27 -05:00
|
|
|
{
|
|
|
|
Assert::stringNotEmpty($domain);
|
|
|
|
|
2016-11-11 21:53:26 +01:00
|
|
|
$response = $this->httpGet(sprintf('/v3/domains/%s/connection', $domain));
|
2016-10-27 01:34:27 -05:00
|
|
|
|
2016-12-07 23:29:08 +01:00
|
|
|
return $this->safeDeserialize($response, ConnectionResponse::class);
|
2016-10-27 01:34:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the specified delivery connection settings for the specified domain.
|
|
|
|
* If a parameter is passed in as null, it will not be updated.
|
|
|
|
*
|
|
|
|
* @param string $domain Name of the domain.
|
|
|
|
* @param bool|null $requireTLS Enforces that messages are sent only over a TLS connection.
|
|
|
|
* @param bool|null $noVerify Disables TLS certificate and hostname verification.
|
|
|
|
*
|
2016-11-11 21:53:26 +01:00
|
|
|
* @return UpdateConnectionResponse|array|ResponseInterface
|
2016-10-27 01:34:27 -05:00
|
|
|
*/
|
2016-11-11 21:53:26 +01:00
|
|
|
public function updateConnection($domain, $requireTLS, $noVerify)
|
2016-10-27 01:34:27 -05:00
|
|
|
{
|
|
|
|
Assert::stringNotEmpty($domain);
|
|
|
|
Assert::nullOrBoolean($requireTLS);
|
|
|
|
Assert::nullOrBoolean($noVerify);
|
|
|
|
|
|
|
|
$params = [];
|
|
|
|
|
|
|
|
if (null !== $requireTLS) {
|
|
|
|
$params['require_tls'] = $requireTLS ? 'true' : 'false';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== $noVerify) {
|
|
|
|
$params['skip_verification'] = $noVerify ? 'true' : 'false';
|
|
|
|
}
|
|
|
|
|
2016-11-24 09:59:05 +01:00
|
|
|
$response = $this->httpPut(sprintf('/v3/domains/%s/connection', $domain), $params);
|
2016-10-27 01:34:27 -05:00
|
|
|
|
2016-12-07 23:29:08 +01:00
|
|
|
return $this->safeDeserialize($response, UpdateConnectionResponse::class);
|
2016-10-27 01:34:27 -05:00
|
|
|
}
|
|
|
|
}
|