Skip to content

Commit 099f383

Browse files
committed
Add trait to the rest of the endpoints
1 parent 2639331 commit 099f383

7 files changed

Lines changed: 61 additions & 29 deletions

File tree

src/Endpoints/User.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
namespace Cloudflare\API\Endpoints;
99

1010
use Cloudflare\API\Adapter\Adapter;
11+
use Cloudflare\API\Traits\BodyAccessorTrait;
1112

1213
class User implements API
1314
{
15+
use BodyAccessorTrait;
16+
1417
private $adapter;
1518

1619
public function __construct(Adapter $adapter)
@@ -21,8 +24,8 @@ public function __construct(Adapter $adapter)
2124
public function getUserDetails(): \stdClass
2225
{
2326
$user = $this->adapter->get('user');
24-
$body = json_decode($user->getBody());
25-
return $body->result;
27+
$this->body = json_decode($user->getBody());
28+
return $this->body->result;
2629
}
2730

2831
public function getUserID(): string
@@ -38,6 +41,7 @@ public function getUserEmail(): string
3841
public function updateUserDetails(array $details): \stdClass
3942
{
4043
$response = $this->adapter->patch('user', $details);
41-
return json_decode($response->getBody());
44+
$this->body = json_decode($response->getBody());
45+
return $this->body;
4246
}
4347
}

src/Endpoints/WAF.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
namespace Cloudflare\API\Endpoints;
1010

1111
use Cloudflare\API\Adapter\Adapter;
12+
use Cloudflare\API\Traits\BodyAccessorTrait;
1213

1314
class WAF implements API
1415
{
16+
use BodyAccessorTrait;
17+
1518
private $adapter;
1619

1720
public function __construct(Adapter $adapter)
@@ -42,9 +45,9 @@ public function getPackages(
4245
}
4346

4447
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/waf/packages', $query);
45-
$body = json_decode($user->getBody());
48+
$this->body = json_decode($user->getBody());
4649

47-
return (object)['result' => $body->result, 'result_info' => $body->result_info];
50+
return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info];
4851
}
4952

5053

@@ -53,9 +56,9 @@ public function getPackageInfo(
5356
string $packageID
5457
): \stdClass {
5558
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/waf/packages/' . $packageID);
56-
$body = json_decode($user->getBody());
59+
$this->body = json_decode($user->getBody());
5760

58-
return $body->result;
61+
return $this->body->result;
5962
}
6063

6164
public function getRules(
@@ -81,9 +84,9 @@ public function getRules(
8184
$query['direction'] = $direction;
8285
}
8386
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/rules', $query);
84-
$body = json_decode($user->getBody());
87+
$this->body = json_decode($user->getBody());
8588

86-
return (object)['result' => $body->result, 'result_info' => $body->result_info];
89+
return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info];
8790
}
8891

8992
public function getRuleInfo(
@@ -92,9 +95,9 @@ public function getRuleInfo(
9295
string $ruleID
9396
): \stdClass {
9497
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/rules/' . $ruleID);
95-
$body = json_decode($user->getBody());
98+
$this->body = json_decode($user->getBody());
9699

97-
return $body->result;
100+
return $this->body->result;
98101
}
99102

100103
public function updateRule(
@@ -111,9 +114,9 @@ public function updateRule(
111114
'zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/rules/' . $ruleID,
112115
$query
113116
);
114-
$body = json_decode($user->getBody());
117+
$this->body = json_decode($user->getBody());
115118

116-
return $body->result;
119+
return $this->body->result;
117120
}
118121

119122
public function getGroups(
@@ -143,9 +146,9 @@ public function getGroups(
143146
'zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/groups',
144147
$query
145148
);
146-
$body = json_decode($user->getBody());
149+
$this->body = json_decode($user->getBody());
147150

148-
return (object)['result' => $body->result, 'result_info' => $body->result_info];
151+
return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info];
149152
}
150153

151154
public function getGroupInfo(
@@ -154,9 +157,9 @@ public function getGroupInfo(
154157
string $groupID
155158
): \stdClass {
156159
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/groups/' . $groupID);
157-
$body = json_decode($user->getBody());
160+
$this->body = json_decode($user->getBody());
158161

159-
return $body->result;
162+
return $this->body->result;
160163
}
161164

162165
public function updateGroup(
@@ -173,8 +176,8 @@ public function updateGroup(
173176
'zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/groups/' . $groupID,
174177
$query
175178
);
176-
$body = json_decode($user->getBody());
179+
$this->body = json_decode($user->getBody());
177180

178-
return $body->result;
181+
return $this->body->result;
179182
}
180183
}

src/Endpoints/ZoneLockdown.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
namespace Cloudflare\API\Endpoints;
1010

1111
use Cloudflare\API\Adapter\Adapter;
12+
use Cloudflare\API\Traits\BodyAccessorTrait;
1213

1314
class ZoneLockdown implements API
1415
{
16+
use BodyAccessorTrait;
17+
1518
private $adapter;
1619

1720
public function __construct(Adapter $adapter)
@@ -30,9 +33,9 @@ public function listLockdowns(
3033
];
3134

3235
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/lockdowns', $query);
33-
$body = json_decode($user->getBody());
36+
$this->body = json_decode($user->getBody());
3437

35-
return (object)['result' => $body->result, 'result_info' => $body->result_info];
38+
return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info];
3639
}
3740

3841
public function createLockdown(
@@ -57,9 +60,9 @@ public function createLockdown(
5760

5861
$user = $this->adapter->post('zones/' . $zoneID . '/firewall/lockdowns', $options);
5962

60-
$body = json_decode($user->getBody());
63+
$this->body = json_decode($user->getBody());
6164

62-
if (isset($body->result->id)) {
65+
if (isset($this->body->result->id)) {
6366
return true;
6467
}
6568

@@ -69,8 +72,8 @@ public function createLockdown(
6972
public function getLockdownDetails(string $zoneID, string $lockdownID): \stdClass
7073
{
7174
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/lockdowns/' . $lockdownID);
72-
$body = json_decode($user->getBody());
73-
return $body->result;
75+
$this->body = json_decode($user->getBody());
76+
return $this->body->result;
7477
}
7578

7679
public function updateLockdown(
@@ -92,9 +95,9 @@ public function updateLockdown(
9295

9396
$user = $this->adapter->put('zones/' . $zoneID . '/firewall/lockdowns/' . $lockdownID, $options);
9497

95-
$body = json_decode($user->getBody());
98+
$this->body = json_decode($user->getBody());
9699

97-
if (isset($body->result->id)) {
100+
if (isset($this->body->result->id)) {
98101
return true;
99102
}
100103

@@ -105,9 +108,9 @@ public function deleteLockdown(string $zoneID, string $lockdownID): bool
105108
{
106109
$user = $this->adapter->delete('zones/' . $zoneID . '/firewall/lockdowns/' . $lockdownID);
107110

108-
$body = json_decode($user->getBody());
111+
$this->body = json_decode($user->getBody());
109112

110-
if (isset($body->result->id)) {
113+
if (isset($this->body->result->id)) {
111114
return true;
112115
}
113116

tests/Endpoints/UARulesTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function testListRules()
3333

3434
$this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $result->result[0]->id);
3535
$this->assertEquals(1, $result->result_info->page);
36+
$this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $zones->getBody()->result[0]->id);
3637
}
3738

3839
public function testCreateRule()
@@ -65,6 +66,7 @@ public function testCreateRule()
6566
'372e67954025e0ba6aaa6d586b9e0b59',
6667
'Prevent access from abusive clients identified by this UserAgent to mitigate DDoS attack'
6768
);
69+
$this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $rules->getBody()->result->id);
6870
}
6971

7072
public function getRuleDetails()
@@ -84,6 +86,7 @@ public function getRuleDetails()
8486
$result = $lockdown->getRuleDetails('023e105f4ecef8ad9ca31a8372d0c353', '372e67954025e0ba6aaa6d586b9e0b59');
8587

8688
$this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $result->id);
89+
$this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $rules->getBody()->result->id);
8790
}
8891

8992
public function testUpdateRule()
@@ -116,6 +119,7 @@ public function testUpdateRule()
116119
$config,
117120
'Restrict access to these endpoints to requests from a known IP address'
118121
);
122+
$this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $rules->getBody()->result->id);
119123
}
120124

121125
public function testDeleteRule()
@@ -133,5 +137,6 @@ public function testDeleteRule()
133137

134138
$rules = new \Cloudflare\API\Endpoints\UARules($mock);
135139
$rules->deleteRule('023e105f4ecef8ad9ca31a8372d0c353', '372e67954025e0ba6aaa6d586b9e0b59');
140+
$this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $rules->getBody()->result->id);
136141
}
137142
}

tests/Endpoints/UserTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function testGetUserDetails()
2121
$this->assertEquals('7c5dae5552338874e5053f2534d2767a', $details->id);
2222
$this->assertObjectHasAttribute('email', $details);
2323
$this->assertEquals('user@example.com', $details->email);
24+
$this->assertEquals('7c5dae5552338874e5053f2534d2767a', $user->getBody()->result->id);
2425
}
2526

2627
public function testGetUserID()
@@ -32,6 +33,7 @@ public function testGetUserID()
3233

3334
$user = new \Cloudflare\API\Endpoints\User($mock);
3435
$this->assertEquals('7c5dae5552338874e5053f2534d2767a', $user->getUserID());
36+
$this->assertEquals('7c5dae5552338874e5053f2534d2767a', $user->getBody()->result->id);
3537
}
3638

3739
public function testGetUserEmail()
@@ -45,6 +47,7 @@ public function testGetUserEmail()
4547

4648
$user = new \Cloudflare\API\Endpoints\User($mock);
4749
$this->assertEquals('user@example.com', $user->getUserEmail());
50+
$this->assertEquals('user@example.com', $user->getBody()->result->email);
4851
}
4952

5053
public function testUpdateUserDetails()
@@ -60,5 +63,6 @@ public function testUpdateUserDetails()
6063

6164
$user = new \Cloudflare\API\Endpoints\User($mock);
6265
$user->updateUserDetails(['email' => 'user2@example.com']);
66+
$this->assertEquals('7c5dae5552338874e5053f2534d2767a', $user->getBody()->result->id);
6367
}
6468
}

tests/Endpoints/WAFTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function testgetPackages()
3636

3737
$this->assertEquals('a25a9a7e9c00afc1fb2e0245519d725b', $result->result[0]->id);
3838
$this->assertEquals(1, $result->result_info->page);
39+
$this->assertEquals('a25a9a7e9c00afc1fb2e0245519d725b', $waf->getBody()->result[0]->id);
3940
}
4041

4142
public function testgetPackageInfo()
@@ -55,6 +56,7 @@ public function testgetPackageInfo()
5556
$result = $waf->getPackageInfo('023e105f4ecef8ad9ca31a8372d0c353', 'a25a9a7e9c00afc1fb2e0245519d725b');
5657

5758
$this->assertEquals('a25a9a7e9c00afc1fb2e0245519d725b', $result->id);
59+
$this->assertEquals('a25a9a7e9c00afc1fb2e0245519d725b', $waf->getBody()->result->id);
5860
}
5961

6062
public function testgetRules()
@@ -85,6 +87,7 @@ public function testgetRules()
8587

8688
$this->assertEquals('92f17202ed8bd63d69a66b86a49a8f6b', $result->result[0]->id);
8789
$this->assertEquals(1, $result->result_info->page);
90+
$this->assertEquals('92f17202ed8bd63d69a66b86a49a8f6b', $waf->getBody()->result[0]->id);
8891
}
8992

9093
public function testgetRuleInfo()
@@ -104,6 +107,7 @@ public function testgetRuleInfo()
104107
$result = $waf->getRuleInfo('023e105f4ecef8ad9ca31a8372d0c353', 'a25a9a7e9c00afc1fb2e0245519d725b', 'f939de3be84e66e757adcdcb87908023');
105108

106109
$this->assertEquals('f939de3be84e66e757adcdcb87908023', $result->id);
110+
$this->assertEquals('f939de3be84e66e757adcdcb87908023', $waf->getBody()->result->id);
107111
}
108112

109113
public function testupdateRule()
@@ -132,6 +136,7 @@ public function testupdateRule()
132136
foreach ($details as $property => $value) {
133137
$this->assertEquals($result->{ $property }, $value);
134138
}
139+
$this->assertEquals('f939de3be84e66e757adcdcb87908023', $waf->getBody()->result->id);
135140
}
136141

137142
public function getGroups()
@@ -162,6 +167,7 @@ public function getGroups()
162167

163168
$this->assertEquals('de677e5818985db1285d0e80225f06e5', $result->result[0]->id);
164169
$this->assertEquals(1, $result->result_info->page);
170+
$this->assertEquals('de677e5818985db1285d0e80225f06e5', $waf->getBody()->result[0]->id);
165171
}
166172

167173
public function testgetGroupInfo()
@@ -181,6 +187,7 @@ public function testgetGroupInfo()
181187
$result = $waf->getGroupInfo('023e105f4ecef8ad9ca31a8372d0c353', 'a25a9a7e9c00afc1fb2e0245519d725b', 'de677e5818985db1285d0e80225f06e5');
182188

183189
$this->assertEquals('de677e5818985db1285d0e80225f06e5', $result->id);
190+
$this->assertEquals('de677e5818985db1285d0e80225f06e5', $waf->getBody()->result->id);
184191
}
185192

186193
public function testupdateGroup()
@@ -209,5 +216,6 @@ public function testupdateGroup()
209216
foreach ($details as $property => $value) {
210217
$this->assertEquals($result->{ $property }, $value);
211218
}
219+
$this->assertEquals('de677e5818985db1285d0e80225f06e5', $waf->getBody()->result->id);
212220
}
213221
}

tests/Endpoints/ZoneLockdownTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function testListLockdowns()
3333

3434
$this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $result->result[0]->id);
3535
$this->assertEquals(1, $result->result_info->page);
36+
$this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $zones->getBody()->result[0]->id);
3637
}
3738

3839
public function testAddLockdown()
@@ -65,6 +66,7 @@ public function testAddLockdown()
6566
'372e67954025e0ba6aaa6d586b9e0b59',
6667
'Restrict access to these endpoints to requests from a known IP address'
6768
);
69+
$this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $zoneLockdown->getBody()->result->id);
6870
}
6971

7072
public function testGetRecordDetails()
@@ -84,6 +86,7 @@ public function testGetRecordDetails()
8486
$result = $lockdown->getLockdownDetails('023e105f4ecef8ad9ca31a8372d0c353', '372e67954025e0ba6aaa6d586b9e0b59');
8587

8688
$this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $result->id);
89+
$this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $lockdown->getBody()->result->id);
8790
}
8891

8992
public function testUpdateLockdown()
@@ -116,6 +119,7 @@ public function testUpdateLockdown()
116119
$config,
117120
'Restrict access to these endpoints to requests from a known IP address'
118121
);
122+
$this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $zoneLockdown->getBody()->result->id);
119123
}
120124

121125
public function testDeleteLockdown()
@@ -136,5 +140,6 @@ public function testDeleteLockdown()
136140

137141
$zoneLockdown = new \Cloudflare\API\Endpoints\ZoneLockdown($mock);
138142
$zoneLockdown->deleteLockdown('023e105f4ecef8ad9ca31a8372d0c353', '372e67954025e0ba6aaa6d586b9e0b59');
143+
$this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $zoneLockdown->getBody()->result->id);
139144
}
140145
}

0 commit comments

Comments
 (0)