From 7ab10aaeaa58bcea33cfa80722b8c537ad08de2a Mon Sep 17 00:00:00 2001 From: Junade Ali Date: Wed, 7 Jun 2017 17:01:50 +0100 Subject: [PATCH] Zones endpoints. --- .idea/workspace.xml | 159 +++++++++++++++-------- src/Endpoints/Zones.php | 107 +++++++++++++++- tests/Endpoints/ZonesTest.php | 233 ++++++++++++++++++++++++++++++++++ 3 files changed, 447 insertions(+), 52 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index c26731d..3458bb8 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,18 +2,9 @@ - - - - - - - - - - - + + @@ -33,7 +24,60 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -73,10 +117,10 @@ @@ -86,6 +130,9 @@ true DEFINITION_ORDER + + + @@ -273,7 +320,7 @@ - + @@ -387,7 +434,7 @@ - + 1484351548066 @@ -400,12 +447,13 @@ - + - + @@ -562,7 +610,6 @@ - @@ -686,14 +733,6 @@ - - - - - - - - @@ -734,14 +773,6 @@ - - - - - - - - @@ -750,34 +781,60 @@ - + - - - - - - - - - - + + - - + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Endpoints/Zones.php b/src/Endpoints/Zones.php index 24a2d8d..f6ab4e2 100644 --- a/src/Endpoints/Zones.php +++ b/src/Endpoints/Zones.php @@ -9,6 +9,7 @@ namespace Cloudflare\API\Endpoints; use Cloudflare\API\Adapter\Adapter; +use Symfony\Component\Config\Definition\Exception\Exception; class Zones implements API { @@ -22,7 +23,7 @@ class Zones implements API public function addZone(string $name, bool $jumpstart = false, string $organizationID = ''): \stdClass { $options = [ - 'name' => $name, + 'name' => $name, 'jumpstart' => $jumpstart ]; @@ -36,4 +37,108 @@ class Zones implements API $body = json_decode($user->getBody()); return $body->result; } + + public function activationCheck(string $zoneID): bool + { + $user = $this->adapter->put('zones/' . $zoneID . '/activation_check', [], []); + $body = json_decode($user->getBody()); + + if (isset($body->result->id)) { + return true; + } + + return false; + } + + public function listZones( + string $name = "", + string $status = "", + int $page = 1, + int $perPage = 20, + string $order = "", + string $direction = "", + string $match = "all" + ): \stdClass { + $options = [ + 'page' => $page, + 'per_page' => $perPage, + 'match' => $match + ]; + + if (!empty($name)) { + $options['name'] = $name; + } + + if (!empty($status)) { + $options['status'] = $status; + } + + if (!empty($order)) { + $options['order'] = $order; + } + + if (!empty($direction)) { + $options['direction'] = $direction; + } + + $user = $this->adapter->get('zones', [], $options); + $body = json_decode($user->getBody()); + + $result = new \stdClass(); + $result->result = $body->result; + $result->result_info = $body->result_info; + + return $result; + } + + public function getZoneID(string $name = ""): string + { + $zones = $this->listZones($name); + + if (sizeof($zones) < 1) { + throw new EndpointException("Could not find zones with specified name."); + } + + return $zones->result[0]->id; + } + + /** + * Purge Everything + * @param string $zoneID + * @return bool + */ + public function purgeAll(string $zoneID): bool + { + $user = $this->adapter->delete('zones/' . $zoneID . '/purge_cache', [], ["purge_everything" => true]); + + $body = json_decode($user->getBody()); + + if (isset($body->result->id)) { + return true; + } + + return false; + } + + public function purge(string $zoneID, array $files = [], array $tags = []): bool + { + if (empty($files) && empty($tags)) { + throw new Exception("No files or tags to purge."); + } + + $options = [ + 'files' => $files, + 'tags' => $tags + ]; + + $user = $this->adapter->delete('zones/' . $zoneID . '/purge_cache', [], $options); + + $body = json_decode($user->getBody()); + + if (isset($body->result->id)) { + return true; + } + + return false; + } } \ No newline at end of file diff --git a/tests/Endpoints/ZonesTest.php b/tests/Endpoints/ZonesTest.php index cba72e1..71d2f8f 100644 --- a/tests/Endpoints/ZonesTest.php +++ b/tests/Endpoints/ZonesTest.php @@ -100,4 +100,237 @@ class ZonesTest extends PHPUnit_Framework_TestCase $zones = new \Cloudflare\API\Endpoints\Zones($mock); $zones->addZone("example.com", true, "01a7362d577a6c3019a474fd6f485823"); } + + public function testActivationTest() + { + $stream = GuzzleHttp\Psr7\stream_for('{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "023e105f4ecef8ad9ca31a8372d0c353" + } +}'); + $response = new GuzzleHttp\Psr7\Response(200, ['Content-Type' => 'application/json'], $stream); + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('put')->willReturn($response); + + $mock->expects($this->once()) + ->method('put') + ->with($this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/activation_check'), + $this->equalTo([]), + $this->equalTo([]) + ); + + $zones = new \Cloudflare\API\Endpoints\Zones($mock); + $result = $zones->activationCheck("c2547eb745079dac9320b638f5e225cf483cc5cfdda41"); + + $this->assertTrue($result); + } + + public function testListZones() + { + $stream = GuzzleHttp\Psr7\stream_for('{ + "success": true, + "errors": [], + "messages": [], + "result": [ + { + "id": "023e105f4ecef8ad9ca31a8372d0c353", + "name": "example.com", + "development_mode": 7200, + "original_name_servers": [ + "ns1.originaldnshost.com", + "ns2.originaldnshost.com" + ], + "original_registrar": "GoDaddy", + "original_dnshost": "NameCheap", + "created_on": "2014-01-01T05:20:00.12345Z", + "modified_on": "2014-01-01T05:20:00.12345Z", + "name_servers": [ + "tony.ns.cloudflare.com", + "woz.ns.cloudflare.com" + ], + "owner": { + "id": "7c5dae5552338874e5053f2534d2767a", + "email": "user@example.com", + "owner_type": "user" + }, + "permissions": [ + "#zone:read", + "#zone:edit" + ], + "plan": { + "id": "e592fd9519420ba7405e1307bff33214", + "name": "Pro Plan", + "price": 20, + "currency": "USD", + "frequency": "monthly", + "legacy_id": "pro", + "is_subscribed": true, + "can_subscribe": true + }, + "plan_pending": { + "id": "e592fd9519420ba7405e1307bff33214", + "name": "Pro Plan", + "price": 20, + "currency": "USD", + "frequency": "monthly", + "legacy_id": "pro", + "is_subscribed": true, + "can_subscribe": true + }, + "status": "active", + "paused": false, + "type": "full" + } + ], + "result_info": { + "page": 1, + "per_page": 20, + "count": 1, + "total_count": 2000 + } +}'); + $response = new GuzzleHttp\Psr7\Response(200, ['Content-Type' => 'application/json'], $stream); + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with($this->equalTo('zones'), + $this->equalTo([]), + $this->equalTo([ + 'page' => 1, + 'per_page' => 20, + 'match' => 'all', + 'name' => 'example.com', + 'status' => 'active', + 'order' => 'status', + 'direction' => 'desc' + ] + )); + + $zones = new \Cloudflare\API\Endpoints\Zones($mock); + $result = $zones->listZones("example.com", "active", 1, 20, "status", "desc", "all"); + + $this->assertObjectHasAttribute('result', $result); + $this->assertObjectHasAttribute('result_info', $result); + + $this->assertEquals("023e105f4ecef8ad9ca31a8372d0c353", $result->result[0]->id); + $this->assertEquals(1, $result->result_info->page); + } + + public function testGetZoneID() + { + $stream = GuzzleHttp\Psr7\stream_for('{ + "success": true, + "errors": [], + "messages": [], + "result": [ + { + "id": "023e105f4ecef8ad9ca31a8372d0c353", + "name": "example.com", + "development_mode": 7200, + "original_name_servers": [ + "ns1.originaldnshost.com", + "ns2.originaldnshost.com" + ], + "original_registrar": "GoDaddy", + "original_dnshost": "NameCheap", + "created_on": "2014-01-01T05:20:00.12345Z", + "modified_on": "2014-01-01T05:20:00.12345Z", + "name_servers": [ + "tony.ns.cloudflare.com", + "woz.ns.cloudflare.com" + ], + "owner": { + "id": "7c5dae5552338874e5053f2534d2767a", + "email": "user@example.com", + "owner_type": "user" + }, + "permissions": [ + "#zone:read", + "#zone:edit" + ], + "plan": { + "id": "e592fd9519420ba7405e1307bff33214", + "name": "Pro Plan", + "price": 20, + "currency": "USD", + "frequency": "monthly", + "legacy_id": "pro", + "is_subscribed": true, + "can_subscribe": true + }, + "plan_pending": { + "id": "e592fd9519420ba7405e1307bff33214", + "name": "Pro Plan", + "price": 20, + "currency": "USD", + "frequency": "monthly", + "legacy_id": "pro", + "is_subscribed": true, + "can_subscribe": true + }, + "status": "active", + "paused": false, + "type": "full" + } + ], + "result_info": { + "page": 1, + "per_page": 20, + "count": 1, + "total_count": 2000 + } +}'); + $response = new GuzzleHttp\Psr7\Response(200, ['Content-Type' => 'application/json'], $stream); + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with($this->equalTo('zones'), + $this->equalTo([]), + $this->equalTo([ + 'name' => 'example.com', + 'page' => 1, + 'per_page' => 20, + 'match' => 'all' + ] + )); + + $zones = new \Cloudflare\API\Endpoints\Zones($mock); + $result = $zones->getZoneID("example.com"); + + $this->assertEquals("023e105f4ecef8ad9ca31a8372d0c353", $result); + } + + public function testPurgeAll() + { + $stream = GuzzleHttp\Psr7\stream_for('{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "023e105f4ecef8ad9ca31a8372d0c353" + } +}'); + $response = new GuzzleHttp\Psr7\Response(200, ['Content-Type' => 'application/json'], $stream); + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('delete')->willReturn($response); + + $mock->expects($this->once()) + ->method('delete') + ->with($this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'), + $this->equalTo([]), + $this->equalTo(["purge_everything" => true]) + ); + + $zones = new \Cloudflare\API\Endpoints\Zones($mock); + $result = $zones->purgeAll("c2547eb745079dac9320b638f5e225cf483cc5cfdda41"); + + $this->assertTrue($result); + } }