From 5ef6bc3be63b1a56dcd004a6df6426185e8073a2 Mon Sep 17 00:00:00 2001 From: Sten Govaerts Date: Wed, 6 Nov 2024 15:15:43 +0100 Subject: [PATCH] Delete local development webhooks command. --- composer.json | 2 +- .../SurveyheroWebhookDeleteLocalCommand.php | 61 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/Commands/SurveyheroWebhookDeleteLocalCommand.php diff --git a/composer.json b/composer.json index 7bf4681..ed5efc3 100644 --- a/composer.json +++ b/composer.json @@ -50,7 +50,7 @@ } }, "scripts": { - "analyse": "vendor/bin/phpstan analyse", + "analyse": "vendor/bin/phpstan analyse --memory-limit=1G", "test": "vendor/bin/pest", "test-coverage": "vendor/bin/pest --coverage", "format": "vendor/bin/pint" diff --git a/src/Commands/SurveyheroWebhookDeleteLocalCommand.php b/src/Commands/SurveyheroWebhookDeleteLocalCommand.php new file mode 100644 index 0000000..4326fb6 --- /dev/null +++ b/src/Commands/SurveyheroWebhookDeleteLocalCommand.php @@ -0,0 +1,61 @@ +webhookService = $webhookService; + } + + public function handle(): int + { + $surveyId = trim($this->option('survey')); + $urlPart = trim($this->option('url')); + + $surveyQuery = app(SurveyheroRegistrar::class)->getSurveyClass()::query(); + if ($surveyId !== 'all') { + $surveyQuery->where('surveyhero_id', $surveyId); + } + $surveys = $surveyQuery->get(); + + foreach ($surveys as $survey) { + /* @var SurveyContract $survey */ + try { + $webhooks = $this->webhookService->listWebhooks($survey); + $webhooksDeleted = 0; + foreach ($webhooks as $webhook) { + $host = parse_url($webhook->url)['host']; + if (Str::contains($host, $urlPart)) { + $this->webhookService->deleteWebhook($survey, $webhook->webhook_id); + $webhooksDeleted++; + } + } + $this->comment("Deleted local webhooks for survey '$survey->name' ($survey->surveyhero_id): $webhooksDeleted"); + } catch (\Exception $e) { + $this->error($e->getMessage()); + + return self::FAILURE; + } + } + + return self::SUCCESS; + } +}