Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/Endpoints/Zones.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ public function cachePurgeEverything(string $zoneID): bool
return false;
}

public function cachePurge(string $zoneID, array $files = null, array $tags = null, array $hosts = null): bool
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the cyclomatic complexity of this method hit 10 with these additions, causing CI to fail. Some functionality will need to be extracted into smaller methods.

public function cachePurge(string $zoneID, array $files = null, array $tags = null, array $hosts = null, array $prefixes = null): bool
{
if ($files === null && $tags === null && $hosts === null) {
throw new EndpointException('No files, tags or hosts to purge.');
if ($files === null && $tags === null && $hosts === null && $prefixes === null) {
throw new EndpointException('No files, tags, hosts or prefixes to purge.');
}

$options = [];
Expand All @@ -247,6 +247,10 @@ public function cachePurge(string $zoneID, array $files = null, array $tags = nu
$options['hosts'] = $hosts;
}

if (!is_null($prefixes)) {
$options['prefixes'] = $prefixes;
}

$user = $this->adapter->post('zones/' . $zoneID . '/purge_cache', $options);

$this->body = json_decode($user->getBody());
Expand Down
30 changes: 30 additions & 0 deletions tests/Endpoints/ZonesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,34 @@ public function testCachePurge()
$this->assertTrue($result);
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id);
}

public function testCachePurgePrefix()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurgePrefix.json');

$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('post')->willReturn($response);

$mock->expects($this->once())
->method('post')
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'),
$this->equalTo(
[
'files' => [],
'tags' => [],
'hosts' => [],
'prefixes' => [
'https://example.com/path'
]
]
)
);

$zones = new \Cloudflare\API\Endpoints\Zones($mock);
$result = $zones->cachePurge('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', [], [], [], ['https://example.com/path']);

$this->assertTrue($result);
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id);
}
}
8 changes: 8 additions & 0 deletions tests/Fixtures/Endpoints/cachePurgePrefix.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "023e105f4ecef8ad9ca31a8372d0c353"
}
}