Skip to content

Commit

Permalink
added create endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandr-mykhailenko committed Feb 29, 2024
1 parent 19d4c4f commit 0b6d918
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 19 deletions.
22 changes: 18 additions & 4 deletions src/Api/Templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Exception;
use Mailgun\Assert;
use Mailgun\Model\Templates\CreateResponse;
use Mailgun\Model\Templates\GetResponse;
use Mailgun\Model\Templates\IndexResponse;
use Mailgun\Model\Templates\ShowResponse;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -37,11 +37,11 @@ class Templates extends HttpApi
* @param string $page
* @param string $pivot
* @param array $requestHeaders
* @return GetResponse|ResponseInterface
* @return IndexResponse|ResponseInterface
* @throws ClientExceptionInterface
* @throws Exception
*/
public function get(string $domain, int $limit, string $page, string $pivot, array $requestHeaders = [])
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]);

Expand All @@ -53,7 +53,7 @@ public function get(string $domain, int $limit, string $page, string $pivot, arr

$response = $this->httpGet(sprintf('/v3/%s/templates', $domain), $params, $requestHeaders);

return $this->hydrateResponse($response, GetResponse::class);
return $this->hydrateResponse($response, IndexResponse::class);
}

/**
Expand Down Expand Up @@ -134,4 +134,18 @@ public function create(

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);
}
}
12 changes: 9 additions & 3 deletions src/Model/Templates/CreateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
final class CreateResponse implements ApiResponse
{
private $message;

/**
* @var Template $template
*/
private $template;

/**
Expand All @@ -26,7 +30,9 @@ public static function create(array $data): self
{
$model = new self();
$model->message = $data['message'] ?? null;
$model->template = $data['template'] ?? null;
if (isset($data['template'])) {
$model->template = Template::create($data['template']);
}

return $model;
}
Expand All @@ -44,9 +50,9 @@ public function getMessage(): ?string
}

/**
* @return array|null
* @return Template|null
*/
public function getTemplate(): ?array
public function getTemplate(): ?Template
{
return $this->template;
}
Expand Down
75 changes: 75 additions & 0 deletions src/Model/Templates/DeleteResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

Check warning on line 1 in src/Model/Templates/DeleteResponse.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: class_attributes_separation

Check warning on line 1 in src/Model/Templates/DeleteResponse.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: phpdoc_var_without_name

Check warning on line 1 in src/Model/Templates/DeleteResponse.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: single_line_after_imports

Check warning on line 1 in src/Model/Templates/DeleteResponse.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: phpdoc_align

declare(strict_types=1);

/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

namespace Mailgun\Model\Templates;

use Mailgun\Model\ApiResponse;


final class DeleteResponse implements ApiResponse
{
/**
* @var string $message
*/
private $message;

/**
* @var Template $template
*/
private $template;


/**
* @param array $data
* @return static
*/
public static function create(array $data): self
{
$template = $data['template'] ?? $data;
$model = new self();
$model->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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
use Mailgun\Model\PagingProvider;
use Psr\Http\Message\StreamInterface;

/**
* @author Tobias Nyholm <[email protected]>
*/
final class GetResponse implements ApiResponse, PagingProvider
final class IndexResponse implements ApiResponse, PagingProvider
{
use PaginationResponse;

Expand Down
3 changes: 0 additions & 3 deletions src/Model/Templates/ShowResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@

use Mailgun\Model\ApiResponse;

/**
* @author Tobias Nyholm <[email protected]>
*/
final class ShowResponse extends Template implements ApiResponse
{
}
10 changes: 5 additions & 5 deletions src/Model/Templates/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function create(array $data): self
$template = $data['template'] ?? $data;
$model = new self();

$model->setId($template['id']);
$model->setId($template['id'] ?? null);
$model->setName($template['name']);
$model->setDescription($template['description'] ?? '');
$model->setCreatedAt($template['createdAt'] ?? '');
Expand All @@ -50,17 +50,17 @@ public static function create(array $data): self
}

/**
* @return string
* @return string|null
*/
public function getId(): string
public function getId(): ?string
{
return $this->id;
}

/**
* @param string $id
* @param string|null $id
*/
public function setId(string $id): void
public function setId(?string $id): void
{
$this->id = $id;

Check failure on line 65 in src/Model/Templates/Template.php

View workflow job for this annotation

GitHub Actions / Psalm

$this->id with non-nullable declared type 'string' cannot be assigned nullable type 'null|string' (see https://psalm.dev/148)
}
Expand Down

0 comments on commit 0b6d918

Please sign in to comment.