Added IP API class, models and test (#515)

* Added IP API class, models and test

* cs

* bugfix
This commit is contained in:
Tobias Nyholm 2019-01-05 20:31:38 +01:00 committed by GitHub
parent c0a386027b
commit 56655ad6c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 379 additions and 0 deletions

116
src/Mailgun/Api/Ip.php Normal file
View File

@ -0,0 +1,116 @@
<?php
/*
* 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\Ip\IndexResponse;
use Mailgun\Model\Ip\ShowResponse;
use Mailgun\Model\Ip\UpdateResponse;
use Psr\Http\Message\ResponseInterface;
/**
* {@link https://documentation.mailgun.com/en/latest/api-ips.html#ips}.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class Ip extends HttpApi
{
/**
* Returns a list of IPs.
*
* @param bool $dedicated
*
* @return IndexResponse|ResponseInterface
*/
public function index($dedicated = false)
{
Assert::boolean($dedicated);
$params = [
'dedicated' => $dedicated,
];
$response = $this->httpGet('/v3/ips', $params);
return $this->hydrateResponse($response, IndexResponse::class);
}
/**
* Returns a list of IPs assigned to a domain.
*
* @param string $domain
*
* @return IndexResponse|ResponseInterface
*/
public function domainIndex($domain)
{
Assert::stringNotEmpty($domain);
$response = $this->httpGet(sprintf('/v3/domains/%s/ip', $domain));
return $this->hydrateResponse($response, IndexResponse::class);
}
/**
* Returns a single ip.
*
* @param string $ip
*
* @return ShowResponse|ResponseInterface
*/
public function show($ip)
{
Assert::ip($ip);
$response = $this->httpGet(sprintf('/v3/ips/%s', $ip));
return $this->hydrateResponse($response, ShowResponse::class);
}
/**
* Assign a dedicated IP to the domain specified.
*
* @param string $domain
* @param string $ip
*
* @return UpdateResponse|ResponseInterface
*/
public function assign($domain, $ip)
{
Assert::stringNotEmpty($domain);
Assert::ip($ip);
$params = [
'id' => $ip,
];
$response = $this->httpPost(sprintf('/v3/domains/%s/ips', $domain), $params);
return $this->hydrateResponse($response, UpdateResponse::class);
}
/**
* Unassign an IP from the domain specified.
*
* @param string $domain
* @param string $ip
*
* @return UpdateResponse|ResponseInterface
*/
public function unassign($domain, $ip)
{
Assert::stringNotEmpty($domain);
Assert::ip($ip);
$response = $this->httpDelete(sprintf('/v3/domains/%s/ips/%s', $domain, $ip));
return $this->hydrateResponse($response, UpdateResponse::class);
}
}

View File

@ -0,0 +1,57 @@
<?php
/*
* 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\Ip;
use Mailgun\Model\ApiResponse;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class IndexResponse implements ApiResponse
{
/**
* @var string[]
*/
private $items;
/**
* @var int
*/
private $totalCount = 0;
private function __construct()
{
}
public static function create(array $data)
{
$model = new self();
$model->items = $data['items'];
$model->totalCount = $data['total_count'];
return $model;
}
/**
* @return string[]
*/
public function getItems()
{
return $this->items;
}
/**
* @return int
*/
public function getTotalCount()
{
return $this->totalCount;
}
}

View File

@ -0,0 +1,71 @@
<?php
/*
* 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\Ip;
use Mailgun\Model\ApiResponse;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class ShowResponse implements ApiResponse
{
/**
* @var string
*/
private $ip;
/**
* @var bool
*/
private $dedicated;
/**
* @var string
*/
private $rdns;
private function __construct()
{
}
public static function create(array $data)
{
$model = new self();
$model->ip = $data['ip'];
$model->dedicated = (bool) $data['dedicated'];
$model->rdns = $data['rdns'];
return $model;
}
/**
* @return string
*/
public function getIp()
{
return $this->ip;
}
/**
* @return bool
*/
public function getDedicated()
{
return $this->dedicated;
}
/**
* @return string
*/
public function getRdns()
{
return $this->rdns;
}
}

View File

@ -0,0 +1,43 @@
<?php
/*
* 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\Ip;
use Mailgun\Model\ApiResponse;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class UpdateResponse implements ApiResponse
{
/**
* @var string
*/
private $message;
private function __construct()
{
}
public static function create(array $data)
{
$model = new self();
$model->message = $data['message'];
return $model;
}
/**
* @return string
*/
public function getMessage()
{
return $this->message;
}
}

View File

@ -0,0 +1,32 @@
<?php
/*
* 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\Model\Ip;
use Mailgun\Model\Ip\IndexResponse;
use Mailgun\Tests\Model\BaseModelTest;
class IndexResponseTest extends BaseModelTest
{
public function testCreate()
{
$json =
<<<'JSON'
{
"items": ["192.161.0.1", "192.168.0.2"],
"total_count": 2
}
JSON;
$model = IndexResponse::create(json_decode($json, true));
$this->assertEquals(2, $model->getTotalCount());
$items = $model->getItems();
$this->assertCount(2, $items);
$this->assertEquals('192.161.0.1', $items[0]);
}
}

View File

@ -0,0 +1,32 @@
<?php
/*
* 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\Model\Ip;
use Mailgun\Model\Ip\ShowResponse;
use Mailgun\Tests\Model\BaseModelTest;
class ShowResponseTest extends BaseModelTest
{
public function testCreate()
{
$json =
<<<'JSON'
{
"ip": "192.161.0.1",
"dedicated": true,
"rdns": "luna.mailgun.net"
}
JSON;
$model = ShowResponse::create(json_decode($json, true));
$this->assertEquals('192.161.0.1', $model->getIp());
$this->assertTrue($model->getDedicated());
$this->assertEquals('luna.mailgun.net', $model->getRdns());
}
}

View File

@ -0,0 +1,28 @@
<?php
/*
* 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\Model\Ip;
use Mailgun\Model\Ip\UpdateResponse;
use Mailgun\Tests\Model\BaseModelTest;
class UpdateResponseTest extends BaseModelTest
{
public function testCreate()
{
$json =
<<<'JSON'
{
"message": "success"
}
JSON;
$model = UpdateResponse::create(json_decode($json, true));
$this->assertEquals('success', $model->getMessage());
}
}