|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * Copyright (C) 2013 Mailgun |
| 7 | + * |
| 8 | + * This software may be modified and distributed under the terms |
| 9 | + * of the MIT license. See the LICENSE file for details. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Mailgun\Api; |
| 13 | + |
| 14 | +use Exception; |
| 15 | +use Mailgun\Assert; |
| 16 | +use Mailgun\Model\Domain\DeleteResponse; |
| 17 | +use Mailgun\Model\Domain\DomainKeyResponse; |
| 18 | +use Mailgun\Model\Domain\IndexResponse; |
| 19 | +use Psr\Http\Client\ClientExceptionInterface; |
| 20 | +use Psr\Http\Message\ResponseInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * @see https://documentation.mailgun.com/docs/mailgun/api-reference/openapi-final/tag/Domain-Keys/ |
| 24 | + * |
| 25 | + */ |
| 26 | +class DomainKeys extends HttpApi |
| 27 | +{ |
| 28 | + private const BITS_SIZE = ['1024', '2048']; |
| 29 | + |
| 30 | + /** |
| 31 | + * Returns a list of domains on the account. |
| 32 | + * @param int|null $limit |
| 33 | + * @param string|null $page |
| 34 | + * @param string|null $signingDomain |
| 35 | + * @param string|null $selector |
| 36 | + * @param array $requestHeaders |
| 37 | + * @return IndexResponse|array |
| 38 | + * @throws ClientExceptionInterface |
| 39 | + * @throws \JsonException |
| 40 | + * @throws Exception |
| 41 | + */ |
| 42 | + public function listKeysForDomains(?int $limit = null, ?string $page = null, ?string $signingDomain = null, ?string $selector = null, array $requestHeaders = []) |
| 43 | + { |
| 44 | + $params = []; |
| 45 | + if (isset($limit)) { |
| 46 | + Assert::range($limit, 1, 1000); |
| 47 | + $params['limit'] = $limit; |
| 48 | + } |
| 49 | + |
| 50 | + if (isset($page)) { |
| 51 | + Assert::stringNotEmpty($page); |
| 52 | + $params['page'] = $page; |
| 53 | + } |
| 54 | + |
| 55 | + if (isset($signingDomain)) { |
| 56 | + $params['signing_domain'] = $signingDomain; |
| 57 | + } |
| 58 | + |
| 59 | + if (isset($selector)) { |
| 60 | + $params['selector'] = $signingDomain; |
| 61 | + } |
| 62 | + |
| 63 | + $response = $this->httpGet('/v1/dkim/keys', $params, $requestHeaders); |
| 64 | + |
| 65 | + return $this->hydrateResponse($response, IndexResponse::class); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns a list of domains on the account. |
| 70 | + * @param string $authorityName |
| 71 | + * @param array $requestHeaders |
| 72 | + * @return IndexResponse|array |
| 73 | + * @throws ClientExceptionInterface |
| 74 | + * @throws \JsonException |
| 75 | + * @throws Exception |
| 76 | + */ |
| 77 | + public function listDomainKeys(string $authorityName, array $requestHeaders = []) |
| 78 | + { |
| 79 | + Assert::stringNotEmpty($authorityName); |
| 80 | + |
| 81 | + $response = $this->httpGet(sprintf('/v4/domains/%s/keys', $authorityName), [], $requestHeaders); |
| 82 | + |
| 83 | + return $this->hydrateResponse($response, DomainKeyResponse::class); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @param string $signingDomain |
| 88 | + * @param string $selector |
| 89 | + * @param string|null $bits |
| 90 | + * @param array $requestHeaders |
| 91 | + * @return mixed|ResponseInterface |
| 92 | + * @throws ClientExceptionInterface |
| 93 | + * @throws \JsonException |
| 94 | + * @throws Exception |
| 95 | + */ |
| 96 | + public function createDomainKey(string $signingDomain, string $selector, ?string $bits = null, array $requestHeaders = []) |
| 97 | + { |
| 98 | + Assert::stringNotEmpty($signingDomain); |
| 99 | + Assert::stringNotEmpty($selector); |
| 100 | + |
| 101 | + $params = [ |
| 102 | + 'signing_domain' => $signingDomain, |
| 103 | + 'selector' => $selector, |
| 104 | + ]; |
| 105 | + |
| 106 | + if (!empty($bits)) { |
| 107 | + Assert::oneOf( |
| 108 | + $bits, |
| 109 | + self::BITS_SIZE, |
| 110 | + 'Length of your domain’s generated DKIM key must be 1024 or 2048' |
| 111 | + ); |
| 112 | + $params['bits'] = $bits; |
| 113 | + } |
| 114 | + |
| 115 | + $response = $this->httpPost('/v1/dkim/keys', $params, $requestHeaders); |
| 116 | + |
| 117 | + return $this->hydrateResponse($response, DomainKeyResponse::class); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @param string $signingDomain |
| 122 | + * @param string $selector |
| 123 | + * @param array $requestHeaders |
| 124 | + * @return mixed|ResponseInterface |
| 125 | + * @throws ClientExceptionInterface |
| 126 | + * @throws \JsonException |
| 127 | + */ |
| 128 | + public function deleteDomainKey(string $signingDomain, string $selector, array $requestHeaders = []) |
| 129 | + { |
| 130 | + Assert::stringNotEmpty($signingDomain); |
| 131 | + Assert::stringNotEmpty($selector); |
| 132 | + |
| 133 | + $params = [ |
| 134 | + 'signing_domain' => $signingDomain, |
| 135 | + 'selector' => $selector, |
| 136 | + ]; |
| 137 | + |
| 138 | + $response = $this->httpDelete('/v1/dkim/keys', $params, $requestHeaders); |
| 139 | + |
| 140 | + return $this->hydrateResponse($response, DeleteResponse::class); |
| 141 | + } |
| 142 | +} |
0 commit comments