Skip to content

Commit

Permalink
Template API. In Progress
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandr-mykhailenko committed Feb 25, 2024
1 parent 85e598f commit 83adc1b
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 0 deletions.
101 changes: 101 additions & 0 deletions src/Api/Templates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

Check warning on line 1 in src/Api/Templates.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/Api/Templates.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: trailing_comma_in_multiline

Check warning on line 1 in src/Api/Templates.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: no_unused_imports

Check warning on line 1 in src/Api/Templates.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: no_extra_blank_lines

Check warning on line 1 in src/Api/Templates.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\Api;

use Exception;
use Mailgun\Assert;
use Mailgun\Model\Domain\CreateResponse;
use Mailgun\Model\Domain\IndexResponse;
use Mailgun\Model\Templates\GetResponse;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\ResponseInterface;

/**
* @see https://documentation.mailgun.com/docs/mailgun/api-reference/openapi-final/tag/Templates/#tag/Templates/operation/httpapi.(*TemplateAPIControler).GetPage-fm-9
*
* @author Sean Johnson <sean@mailgun.com>
*/
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;

Check failure on line 93 in src/Api/Templates.php

View workflow job for this annotation

GitHub Actions / Psalm

Possibly undefined variable $params, first seen on line 93 (see https://psalm.dev/018)



$response = $this->httpPost('/v3/domains', $params, $requestHeaders);

return $this->hydrateResponse($response, CreateResponse::class);
}
}
13 changes: 13 additions & 0 deletions src/Mailgun.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
84 changes: 84 additions & 0 deletions src/Model/Templates/GetResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

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;
use Mailgun\Model\PaginationResponse;
use Mailgun\Model\PagingProvider;
use Psr\Http\Message\StreamInterface;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
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;
}
}
32 changes: 32 additions & 0 deletions src/Model/Templates/Template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

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

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: blank_line_before_statement

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;

class Template
{
private $tag;
private $description;
private $firstSeen;
private $lastSeen;

private function __construct()
{
}

public static function create(array $data): self
{
$model = new self();

print_r($data);
return $model;
}
}

0 comments on commit 83adc1b

Please sign in to comment.