From 9dfe98cbefe212f2636d6a3cd879c587e193d1e9 Mon Sep 17 00:00:00 2001 From: Rico van Zelst Date: Tue, 14 Nov 2023 11:02:42 +0100 Subject: [PATCH] fix: only clear cloudflare on prod --- .../Commands/CloudflarePurgeCommand.php | 45 ++++++++++--------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/app/Console/Commands/CloudflarePurgeCommand.php b/app/Console/Commands/CloudflarePurgeCommand.php index 1e1257d..bde3e92 100644 --- a/app/Console/Commands/CloudflarePurgeCommand.php +++ b/app/Console/Commands/CloudflarePurgeCommand.php @@ -5,6 +5,7 @@ namespace App\Console\Commands; use GuzzleHttp\Exception\GuzzleException; use Illuminate\Console\Command; use GuzzleHttp\Client; +use Illuminate\Support\Facades\App; class CloudflarePurgeCommand extends Command { @@ -17,30 +18,32 @@ class CloudflarePurgeCommand extends Command */ public function handle(): void { - $client = new Client(); - $cf_zone_id = config('cloudflare.cf_zone_id'); - $cf_auth_bearer = config('cloudflare.cf_auth_bearer'); + if (App::environment('production')) { + $client = new Client(); + $cf_zone_id = config('cloudflare.cf_zone_id'); + $cf_auth_bearer = config('cloudflare.cf_auth_bearer'); - $response = $client->request( - 'POST', - "https://api.cloudflare.com/client/v4/zones/" . $cf_zone_id . "/purge_cache", - [ - 'headers' => [ - 'Authorization' => 'Bearer ' . $cf_auth_bearer, - 'Content-Type' => 'application/json', - ], - 'json' => [ - 'purge_everything' => true, - ], - ] - ); + $response = $client->request( + 'POST', + "https://api.cloudflare.com/client/v4/zones/" . $cf_zone_id . "/purge_cache", + [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $cf_auth_bearer, + 'Content-Type' => 'application/json', + ], + 'json' => [ + 'purge_everything' => true, + ], + ] + ); - $body = json_decode($response->getBody(), true); + $body = json_decode($response->getBody(), true); - if ($body['success'] === true) { - $this->info('Cloudflare cache purged successfully.'); - } else { - $this->error('Cloudflare cache could not be purged.'); + if ($body['success'] === true) { + $this->info('Cloudflare cache purged successfully.'); + } else { + $this->error('Cloudflare cache could not be purged.'); + } } } }