mirror of
https://github.com/retailcrm/mailgun-php.git
synced 2024-11-29 00:25:37 +03:00
Added IP API class, models and test (#515)
* Added IP API class, models and test * cs * bugfix
This commit is contained in:
parent
c0a386027b
commit
56655ad6c0
116
src/Mailgun/Api/Ip.php
Normal file
116
src/Mailgun/Api/Ip.php
Normal 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);
|
||||
}
|
||||
}
|
57
src/Mailgun/Model/Ip/IndexResponse.php
Normal file
57
src/Mailgun/Model/Ip/IndexResponse.php
Normal 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;
|
||||
}
|
||||
}
|
71
src/Mailgun/Model/Ip/ShowResponse.php
Normal file
71
src/Mailgun/Model/Ip/ShowResponse.php
Normal 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;
|
||||
}
|
||||
}
|
43
src/Mailgun/Model/Ip/UpdateResponse.php
Normal file
43
src/Mailgun/Model/Ip/UpdateResponse.php
Normal 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;
|
||||
}
|
||||
}
|
32
tests/Model/Ip/IndexResponseTest.php
Normal file
32
tests/Model/Ip/IndexResponseTest.php
Normal 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]);
|
||||
}
|
||||
}
|
32
tests/Model/Ip/ShowResponseTest.php
Normal file
32
tests/Model/Ip/ShowResponseTest.php
Normal 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());
|
||||
}
|
||||
}
|
28
tests/Model/Ip/UpdateResponseTest.php
Normal file
28
tests/Model/Ip/UpdateResponseTest.php
Normal 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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user