Skip to content

Commit 7aa3a11

Browse files
authored
Merge pull request #78 from linuxjuggler/master
Implementing the User's Account Memberships
2 parents ae2c680 + 6f1ac60 commit 7aa3a11

File tree

8 files changed

+410
-1
lines changed

8 files changed

+410
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/.idea
12
/vendor/

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"require": {
66
"guzzlehttp/guzzle": "^6.2.2",
77
"php": ">=7.0.0",
8-
"psr/http-message": "~1.0"
8+
"psr/http-message": "~1.0",
9+
"ext-json": "*"
910
},
1011
"require-dev": {
1112
"phpunit/phpunit": "5.7.5",
@@ -28,5 +29,10 @@
2829
"classmap": [
2930
"tests/"
3031
]
32+
},
33+
"config": {
34+
"preferred-install": "dist",
35+
"sort-packages": true,
36+
"optimize-autoloader": true
3137
}
3238
}

src/Endpoints/Membership.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Cloudflare\API\Endpoints;
4+
5+
use Cloudflare\API\Adapter\Adapter;
6+
use Cloudflare\API\Traits\BodyAccessorTrait;
7+
8+
/* A list of memberships of accounts this user can access */
9+
10+
class Membership implements API
11+
{
12+
use BodyAccessorTrait;
13+
14+
private $adapter;
15+
16+
public function __construct(Adapter $adapter)
17+
{
18+
$this->adapter = $adapter;
19+
}
20+
21+
22+
public function listMemberships(
23+
string $name = '',
24+
string $status = '',
25+
int $page = 1,
26+
int $perPage = 20,
27+
string $order = '',
28+
string $direction = ''
29+
): \stdClass {
30+
$query = [
31+
'page' => $page,
32+
'per_page' => $perPage
33+
];
34+
35+
if (!empty($name)) {
36+
$query['account.name'] = $name;
37+
}
38+
39+
if (!empty($status) && in_array($status, ['accepted', 'pending', 'rejected'], true)) {
40+
$query['status'] = $status;
41+
}
42+
43+
if (!empty($order) && in_array($order, ['id', 'account.name', 'status'], true)) {
44+
$query['order'] = $order;
45+
}
46+
47+
if (!empty($direction) && in_array($direction, ['asc', 'desc'], true)) {
48+
$query['direction'] = $direction;
49+
}
50+
51+
$memberships = $this->adapter->get('memberships', $query);
52+
$this->body = json_decode($memberships->getBody());
53+
54+
return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info];
55+
}
56+
57+
public function getMembershipDetails(string $membershipId): \stdClass
58+
{
59+
$membership = $this->adapter->get(sprintf('memberships/%s', $membershipId));
60+
$this->body = json_decode($membership->getBody());
61+
return $this->body->result;
62+
}
63+
64+
public function updateMembershipStatus(string $membershipId, string $status): \stdClass
65+
{
66+
$response = $this->adapter->put(sprintf('memberships/%s', $membershipId), ['status' => $status]);
67+
$this->body = json_decode($response->getBody());
68+
return $this->body;
69+
}
70+
71+
public function deleteMembership(string $membershipId): bool
72+
{
73+
$response = $this->adapter->delete(sprintf('memberships/%s', $membershipId));
74+
75+
$this->body = json_decode($response->getBody());
76+
77+
if (isset($this->body->result->id)) {
78+
return true;
79+
}
80+
81+
return false;
82+
}
83+
}

tests/Endpoints/MembershipTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
4+
class MembershipTest extends TestCase
5+
{
6+
public function testListMemberships()
7+
{
8+
$response = $this->getPsr7JsonResponseForFixture('Endpoints/listMemberships.json');
9+
10+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
11+
$mock->method('get')->willReturn($response);
12+
13+
$mock->expects($this->once())
14+
->method('get')
15+
->with(
16+
$this->equalTo('memberships'),
17+
$this->equalTo([
18+
'page' => 1,
19+
'per_page' => 20,
20+
'account.name' => 'Demo Account',
21+
'status' => 'accepted',
22+
'order' => 'status',
23+
'direction' => 'desc',
24+
])
25+
);
26+
27+
$zones = new \Cloudflare\API\Endpoints\Membership($mock);
28+
$result = $zones->listMemberships('Demo Account', 'accepted', 1, 20, 'status', 'desc');
29+
30+
$this->assertObjectHasAttribute('result', $result);
31+
$this->assertObjectHasAttribute('result_info', $result);
32+
33+
$this->assertEquals('4536bcfad5faccb111b47003c79917fa', $result->result[0]->id);
34+
$this->assertEquals(1, $result->result_info->page);
35+
$this->assertEquals('4536bcfad5faccb111b47003c79917fa', $zones->getBody()->result[0]->id);
36+
}
37+
38+
public function testGetMembershipDetails()
39+
{
40+
$response = $this->getPsr7JsonResponseForFixture('Endpoints/getMembershipDetails.json');
41+
42+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
43+
$mock->method('get')->willReturn($response);
44+
45+
$membership = new \Cloudflare\API\Endpoints\Membership($mock);
46+
$details = $membership->getMembershipDetails('4536bcfad5faccb111b47003c79917fa');
47+
48+
$this->assertObjectHasAttribute('id', $details);
49+
$this->assertEquals('4536bcfad5faccb111b47003c79917fa', $details->id);
50+
$this->assertObjectHasAttribute('code', $details);
51+
$this->assertEquals('05dd05cce12bbed97c0d87cd78e89bc2fd41a6cee72f27f6fc84af2e45c0fac0', $details->code);
52+
$this->assertEquals('4536bcfad5faccb111b47003c79917fa', $membership->getBody()->result->id);
53+
}
54+
55+
public function testUpdateMembershipDetails()
56+
{
57+
$response = $this->getPsr7JsonResponseForFixture('Endpoints/updateMembershipStatus.json');
58+
59+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
60+
$mock->method('put')->willReturn($response);
61+
62+
$mock->expects($this->once())
63+
->method('put')
64+
->with(
65+
$this->equalTo('memberships/4536bcfad5faccb111b47003c79917fa'),
66+
$this->equalTo([
67+
'status' => 'accepted'
68+
])
69+
);
70+
71+
$membership = new \Cloudflare\API\Endpoints\Membership($mock);
72+
$membership->updateMembershipStatus('4536bcfad5faccb111b47003c79917fa', 'accepted');
73+
$this->assertEquals('4536bcfad5faccb111b47003c79917fa', $membership->getBody()->result->id);
74+
}
75+
76+
public function testDeleteMembership()
77+
{
78+
$response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteMembership.json');
79+
80+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
81+
$mock->method('delete')->willReturn($response);
82+
83+
$mock->expects($this->once())
84+
->method('delete')
85+
->with($this->equalTo('memberships/4536bcfad5faccb111b47003c79917fa'));
86+
87+
$membership = new \Cloudflare\API\Endpoints\Membership($mock);
88+
89+
$membership->deleteMembership('4536bcfad5faccb111b47003c79917fa');
90+
91+
$this->assertEquals('4536bcfad5faccb111b47003c79917fa', $membership->getBody()->result->id);
92+
}
93+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"success": true,
3+
"errors": [],
4+
"messages": [],
5+
"result": {
6+
"id": "4536bcfad5faccb111b47003c79917fa"
7+
}
8+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"success": true,
3+
"errors": [],
4+
"messages": [],
5+
"result": {
6+
"id": "4536bcfad5faccb111b47003c79917fa",
7+
"code": "05dd05cce12bbed97c0d87cd78e89bc2fd41a6cee72f27f6fc84af2e45c0fac0",
8+
"status": "accepted",
9+
"account": {
10+
"id": "01a7362d577a6c3019a474fd6f485823",
11+
"name": "Demo Account",
12+
"settings": {
13+
"enforce_twofactor": false
14+
}
15+
},
16+
"roles": [
17+
"Account Administrator"
18+
],
19+
"permissions": {
20+
"analytics": {
21+
"read": true,
22+
"write": true
23+
},
24+
"billing": {
25+
"read": true,
26+
"write": true
27+
},
28+
"cache_purge": {
29+
"read": true,
30+
"write": true
31+
},
32+
"dns": {
33+
"read": true,
34+
"write": true
35+
},
36+
"dns_records": {
37+
"read": true,
38+
"write": true
39+
},
40+
"lb": {
41+
"read": true,
42+
"write": true
43+
},
44+
"logs": {
45+
"read": true,
46+
"write": true
47+
},
48+
"organization": {
49+
"read": true,
50+
"write": true
51+
},
52+
"ssl": {
53+
"read": true,
54+
"write": true
55+
},
56+
"waf": {
57+
"read": true,
58+
"write": true
59+
},
60+
"zones": {
61+
"read": true,
62+
"write": true
63+
},
64+
"zone_settings": {
65+
"read": true,
66+
"write": true
67+
}
68+
}
69+
}
70+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"success": true,
3+
"errors": [],
4+
"messages": [],
5+
"result": [
6+
{
7+
"id": "4536bcfad5faccb111b47003c79917fa",
8+
"code": "05dd05cce12bbed97c0d87cd78e89bc2fd41a6cee72f27f6fc84af2e45c0fac0",
9+
"status": "accepted",
10+
"account": {
11+
"id": "01a7362d577a6c3019a474fd6f485823",
12+
"name": "Demo Account",
13+
"settings": {
14+
"enforce_twofactor": false
15+
}
16+
},
17+
"roles": [
18+
"Account Administrator"
19+
],
20+
"permissions": {
21+
"analytics": {
22+
"read": true,
23+
"write": true
24+
},
25+
"billing": {
26+
"read": true,
27+
"write": true
28+
},
29+
"cache_purge": {
30+
"read": true,
31+
"write": true
32+
},
33+
"dns": {
34+
"read": true,
35+
"write": true
36+
},
37+
"dns_records": {
38+
"read": true,
39+
"write": true
40+
},
41+
"lb": {
42+
"read": true,
43+
"write": true
44+
},
45+
"logs": {
46+
"read": true,
47+
"write": true
48+
},
49+
"organization": {
50+
"read": true,
51+
"write": true
52+
},
53+
"ssl": {
54+
"read": true,
55+
"write": true
56+
},
57+
"waf": {
58+
"read": true,
59+
"write": true
60+
},
61+
"zones": {
62+
"read": true,
63+
"write": true
64+
},
65+
"zone_settings": {
66+
"read": true,
67+
"write": true
68+
}
69+
}
70+
}
71+
],
72+
"result_info": {
73+
"page": 1,
74+
"per_page": 20,
75+
"count": 1,
76+
"total_count": 2000
77+
}
78+
}

0 commit comments

Comments
 (0)