diff --git a/CHANGELOG.md b/CHANGELOG.md index ceed41e3..da34df44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release. +## 4.2.0 +- Added basic templates functionality + ## 4.0.1 - Fix wrong classes in tests - Fixed response in case of 404 http error. Respect server error message diff --git a/src/Api/Templates.php b/src/Api/Templates.php new file mode 100644 index 00000000..ec665141 --- /dev/null +++ b/src/Api/Templates.php @@ -0,0 +1,151 @@ + + */ +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 IndexResponse|ResponseInterface + * @throws ClientExceptionInterface + * @throws Exception + */ + public function index(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, IndexResponse::class); + } + + /** + * @param string $domain + * @param string $templateId + * @param array $requestHeaders + * @return mixed|ResponseInterface + * @throws ClientExceptionInterface + * @throws Exception + */ + public function show(string $domain, string $templateId, array $requestHeaders = []) + { + Assert::notEmpty($domain); + Assert::notEmpty($templateId); + + $response = $this->httpGet(sprintf('/v3/%s/templates/%s', $domain, $templateId), [], $requestHeaders); + + return $this->hydrateResponse($response, ShowResponse::class); + } + + /** + * @param string $domain + * @param string $name + * @param string|null $template + * @param array|null $headers + * @param string|null $tag + * @param string|null $comment + * @param string|null $createdBy + * @param string|null $description + * @param string|null $engine + * @param array $requestHeaders + * @return CreateResponse|ResponseInterface + * @throws ClientExceptionInterface + * @throws Exception + */ + public function create( + string $domain, + string $name, + ?string $template = null, + ?array $headers = null, + ?string $tag = null, + ?string $comment = null, + ?string $createdBy = null, + ?string $description = null, + ?string $engine = null, + array $requestHeaders = [] + ) { + Assert::stringNotEmpty($domain); + Assert::stringNotEmpty($name); + + $body = [ + 'name' => $name, + ]; + + if (!empty($template)) { + $body['template'] = $template; + } + if (!empty($tag)) { + $body['tag'] = $tag; + } + if (!empty($comment)) { + $body['comment'] = $comment; + } + if (!empty($createdBy)) { + $body['createdBy'] = $createdBy; + } + if (!empty($headers)) { + $body['headers'] = json_encode($headers); + } + if (!empty($description)) { + $body['description'] = $description; + } + if (!empty($engine)) { + $body['engine'] = $engine; + } + + $response = $this->httpPost(sprintf('/v3/%s/templates', $domain), $body, $requestHeaders); + + return $this->hydrateResponse($response, CreateResponse::class); + } + + /** + * @param string $domain + * @param string $templateName + * @param array $requestHeaders + * @return mixed|ResponseInterface + * @throws ClientExceptionInterface + */ + public function deleteTemplate(string $domain, string $templateName, array $requestHeaders = []) + { + $response = $this->httpDelete(sprintf('/v3/%s/templates/%s', $domain, $templateName), [], $requestHeaders); + + return $this->hydrateResponse($response, ShowResponse::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/CreateResponse.php b/src/Model/Templates/CreateResponse.php new file mode 100644 index 00000000..d6221898 --- /dev/null +++ b/src/Model/Templates/CreateResponse.php @@ -0,0 +1,59 @@ +message = $data['message'] ?? null; + if (isset($data['template'])) { + $model->template = Template::create($data['template']); + } + + return $model; + } + + private function __construct() + { + } + + /** + * @return string|null + */ + public function getMessage(): ?string + { + return $this->message; + } + + /** + * @return Template|null + */ + public function getTemplate(): ?Template + { + return $this->template; + } +} diff --git a/src/Model/Templates/DeleteResponse.php b/src/Model/Templates/DeleteResponse.php new file mode 100644 index 00000000..6b98a79c --- /dev/null +++ b/src/Model/Templates/DeleteResponse.php @@ -0,0 +1,73 @@ +setMessage($data['message']); + $model->setTemplate(Template::create($template)); + + return $model; + } + + /** + * @return string + */ + public function getMessage(): string + { + return $this->message; + } + + /** + * @param string $message + */ + public function setMessage(string $message): void + { + $this->message = $message; + } + + /** + * @return Template + */ + public function getTemplate(): Template + { + return $this->template; + } + + /** + * @param Template $template + */ + public function setTemplate(Template $template): void + { + $this->template = $template; + } +} diff --git a/src/Model/Templates/IndexResponse.php b/src/Model/Templates/IndexResponse.php new file mode 100644 index 00000000..fda0400a --- /dev/null +++ b/src/Model/Templates/IndexResponse.php @@ -0,0 +1,85 @@ +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/ShowResponse.php b/src/Model/Templates/ShowResponse.php new file mode 100644 index 00000000..41e4127d --- /dev/null +++ b/src/Model/Templates/ShowResponse.php @@ -0,0 +1,18 @@ +setId($template['id'] ?? null); + $model->setName($template['name']); + $model->setDescription($template['description'] ?? ''); + $model->setCreatedAt($template['createdAt'] ?? ''); + $model->setCreatedBy($template['createdBy'] ?? ''); + + return $model; + } + + /** + * @return string|null + */ + public function getId(): ?string + { + return $this->id; + } + + /** + * @param string|null $id + */ + public function setId(?string $id): void + { + $this->id = $id; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @param string $name + */ + public function setName(string $name): void + { + $this->name = $name; + } + + /** + * @return string + */ + public function getDescription(): string + { + return $this->description; + } + + /** + * @param string $description + */ + public function setDescription(string $description): void + { + $this->description = $description; + } + + /** + * @return string + */ + public function getCreatedAt(): string + { + return $this->createdAt; + } + + /** + * @param string $createdAt + */ + public function setCreatedAt(string $createdAt): void + { + $this->createdAt = $createdAt; + } + + /** + * @return string + */ + public function getCreatedBy(): string + { + return $this->createdBy; + } + + /** + * @param string $createdBy + */ + public function setCreatedBy(string $createdBy): void + { + $this->createdBy = $createdBy; + } +}