Added Mailbox api usign httpPost etc

This commit is contained in:
Pawel Wasiluk 2021-08-19 13:51:31 +02:00 committed by David Garcia
parent 6718ccee89
commit 2bec29071e
7 changed files with 364 additions and 0 deletions

100
src/Api/Mailboxes.php Normal file
View File

@ -0,0 +1,100 @@
<?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\Api;
use Mailgun\Assert;
use Mailgun\Model\Mailboxes\CreateResponse;
use Mailgun\Model\Mailboxes\DeleteResponse;
use Mailgun\Model\Mailboxes\ShowResponse;
use Mailgun\Model\Mailboxes\UpdateResponse;
class Mailboxes extends HttpApi
{
const MIN_PASSWORD_LENGTH = 5;
/**
* @param string $domain
* @param array $parameters
*
* @return CreateResponse
*
* @throws \Exception
*/
public function create(string $domain, array $parameters)
{
Assert::stringNotEmpty($domain);
Assert::isArray($parameters);
Assert::keyExists($parameters ,'mailbox');
Assert::keyExists($parameters ,'password');
Assert::minLength($parameters['password'], self::MIN_PASSWORD_LENGTH);
$response = $this->httpPost(sprintf('/v3/%s/mailboxes', $domain), $parameters);
return $this->hydrateResponse($response, CreateResponse::class);
}
/**
* @param string $domain
* @param array $parameters
*
* @return ShowResponse
*
* @throws \Exception
*/
public function show(string $domain, array $parameters = [])
{
Assert::stringNotEmpty($domain);
Assert::isArray($parameters);
$response = $this->httpGet(sprintf('/v3/%s/mailboxes', $domain), $parameters);
return $this->hydrateResponse($response, ShowResponse::class);
}
/**
* @param string $domain
* @param string $mailbox
* @param array $parameters
*
* @return UpdateResponse
*
* @throws \Exception
*/
public function update(string $domain, string $mailbox, array $parameters = [])
{
Assert::stringNotEmpty($domain);
Assert::stringNotEmpty($mailbox);
Assert::isArray($parameters);
$response = $this->httpPut(sprintf('/v3/%s/mailboxes/%s', $domain, $mailbox), $parameters);
return $this->hydrateResponse($response, UpdateResponse::class);
}
/**
* @param string $address
* @param string $mailbox
*
* @return DeleteResponse
*
* @throws \Exception
*/
public function delete(string $domain, string $mailbox)
{
Assert::stringNotEmpty($domain);
Assert::stringNotEmpty($mailbox);
$response = $this->httpDelete(sprintf('/v3/%s/mailboxes/%s', $domain, $mailbox));
return $this->hydrateResponse($response, DeleteResponse::class);
}
}

View File

@ -143,4 +143,9 @@ class Mailgun
{
return new Api\Webhook($this->httpClient, $this->requestBuilder, $this->hydrator, $this->apiKey);
}
public function mailboxes(): Api\Mailboxes
{
return new Api\Mailboxes($this->httpClient, $this->requestBuilder, $this->hydrator, $this->apiKey);
}
}

View File

@ -0,0 +1,36 @@
<?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\Mailboxes;
use Mailgun\Model\ApiResponse;
final class CreateResponse implements ApiResponse
{
private $message;
public static function create(array $data): self
{
$model = new self();
$model->message = $data['message'] ?? null;
return $model;
}
private function __construct()
{
}
public function getMessage(): ?string
{
return $this->message;
}
}

View File

@ -0,0 +1,43 @@
<?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\Mailboxes;
use Mailgun\Model\ApiResponse;
final class DeleteResponse implements ApiResponse
{
private $message;
private $spec;
public static function create(array $data): self
{
$model = new self();
$model->message = $data['message'] ?? null;
$model->spec = $data['spec'] ?? null;
return $model;
}
private function __construct()
{
}
public function getMessage(): ?string
{
return $this->message;
}
public function getSpec(): ?string
{
return $this->spec;
}
}

View File

@ -0,0 +1,43 @@
<?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\Mailboxes;
use Mailgun\Model\ApiResponse;
final class ShowResponse implements ApiResponse
{
private $totalCount;
private $items;
public static function create(array $data): self
{
$model = new self();
$model->totalCount = $data['total_count'] ?? null;
$model->items = $data['items'] ?? null;
return $model;
}
private function __construct()
{
}
public function getTotalCount(): ?string
{
return $this->totalCount;
}
public function getItems(): ?string
{
return $this->items;
}
}

View File

@ -0,0 +1,36 @@
<?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\Mailboxes;
use Mailgun\Model\ApiResponse;
final class UpdateResponse implements ApiResponse
{
private $message;
public static function create(array $data): self
{
$model = new self();
$model->message = $data['message'] ?? null;
return $model;
}
private function __construct()
{
}
public function getMessage(): ?string
{
return $this->message;
}
}

101
tests/Api/MailboxesTest.php Normal file
View File

@ -0,0 +1,101 @@
<?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\Tests\Api;
use GuzzleHttp\Psr7\Response;
use Mailgun\Api\Mailboxes;
use Mailgun\Exception\InvalidArgumentException;
class MailboxesTest extends TestCase
{
public function testCreate()
{
$parameters = [
'mailbox' => 'mailbox',
'password' => 'password123',
];
$domain = 'testing@domain.com';
$api = $this->getApiMock();
$api->expects($this->once())
->method('httpPost')
->with(sprintf('/v3/%s/mailboxes', $domain), $parameters)
->willReturn(new Response());
$api->create($domain, $parameters);
}
public function testCreateInvalidPassword()
{
$this->expectException(InvalidArgumentException::class);
$parameters = [
'mailbox' => 'mailbox',
'password' => 'pass',
];
$domain = 'testing@domain.com';
$api = $this->getApiMock();
$api->create($domain, $parameters);
}
public function testShow()
{
$parameters = [
'limit' => '2',
'skip' => '4',
];
$domain = 'testing@domain.com';
$api = $this->getApiMock();
$api->expects($this->once())
->method('httpGet')
->with(sprintf('/v3/%s/mailboxes', $domain), $parameters)
->willReturn(new Response());
$api->show($domain, $parameters);
}
public function testUpdate()
{
$parameters = [
'password' => 'password123',
];
$mailbox = 'mailboxname';
$domain = 'testing@domain.com';
$api = $this->getApiMock();
$api->expects($this->once())
->method('httpPut')
->with(sprintf('/v3/%s/mailboxes/%s', $domain, $mailbox), $parameters)
->willReturn(new Response());
$api->update($domain, $mailbox, $parameters);
}
public function testDelete()
{
$domain = 'testing@domain.com';
$mailbox = 'mailboxname';
$api = $this->getApiMock();
$api->expects($this->once())
->method('httpDelete')
->with(sprintf('/v3/%s/mailboxes/%s', $domain, $mailbox))
->willReturn(new Response());
$api->delete($domain, $mailbox);
}
/**
* {@inheritdoc}
*/
protected function getApiClass()
{
return Mailboxes::class;
}
}