/
Zones.php
276 lines (219 loc) · 6.89 KB
/
Zones.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* Created by PhpStorm.
* User: junade
* Date: 06/06/2017
* Time: 15:45
*/
namespace Cloudflare\API\Endpoints;
use Cloudflare\API\Adapter\Adapter;
use Cloudflare\API\Traits\BodyAccessorTrait;
class Zones implements API
{
use BodyAccessorTrait;
private $adapter;
public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
}
/**
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*
* @param string $name
* @param bool $jumpStart
* @param string $accountId
* @return \stdClass
*/
public function addZone(string $name, bool $jumpStart = false, string $accountId = ''): \stdClass
{
$options = [
'name' => $name,
'jump_start' => $jumpStart
];
if (!empty($accountId)) {
$options['account'] = [
'id' => $accountId,
];
}
$user = $this->adapter->post('zones', $options);
$this->body = json_decode($user->getBody());
return $this->body->result;
}
public function activationCheck(string $zoneID): bool
{
$user = $this->adapter->put('zones/' . $zoneID . '/activation_check');
$this->body = json_decode($user->getBody());
if (isset($this->body->result->id)) {
return true;
}
return false;
}
public function pause(string $zoneID): bool
{
$user = $this->adapter->patch('zones/' . $zoneID, ['paused' => true]);
$this->body = json_decode($user->getBody());
if (isset($this->body->result->id)) {
return true;
}
return false;
}
public function unpause(string $zoneID): bool
{
$user = $this->adapter->patch('zones/' . $zoneID, ['paused' => false]);
$this->body = json_decode($user->getBody());
if (isset($this->body->result->id)) {
return true;
}
return false;
}
public function getZoneById(
string $zoneId
): \stdClass {
$user = $this->adapter->get('zones/' . $zoneId);
$this->body = json_decode($user->getBody());
return (object)['result' => $this->body->result];
}
public function listZones(
string $name = '',
string $status = '',
int $page = 1,
int $perPage = 20,
string $order = '',
string $direction = '',
string $match = 'all'
): \stdClass {
$query = [
'page' => $page,
'per_page' => $perPage,
'match' => $match
];
if (!empty($name)) {
$query['name'] = $name;
}
if (!empty($status)) {
$query['status'] = $status;
}
if (!empty($order)) {
$query['order'] = $order;
}
if (!empty($direction)) {
$query['direction'] = $direction;
}
$user = $this->adapter->get('zones', $query);
$this->body = json_decode($user->getBody());
return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info];
}
public function getZoneID(string $name = ''): string
{
$zones = $this->listZones($name);
if (count($zones->result) < 1) {
throw new EndpointException('Could not find zones with specified name.');
}
return $zones->result[0]->id;
}
/**
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*
* @param string $zoneID
* @param string $since
* @param string $until
* @param bool $continuous
* @return \stdClass
*/
public function getAnalyticsDashboard(string $zoneID, string $since = '-10080', string $until = '0', bool $continuous = true): \stdClass
{
$response = $this->adapter->get('zones/' . $zoneID . '/analytics/dashboard', ['since' => $since, 'until' => $until, 'continuous' => var_export($continuous, true)]);
$this->body = $response->getBody();
return json_decode($this->body)->result;
}
/**
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*
* @param string $zoneID
* @param bool $enable
* @return bool
*/
public function changeDevelopmentMode(string $zoneID, bool $enable = false): bool
{
$response = $this->adapter->patch('zones/' . $zoneID . '/settings/development_mode', ['value' => $enable ? 'on' : 'off']);
$this->body = json_decode($response->getBody());
if ($this->body->success) {
return true;
}
return false;
}
/**
* Return caching level settings
* @param string $zoneID
* @return string
*/
public function getCachingLevel(string $zoneID): string
{
$response = $this->adapter->get('zones/' . $zoneID . '/settings/cache_level');
$this->body = json_decode($response->getBody());
return $this->body->result->value;
}
/**
* Change caching level settings
* @param string $zoneID
* @param string $level (aggressive | basic | simplified)
* @return bool
*/
public function setCachingLevel(string $zoneID, string $level = 'aggressive'): bool
{
$response = $this->adapter->patch('zones/' . $zoneID . '/settings/cache_level', ['value' => $level]);
$this->body = json_decode($response->getBody());
if ($this->body->success) {
return true;
}
return false;
}
/**
* Purge Everything
* @param string $zoneID
* @return bool
*/
public function cachePurgeEverything(string $zoneID): bool
{
$user = $this->adapter->post('zones/' . $zoneID . '/purge_cache', ['purge_everything' => true]);
$this->body = json_decode($user->getBody());
if (isset($this->body->result->id)) {
return true;
}
return false;
}
public function cachePurge(string $zoneID, array $files = null, array $tags = null, array $hosts = null): bool
{
if ($files === null && $tags === null && $hosts === null) {
throw new EndpointException('No files, tags or hosts to purge.');
}
$options = [];
if (!is_null($files)) {
$options['files'] = $files;
}
if (!is_null($tags)) {
$options['tags'] = $tags;
}
if (!is_null($hosts)) {
$options['hosts'] = $hosts;
}
$user = $this->adapter->post('zones/' . $zoneID . '/purge_cache', $options);
$this->body = json_decode($user->getBody());
if (isset($this->body->result->id)) {
return true;
}
return false;
}
/**
* Delete Zone
*/
public function deleteZone(string $identifier): bool
{
$user = $this->adapter->delete('zones/' . $identifier);
$this->body = json_decode($user->getBody());
if (isset($this->body->result->id)) {
return true;
}
return false;
}
}