Skip to content

Commit

Permalink
Delete local development webhooks command.
Browse files Browse the repository at this point in the history
  • Loading branch information
sten committed Nov 6, 2024
1 parent 39355ad commit 5ef6bc3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
61 changes: 61 additions & 0 deletions src/Commands/SurveyheroWebhookDeleteLocalCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Statikbe\Surveyhero\Contracts\SurveyContract;
use Statikbe\Surveyhero\Services\SurveyWebhookService;
use Statikbe\Surveyhero\SurveyheroRegistrar;

class SurveyheroWebhookDeleteLocalCommand extends Command
{
public $signature = 'surveyhero:delete-local-webhooks
{--survey=all : The Surveyhero survey ID}
{--url=ngrok : The part of the url of the local URLs}';

public $description = 'Delete all local webhooks for Surveyhero surveys';

private SurveyWebhookService $webhookService;

public function __construct(SurveyWebhookService $webhookService)
{
parent::__construct();

$this->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;
}
}

0 comments on commit 5ef6bc3

Please sign in to comment.