From 83adc1b43ed7e4e0a631bad276fc72d212bd07b0 Mon Sep 17 00:00:00 2001 From: Oleksandr Mykhailenko Date: Sun, 25 Feb 2024 15:05:54 +0200 Subject: [PATCH] Template API. In Progress --- src/Api/Templates.php | 101 ++++++++++++++++++++++++++++ src/Mailgun.php | 13 ++++ src/Model/Templates/GetResponse.php | 84 +++++++++++++++++++++++ src/Model/Templates/Template.php | 32 +++++++++ 4 files changed, 230 insertions(+) create mode 100644 src/Api/Templates.php create mode 100644 src/Model/Templates/GetResponse.php create mode 100644 src/Model/Templates/Template.php diff --git a/src/Api/Templates.php b/src/Api/Templates.php new file mode 100644 index 00000000..120098c3 --- /dev/null +++ b/src/Api/Templates.php @@ -0,0 +1,101 @@ + + */ +class Templates extends HttpApi +{ + private const PAGE_NEXT = 'next'; + private const PAGE_FIRST = 'first'; + private const PAGE_PREVIOUS = 'previous'; + private const PAGE_LAST = 'last'; + + /** + * @param string $domain + * @param int $limit + * @param string $page + * @param string $pivot + * @param array $requestHeaders + * @return mixed|ResponseInterface + * @throws ClientExceptionInterface + */ + public function get(string $domain, int $limit, string $page, string $pivot, array $requestHeaders = []) + { + Assert::inArray($page, [self::PAGE_LAST, self::PAGE_FIRST, self::PAGE_PREVIOUS, self::PAGE_NEXT]); + + $params = [ + 'limit' => $limit, + 'skip' => $page, + 'p' => $pivot + ]; + + $response = $this->httpGet(sprintf('/v3/%s/templates', $domain), $params, $requestHeaders); + + return $this->hydrateResponse($response, GetResponse::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 $wildcard domain will accept email for subdomains + * @param bool $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['name'] = $domain; + + + + $response = $this->httpPost('/v3/domains', $params, $requestHeaders); + + return $this->hydrateResponse($response, CreateResponse::class); + } +} diff --git a/src/Mailgun.php b/src/Mailgun.php index 2da25ad6..42db2626 100644 --- a/src/Mailgun.php +++ b/src/Mailgun.php @@ -24,8 +24,10 @@ use Mailgun\Api\Message; use Mailgun\Api\Route; use Mailgun\Api\Stats; +use Mailgun\Api\SubAccounts; use Mailgun\Api\Suppression; use Mailgun\Api\Tag; +use Mailgun\Api\Templates; use Mailgun\Api\Webhook; use Mailgun\HttpClient\HttpClientConfigurator; use Mailgun\HttpClient\Plugin\History; @@ -229,8 +231,19 @@ public function httpClient(): Api\HttpClient return new Api\HttpClient($this->httpClient, $this->requestBuilder, $this->hydrator); } + /** + * @return SubAccounts + */ public function subaccounts(): Api\SubAccounts { return new Api\SubAccounts($this->httpClient, $this->requestBuilder, $this->hydrator); } + + /** + * @return Templates + */ + public function templates(): Templates + { + return new Templates($this->httpClient, $this->requestBuilder, $this->hydrator); + } } diff --git a/src/Model/Templates/GetResponse.php b/src/Model/Templates/GetResponse.php new file mode 100644 index 00000000..bf82e355 --- /dev/null +++ b/src/Model/Templates/GetResponse.php @@ -0,0 +1,84 @@ + + */ +final class GetResponse implements ApiResponse, PagingProvider +{ + use PaginationResponse; + + private $items; + + /** + * @var StreamInterface|null + */ + private $rawStream; + + private function __construct() + { + } + + public static function create(array $data): self + { + $items = []; + foreach ($data['items'] as $item) { + $items[] = Template::create($item); + } + + $model = new self(); + $model->items = $items; + + // Fix http urls that is coming from server + $data['paging'] = array_map( + static function (string $url) { + return str_replace('http://', 'https://', $url); + }, $data['paging'] + ); + + $model->paging = $data['paging']; + + return $model; + } + + /** + * @return Template[] + */ + public function getItems(): array + { + return $this->items; + } + + /** + * Only available with message/rfc2822. + * + * @return StreamInterface|null + */ + public function getRawStream(): ?StreamInterface + { + return $this->rawStream; + } + + /** + * @param StreamInterface|null $rawStream + */ + public function setRawStream(?StreamInterface $rawStream): void + { + $this->rawStream = $rawStream; + } +} diff --git a/src/Model/Templates/Template.php b/src/Model/Templates/Template.php new file mode 100644 index 00000000..6bb27072 --- /dev/null +++ b/src/Model/Templates/Template.php @@ -0,0 +1,32 @@ +