diff --git a/src/Api/DomainV4.php b/src/Api/DomainV4.php new file mode 100644 index 00000000..c3f9641e --- /dev/null +++ b/src/Api/DomainV4.php @@ -0,0 +1,220 @@ + $limit, + 'skip' => $skip, + ]; + + $response = $this->httpGet('/v4/domains', $params, $requestHeaders); + + return $this->hydrateResponse($response, IndexResponse::class); + } + + /** + * Returns a single domain. + * @param string $domain name of the domain + * @param array $requestHeaders + * @return ShowResponse|array|ResponseInterface + * @throws ClientExceptionInterface + */ + public function show(string $domain, array $requestHeaders = []) + { + Assert::stringNotEmpty($domain); + + $response = $this->httpGet(sprintf('/v4/domains/%s', $domain), [], $requestHeaders); + + return $this->hydrateResponse($response, ShowResponse::class); + } + + /** + * Creates a new domain for the account. + * See below for spam filtering parameter information. + * {@link https://documentation.mailgun.com/en/latest/user_manual.html#um-spam-filter}. + * @see https://documentation.mailgun.com/en/latest/api-domains.html#domains + * @param string $domain name of the domain + * @param string|null $smtpPass password for SMTP authentication + * @param string|null $spamAction `disable` or `tag` - inbound spam filtering + * @param bool|null $wildcard domain will accept email for subdomains + * @param bool|null $forceDkimAuthority force DKIM authority + * @param string[] $ips an array of ips to be assigned to the domain + * @param ?string $pool_id pool id to assign to the domain + * @param string $webScheme `http` or `https` - set your open, click and unsubscribe URLs to use http or https. The default is http + * @param string $dkimKeySize Set length of your domain’s generated DKIM + * key + * @return CreateResponse|array|ResponseInterface + * @throws Exception + */ + public function create( + string $domain, + string $smtpPass = null, + string $spamAction = null, + ?bool $wildcard = null, + ?bool $forceDkimAuthority = null, + ?array $ips = null, + ?string $pool_id = null, + string $webScheme = 'http', + string $dkimKeySize = '1024', + array $requestHeaders = [] + ) { + Assert::stringNotEmpty($domain); + + $params = []; + + $params['name'] = $domain; + + if (!empty($smtpPass)) { + Assert::stringNotEmpty($smtpPass); + $params['smtp_password'] = $smtpPass; + } + + if (!empty($spamAction)) { + Assert::stringNotEmpty($spamAction); + $params['spam_action'] = $spamAction; + } + + if (null !== $wildcard) { + $params['wildcard'] = $wildcard ? 'true' : 'false'; + } + + if (null !== $forceDkimAuthority) { + $params['force_dkim_authority'] = $forceDkimAuthority ? 'true' : 'false'; + } + + if (null !== $ips) { + Assert::isList($ips); + Assert::allString($ips); + + $params['ips'] = implode(',', $ips); + } + + if (!empty($webScheme)) { + Assert::stringNotEmpty($webScheme); + Assert::oneOf($webScheme, ['https', 'http']); + $params['web_scheme'] = $webScheme; + } + + if (null !== $pool_id) { + Assert::stringNotEmpty($pool_id); + + $params['pool_id'] = $pool_id; + } + if (!empty($dkimKeySize)) { + Assert::oneOf( + $dkimKeySize, + self::DKIM_SIZES, + 'Length of your domain’s generated DKIM key must be 1024 or 2048' + ); + $params['dkim_key_size'] = $dkimKeySize; + } + + $response = $this->httpPost('/v4/domains', $params, $requestHeaders); + + return $this->hydrateResponse($response, CreateResponse::class); + } + + /** + * Removes a domain from the account. + * WARNING: This action is irreversible! Be cautious! + * @param string $domain name of the domain + * @param array $requestHeaders + * @return DeleteResponse|array|ResponseInterface + * @throws ClientExceptionInterface + */ + public function delete(string $domain, array $requestHeaders = []) + { + Assert::stringNotEmpty($domain); + + $response = $this->httpDelete(sprintf('/v4/domains/%s', $domain), [], $requestHeaders); + + return $this->hydrateResponse($response, DeleteResponse::class); + } + + /** + * Update webScheme for existing domain + * See below for spam filtering parameter information. + * {@link https://documentation.mailgun.com/en/latest/user_manual.html#um-spam-filter}. + * @see https://documentation.mailgun.com/en/latest/api-domains.html#domains + * @param string $domain name of the domain + * @param string $webScheme `http` or `https` - set your open, click and unsubscribe URLs to use http or https. The default is http + * @param array $requestHeaders + * @return WebSchemeResponse|array|ResponseInterface + * @throws ClientExceptionInterface + */ + public function updateWebScheme(string $domain, string $webScheme = 'http', array $requestHeaders = []) + { + $params = []; + Assert::stringNotEmpty($domain); + Assert::stringNotEmpty($webScheme); + Assert::oneOf($webScheme, ['https', 'http']); + + $params['web_scheme'] = $webScheme; + + $response = $this->httpPut(sprintf('/v4/domains/%s', $domain), $params, $requestHeaders); + + return $this->hydrateResponse($response, WebSchemeResponse::class); + } + + /** + * Returns a single domain. + * @param string $domain name of the domain + * @param array $requestHeaders + * @return VerifyResponse|array|ResponseInterface + * @throws ClientExceptionInterface + */ + public function verify(string $domain, array $requestHeaders = []) + { + Assert::stringNotEmpty($domain); + + $response = $this->httpPut(sprintf('/v4/domains/%s/verify', $domain), [], $requestHeaders); + + return $this->hydrateResponse($response, VerifyResponse::class); + } +} diff --git a/src/Mailgun.php b/src/Mailgun.php index 42db2626..b7649a5e 100644 --- a/src/Mailgun.php +++ b/src/Mailgun.php @@ -127,6 +127,14 @@ public function domains(): Api\Domain return new Api\Domain($this->httpClient, $this->requestBuilder, $this->hydrator); } + /** + * @return Api\DomainV4 + */ + public function domainsV4(): Api\DomainV4 + { + return new Api\DomainV4($this->httpClient, $this->requestBuilder, $this->hydrator); + } + /** * @return EmailValidation */