From 9fca1b3d935e4d12ba0d4d75901729b4db96226c Mon Sep 17 00:00:00 2001
From: Jacob Fowler <jfowler@cloudflare.com>
Date: Tue, 17 Dec 2024 14:52:51 -0500
Subject: [PATCH 1/5] make cache purge environment aware

---
 src/Endpoints/Zones.php                       |  20 +++-
 tests/Endpoints/ZoneCacheTest.php             | 109 +++++++++++++++++-
 tests/Fixtures/Endpoints/getEnvironments.json |  40 +++++++
 3 files changed, 164 insertions(+), 5 deletions(-)
 create mode 100644 tests/Fixtures/Endpoints/getEnvironments.json

diff --git a/src/Endpoints/Zones.php b/src/Endpoints/Zones.php
index fd9e18b0..609f4808 100644
--- a/src/Endpoints/Zones.php
+++ b/src/Endpoints/Zones.php
@@ -1,4 +1,5 @@
 <?php
+
 /**
  * Created by PhpStorm.
  * User: junade
@@ -217,8 +218,15 @@ public function setCachingLevel(string $zoneID, string $level = 'aggressive'): b
      * @param string $zoneID
      * @return bool
      */
-    public function cachePurgeEverything(string $zoneID): bool
+    public function cachePurgeEverything(string $zoneID, bool $includeEnvironments = false): bool
     {
+        if ($includeEnvironments) {
+            $env = $this->adapter->get("zones/$zoneID/environments");
+            $envs = json_decode($env->getBody(), true);
+            foreach ($envs["result"]["environments"] as $env) {
+                $this->adapter->post("zones/$zoneID/environments/{$env["ref"]}/purge_cache", ['purge_everything' => true]);
+            }
+        }
         $user = $this->adapter->post('zones/' . $zoneID . '/purge_cache', ['purge_everything' => true]);
 
         $this->body = json_decode($user->getBody());
@@ -230,7 +238,7 @@ public function cachePurgeEverything(string $zoneID): bool
         return false;
     }
 
-    public function cachePurge(string $zoneID, array $files = null, array $tags = null, array $hosts = null): bool
+    public function cachePurge(string $zoneID, array $files = null, array $tags = null, array $hosts = null, bool $includeEnvironments = false): bool
     {
         if ($files === null && $tags === null && $hosts === null) {
             throw new EndpointException('No files, tags or hosts to purge.');
@@ -249,6 +257,14 @@ public function cachePurge(string $zoneID, array $files = null, array $tags = nu
             $options['hosts'] = $hosts;
         }
 
+        if ($includeEnvironments) {
+            $env = $this->adapter->get("zones/$zoneID/environments");
+            $envs = json_decode($env->getBody(), true);
+            foreach ($envs["result"]["environments"] as $env) {
+                $this->adapter->post("zones/$zoneID/environments/{$env["ref"]}/purge_cache", $options);
+            }
+        }
+
         $user = $this->adapter->post('zones/' . $zoneID . '/purge_cache', $options);
 
         $this->body = json_decode($user->getBody());
diff --git a/tests/Endpoints/ZoneCacheTest.php b/tests/Endpoints/ZoneCacheTest.php
index bdefeeca..13c2d9b1 100644
--- a/tests/Endpoints/ZoneCacheTest.php
+++ b/tests/Endpoints/ZoneCacheTest.php
@@ -61,9 +61,10 @@ public function testCachePurge()
             ->method('post')
             ->with(
                 $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'),
-                $this->equalTo(['files' => [
-                    'https://example.com/file.jpg',
-                ]
+                $this->equalTo([
+                    'files' => [
+                        'https://example.com/file.jpg',
+                    ]
                 ])
             );
 
@@ -75,4 +76,106 @@ public function testCachePurge()
         $this->assertTrue($result);
         $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id);
     }
+
+    public function testCachePurgeIncludingEnvironments()
+    {
+        $envResp = $this->getPsr7JsonResponseForFixture('Endpoints/getEnvironments.json');
+        $cacheResp = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurge.json');
+        $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
+
+        $mock->expects($this->once())
+            ->method('get')
+            ->willReturn($envResp)
+            ->with(
+                $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments'),
+            );
+
+        $mock->expects($this->exactly(4))
+            ->method('post')
+            ->willReturn($cacheResp)
+            ->withConsecutive(
+                [
+                    $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments/first/purge_cache'),
+                    $this->equalTo([
+                        'files' => [
+                            'https://example.com/file.jpg',
+                        ]
+                    ])
+                ],
+                [
+                    $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments/second/purge_cache'),
+                    $this->equalTo([
+                        'files' => [
+                            'https://example.com/file.jpg',
+                        ]
+                    ])
+                ],
+                [
+                    $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments/third/purge_cache'),
+                    $this->equalTo([
+                        'files' => [
+                            'https://example.com/file.jpg',
+                        ]
+                    ])
+                ],
+                [
+                    $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'),
+                    $this->equalTo([
+                        'files' => [
+                            'https://example.com/file.jpg',
+                        ]
+                    ])
+                ]
+            );
+
+        $zones = new \Cloudflare\API\Endpoints\Zones($mock);
+        $result = $zones->cachePurge('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', [
+            'https://example.com/file.jpg',
+        ], null, null, true);
+
+        $this->assertTrue($result);
+        $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id);
+    }
+
+    public function testCachePurgeEverythingIncludingEnvironments()
+    {
+        $envResp = $this->getPsr7JsonResponseForFixture('Endpoints/getEnvironments.json');
+        $cacheResp = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurgeEverything.json');
+        $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
+
+        $mock->expects($this->once())
+            ->method('get')
+            ->willReturn($envResp)
+            ->with(
+                $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments'),
+            );
+
+        $mock->expects($this->exactly(4))
+            ->method('post')
+            ->willReturn($cacheResp)
+            ->withConsecutive(
+                [
+                    $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments/first/purge_cache'),
+                    $this->equalTo(['purge_everything' => true])
+                ],
+                [
+                    $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments/second/purge_cache'),
+                    $this->equalTo(['purge_everything' => true])
+                ],
+                [
+                    $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments/third/purge_cache'),
+                    $this->equalTo(['purge_everything' => true])
+                ],
+                [
+                    $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'),
+                    $this->equalTo(['purge_everything' => true])
+                ]
+            );
+
+        $zones = new \Cloudflare\API\Endpoints\Zones($mock);
+        $result = $zones->cachePurgeEverything('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', true);
+
+        $this->assertTrue($result);
+        $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id);
+    }
 }
diff --git a/tests/Fixtures/Endpoints/getEnvironments.json b/tests/Fixtures/Endpoints/getEnvironments.json
new file mode 100644
index 00000000..3a97bfe9
--- /dev/null
+++ b/tests/Fixtures/Endpoints/getEnvironments.json
@@ -0,0 +1,40 @@
+{
+  "result": {
+    "environments": [
+      {
+        "name": "Production",
+        "ref": "first",
+        "version": 0,
+        "expression": "expression_1",
+        "locked_on_deployment": false,
+        "position": {
+          "before": "second"
+        }
+      },
+      {
+        "name": "Staging",
+        "ref": "second",
+        "version": 0,
+        "expression": "expression_2",
+        "locked_on_deployment": false,
+        "position": {
+          "before": "third",
+          "after": "first"
+        }
+      },
+      {
+        "name": "Development",
+        "ref": "third",
+        "version": 0,
+        "expression": "expression_3",
+        "locked_on_deployment": false,
+        "position": {
+          "after": "second"
+        }
+      }
+    ]
+  },
+  "success": true,
+  "errors": [],
+  "messages": []
+}

From 73b49cd90d784f7e88be86dfc66a6cc192999ac9 Mon Sep 17 00:00:00 2001
From: Jacob Fowler <6750990+jafowler@users.noreply.github.com>
Date: Tue, 17 Dec 2024 17:00:41 -0500
Subject: [PATCH 2/5] Update tests/Endpoints/ZoneCacheTest.php

remove commas to make <php7.2 happy

Co-authored-by: Jacob Bednarz <jacob.bednarz@hey.com>
---
 tests/Endpoints/ZoneCacheTest.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/Endpoints/ZoneCacheTest.php b/tests/Endpoints/ZoneCacheTest.php
index 13c2d9b1..fc1666a2 100644
--- a/tests/Endpoints/ZoneCacheTest.php
+++ b/tests/Endpoints/ZoneCacheTest.php
@@ -87,7 +87,7 @@ public function testCachePurgeIncludingEnvironments()
             ->method('get')
             ->willReturn($envResp)
             ->with(
-                $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments'),
+                $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments')
             );
 
         $mock->expects($this->exactly(4))

From 2dceed986e9c0f3bdc7a1cff3aab80791e14906c Mon Sep 17 00:00:00 2001
From: Jacob Bednarz <jacob.bednarz@hey.com>
Date: Wed, 18 Dec 2024 09:08:24 +1100
Subject: [PATCH 3/5] remove extra trailing comma

---
 tests/Endpoints/ZoneCacheTest.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/Endpoints/ZoneCacheTest.php b/tests/Endpoints/ZoneCacheTest.php
index fc1666a2..86bc04d5 100644
--- a/tests/Endpoints/ZoneCacheTest.php
+++ b/tests/Endpoints/ZoneCacheTest.php
@@ -147,7 +147,7 @@ public function testCachePurgeEverythingIncludingEnvironments()
             ->method('get')
             ->willReturn($envResp)
             ->with(
-                $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments'),
+                $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments')
             );
 
         $mock->expects($this->exactly(4))

From 0a78ed0915a92002a8c23d982403fa3b6cd81005 Mon Sep 17 00:00:00 2001
From: Jacob Fowler <jfowler@cloudflare.com>
Date: Tue, 17 Dec 2024 17:39:13 -0500
Subject: [PATCH 4/5] supress phpmd for zone cache functions

---
 src/Endpoints/Zones.php | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/Endpoints/Zones.php b/src/Endpoints/Zones.php
index 609f4808..59984e5d 100644
--- a/src/Endpoints/Zones.php
+++ b/src/Endpoints/Zones.php
@@ -217,6 +217,8 @@ public function setCachingLevel(string $zoneID, string $level = 'aggressive'): b
      * Purge Everything
      * @param string $zoneID
      * @return bool
+     *
+     * @SuppressWarnings(PHPMD)
      */
     public function cachePurgeEverything(string $zoneID, bool $includeEnvironments = false): bool
     {
@@ -238,6 +240,9 @@ public function cachePurgeEverything(string $zoneID, bool $includeEnvironments =
         return false;
     }
 
+    /*
+     *  @SuppressWarnings(PHPMD)
+     **/
     public function cachePurge(string $zoneID, array $files = null, array $tags = null, array $hosts = null, bool $includeEnvironments = false): bool
     {
         if ($files === null && $tags === null && $hosts === null) {

From 06412c0a0c9c7aaa8a1cd4a40918e95e0d835143 Mon Sep 17 00:00:00 2001
From: Jacob Bednarz <jacob.bednarz@hey.com>
Date: Wed, 18 Dec 2024 10:02:36 +1100
Subject: [PATCH 5/5] fix phpmd annotation

---
 src/Endpoints/Zones.php | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/Endpoints/Zones.php b/src/Endpoints/Zones.php
index 59984e5d..3a14c587 100644
--- a/src/Endpoints/Zones.php
+++ b/src/Endpoints/Zones.php
@@ -240,9 +240,9 @@ public function cachePurgeEverything(string $zoneID, bool $includeEnvironments =
         return false;
     }
 
-    /*
-     *  @SuppressWarnings(PHPMD)
-     **/
+    /**
+     * @SuppressWarnings(PHPMD)
+     */
     public function cachePurge(string $zoneID, array $files = null, array $tags = null, array $hosts = null, bool $includeEnvironments = false): bool
     {
         if ($files === null && $tags === null && $hosts === null) {