feat: cloudflare purge command

This commit is contained in:
Rico van Zelst
2023-11-09 16:22:14 +01:00
parent 7c7db0844b
commit 36009e116e
4 changed files with 67 additions and 1 deletions

View File

@@ -0,0 +1,46 @@
<?php
namespace App\Console\Commands;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;
use GuzzleHttp\Client;
class CloudflarePurgeCommand extends Command
{
protected $signature = 'cloudflare:purge';
protected $description = 'Purge the Cloudflare cache.';
/**
* @throws GuzzleException
*/
public function handle(): void
{
$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,
],
]
);
$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.');
}
}
}