From 56655ad6c043b31d8e74b5b7863038703d14b7ef Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sat, 5 Jan 2019 20:31:38 +0100 Subject: [PATCH] Added IP API class, models and test (#515) * Added IP API class, models and test * cs * bugfix --- src/Mailgun/Api/Ip.php | 116 ++++++++++++++++++++++++ src/Mailgun/Model/Ip/IndexResponse.php | 57 ++++++++++++ src/Mailgun/Model/Ip/ShowResponse.php | 71 +++++++++++++++ src/Mailgun/Model/Ip/UpdateResponse.php | 43 +++++++++ tests/Model/Ip/IndexResponseTest.php | 32 +++++++ tests/Model/Ip/ShowResponseTest.php | 32 +++++++ tests/Model/Ip/UpdateResponseTest.php | 28 ++++++ 7 files changed, 379 insertions(+) create mode 100644 src/Mailgun/Api/Ip.php create mode 100644 src/Mailgun/Model/Ip/IndexResponse.php create mode 100644 src/Mailgun/Model/Ip/ShowResponse.php create mode 100644 src/Mailgun/Model/Ip/UpdateResponse.php create mode 100644 tests/Model/Ip/IndexResponseTest.php create mode 100644 tests/Model/Ip/ShowResponseTest.php create mode 100644 tests/Model/Ip/UpdateResponseTest.php diff --git a/src/Mailgun/Api/Ip.php b/src/Mailgun/Api/Ip.php new file mode 100644 index 0000000..c69f5ca --- /dev/null +++ b/src/Mailgun/Api/Ip.php @@ -0,0 +1,116 @@ + + */ +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); + } +} diff --git a/src/Mailgun/Model/Ip/IndexResponse.php b/src/Mailgun/Model/Ip/IndexResponse.php new file mode 100644 index 0000000..4944784 --- /dev/null +++ b/src/Mailgun/Model/Ip/IndexResponse.php @@ -0,0 +1,57 @@ + + */ +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; + } +} diff --git a/src/Mailgun/Model/Ip/ShowResponse.php b/src/Mailgun/Model/Ip/ShowResponse.php new file mode 100644 index 0000000..86180fe --- /dev/null +++ b/src/Mailgun/Model/Ip/ShowResponse.php @@ -0,0 +1,71 @@ + + */ +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; + } +} diff --git a/src/Mailgun/Model/Ip/UpdateResponse.php b/src/Mailgun/Model/Ip/UpdateResponse.php new file mode 100644 index 0000000..cfa74ef --- /dev/null +++ b/src/Mailgun/Model/Ip/UpdateResponse.php @@ -0,0 +1,43 @@ + + */ +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; + } +} diff --git a/tests/Model/Ip/IndexResponseTest.php b/tests/Model/Ip/IndexResponseTest.php new file mode 100644 index 0000000..6c1bf2c --- /dev/null +++ b/tests/Model/Ip/IndexResponseTest.php @@ -0,0 +1,32 @@ +assertEquals(2, $model->getTotalCount()); + $items = $model->getItems(); + $this->assertCount(2, $items); + $this->assertEquals('192.161.0.1', $items[0]); + } +} diff --git a/tests/Model/Ip/ShowResponseTest.php b/tests/Model/Ip/ShowResponseTest.php new file mode 100644 index 0000000..0708100 --- /dev/null +++ b/tests/Model/Ip/ShowResponseTest.php @@ -0,0 +1,32 @@ +assertEquals('192.161.0.1', $model->getIp()); + $this->assertTrue($model->getDedicated()); + $this->assertEquals('luna.mailgun.net', $model->getRdns()); + } +} diff --git a/tests/Model/Ip/UpdateResponseTest.php b/tests/Model/Ip/UpdateResponseTest.php new file mode 100644 index 0000000..61db162 --- /dev/null +++ b/tests/Model/Ip/UpdateResponseTest.php @@ -0,0 +1,28 @@ +assertEquals('success', $model->getMessage()); + } +}