Skip to content

Commit 3453e44

Browse files
TylerVigarioIcyApril
authored andcommitted
Added changeDevelopmentMode & getAnalyticsDashboard to Zones (#32)
* Added toggleDevelopmentMode to Zones * Improved success checking Added PHPUnit test (I think?) * Fixed success checking * Fixed test * Ran PHP CS Fixer * Added getAnalyticsDashboard to Zones Renamed toggleDevelopmentMode to changeDevelopmentMode to be uniform * Fixed test (hopefully) * Trying it a different way * Another attempt * Lol I guess we are just supressing errors for this one?
1 parent 3cfaed1 commit 3453e44

File tree

4 files changed

+268
-0
lines changed

4 files changed

+268
-0
lines changed

src/Endpoints/Zones.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,43 @@ public function getZoneID(string $name = ""): string
109109
return $zones->result[0]->id;
110110
}
111111

112+
/**
113+
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
114+
*
115+
* @param string $zoneID
116+
* @param string $since
117+
* @param string $until
118+
* @param bool $continuous
119+
* @return \stdClass
120+
*/
121+
public function getAnalyticsDashboard(string $zoneID, string $since = "-10080", string $until = "0", bool $continuous = true): \stdClass
122+
{
123+
$response = $this->adapter->get('zones/' . $zoneID . '/analytics/dashboard', [], ["since" => $since, "until" => $until, "continuous" => $continuous]);
124+
125+
return json_decode($response->getBody())->result;
126+
}
127+
128+
/**
129+
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
130+
*
131+
* @param string $zoneID
132+
* @param bool $enable
133+
* @return bool
134+
*/
135+
public function changeDevelopmentMode(string $zoneID, bool $enable = false): bool
136+
{
137+
$response = $this->adapter->patch('zones/' . $zoneID . '/settings/development_mode', [], ["value" => ($enable ? "on" : "off")]);
138+
139+
$body = json_decode($response->getBody());
140+
141+
if ($body->success) {
142+
return true;
143+
}
144+
145+
return false;
146+
}
147+
148+
112149
/**
113150
* Purge Everything
114151
* @param string $zoneID

tests/Endpoints/ZonesTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,49 @@ public function testGetZoneID()
129129
$this->assertEquals("023e105f4ecef8ad9ca31a8372d0c353", $result);
130130
}
131131

132+
public function testGetAnalyticsDashboard()
133+
{
134+
$response = $this->getPsr7JsonResponseForFixture('Endpoints/getAnalyticsDashboard.json');
135+
136+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
137+
$mock->method('get')->willReturn($response);
138+
139+
$mock->expects($this->once())
140+
->method('get')
141+
->with(
142+
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/analytics/dashboard'),
143+
$this->equalTo([]),
144+
$this->equalTo(["since" => "-10080", "until" => "0", "continuous" => true])
145+
);
146+
147+
$zones = new \Cloudflare\API\Endpoints\Zones($mock);
148+
$analytics = $zones->getAnalyticsDashboard("c2547eb745079dac9320b638f5e225cf483cc5cfdda41");
149+
150+
$this->assertObjectHasAttribute("since", $analytics->totals);
151+
$this->assertObjectHasAttribute("since", $analytics->timeseries[0]);
152+
}
153+
154+
public function testChangeDevelopmentMode()
155+
{
156+
$response = $this->getPsr7JsonResponseForFixture('Endpoints/changeDevelopmentMode.json');
157+
158+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
159+
$mock->method('patch')->willReturn($response);
160+
161+
$mock->expects($this->once())
162+
->method('patch')
163+
->with(
164+
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/development_mode'),
165+
$this->equalTo([]),
166+
$this->equalTo(["value" => "on"])
167+
);
168+
169+
$zones = new \Cloudflare\API\Endpoints\Zones($mock);
170+
$result = $zones->changeDevelopmentMode("c2547eb745079dac9320b638f5e225cf483cc5cfdda41", true);
171+
172+
$this->assertTrue($result);
173+
}
174+
132175
public function testCachePurgeEverything()
133176
{
134177
$response = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurgeEverything.json');
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"result": {
3+
"id": "development_mode",
4+
"value": "on",
5+
"modified_on": "2017-11-06T05:16:42.864300Z",
6+
"time_remaining": 10800,
7+
"editable": true
8+
},
9+
"success": true,
10+
"errors": [],
11+
"messages": []
12+
}
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
{
2+
"success": true,
3+
"errors": [
4+
{}
5+
],
6+
"messages": [
7+
{}
8+
],
9+
"result": {
10+
"totals": {
11+
"since": "2015-01-01T12:23:00Z",
12+
"until": "2015-01-02T12:23:00Z",
13+
"requests": {
14+
"all": 1234085328,
15+
"cached": 1234085328,
16+
"uncached": 13876154,
17+
"content_type": {
18+
"css": 15343,
19+
"html": 1234213,
20+
"javascript": 318236,
21+
"gif": 23178,
22+
"jpeg": 1982048
23+
},
24+
"country": {
25+
"US": 4181364,
26+
"AG": 37298,
27+
"GI": 293846
28+
},
29+
"ssl": {
30+
"encrypted": 12978361,
31+
"unencrypted": 781263
32+
},
33+
"http_status": {
34+
"200": 13496983,
35+
"301": 283,
36+
"400": 187936,
37+
"402": 1828,
38+
"404": 1293
39+
}
40+
},
41+
"bandwidth": {
42+
"all": 213867451,
43+
"cached": 113205063,
44+
"uncached": 113205063,
45+
"content_type": {
46+
"css": 237421,
47+
"html": 1231290,
48+
"javascript": 123245,
49+
"gif": 1234242,
50+
"jpeg": 784278
51+
},
52+
"country": {
53+
"US": 123145433,
54+
"AG": 2342483,
55+
"GI": 984753
56+
},
57+
"ssl": {
58+
"encrypted": 37592942,
59+
"unencrypted": 237654192
60+
}
61+
},
62+
"threats": {
63+
"all": 23423873,
64+
"country": {
65+
"US": 123,
66+
"CN": 523423,
67+
"AU": 91
68+
},
69+
"type": {
70+
"user.ban.ip": 123,
71+
"hot.ban.unknown": 5324,
72+
"macro.chl.captchaErr": 1341,
73+
"macro.chl.jschlErr": 5323
74+
}
75+
},
76+
"pageviews": {
77+
"all": 5724723,
78+
"search_engines": {
79+
"googlebot": 35272,
80+
"pingdom": 13435,
81+
"bingbot": 5372,
82+
"baidubot": 1345
83+
}
84+
},
85+
"uniques": {
86+
"all": 12343
87+
}
88+
},
89+
"timeseries": [
90+
{
91+
"since": "2015-01-01T12:23:00Z",
92+
"until": "2015-01-02T12:23:00Z",
93+
"requests": {
94+
"all": 1234085328,
95+
"cached": 1234085328,
96+
"uncached": 13876154,
97+
"content_type": {
98+
"css": 15343,
99+
"html": 1234213,
100+
"javascript": 318236,
101+
"gif": 23178,
102+
"jpeg": 1982048
103+
},
104+
"country": {
105+
"US": 4181364,
106+
"AG": 37298,
107+
"GI": 293846
108+
},
109+
"ssl": {
110+
"encrypted": 12978361,
111+
"unencrypted": 781263
112+
},
113+
"http_status": {
114+
"200": 13496983,
115+
"301": 283,
116+
"400": 187936,
117+
"402": 1828,
118+
"404": 1293
119+
}
120+
},
121+
"bandwidth": {
122+
"all": 213867451,
123+
"cached": 113205063,
124+
"uncached": 113205063,
125+
"content_type": {
126+
"css": 237421,
127+
"html": 1231290,
128+
"javascript": 123245,
129+
"gif": 1234242,
130+
"jpeg": 784278
131+
},
132+
"country": {
133+
"US": 123145433,
134+
"AG": 2342483,
135+
"GI": 984753
136+
},
137+
"ssl": {
138+
"encrypted": 37592942,
139+
"unencrypted": 237654192
140+
}
141+
},
142+
"threats": {
143+
"all": 23423873,
144+
"country": {
145+
"US": 123,
146+
"CN": 523423,
147+
"AU": 91
148+
},
149+
"type": {
150+
"user.ban.ip": 123,
151+
"hot.ban.unknown": 5324,
152+
"macro.chl.captchaErr": 1341,
153+
"macro.chl.jschlErr": 5323
154+
}
155+
},
156+
"pageviews": {
157+
"all": 5724723,
158+
"search_engines": {
159+
"googlebot": 35272,
160+
"pingdom": 13435,
161+
"bingbot": 5372,
162+
"baidubot": 1345
163+
}
164+
},
165+
"uniques": {
166+
"all": 12343
167+
}
168+
}
169+
]
170+
},
171+
"query": {
172+
"since": "2015-01-01T12:23:00Z",
173+
"until": "2015-01-02T12:23:00Z",
174+
"time_delta": 60
175+
}
176+
}

0 commit comments

Comments
 (0)