Skip to content

Commit 3d5e692

Browse files
committed
Added command to list & delete webhooks in Surveyhero
1 parent 3ed4f01 commit 3d5e692

7 files changed

+184
-3
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,22 @@ With the command below you can add a webhook to your Survey:
451451
php artisan surveyhero:add-webhooks --eventType=response.completed --url=https://webhook.site/complete-response
452452
```
453453

454+
### List webhooks
455+
456+
With the command below you can list all the webhooks of your Survey:
457+
458+
```shell
459+
php artisan surveyhero:list-webhooks --survey=12345
460+
```
461+
462+
### Delete a webhook
463+
464+
With the command below you can delete a webhook from your Survey:
465+
466+
```shell
467+
php artisan surveyhero:delete-webhook --survey=12345 --webhook=2553
468+
```
469+
454470
### Webhook handlers
455471

456472
We have also implemented a default controller (`SurveyheroWebhookController.php`) to handle the webhook responses. Currently, it only supports the

src/Commands/SurveyheroWebhookCommand.php src/Commands/SurveyheroWebhookAddCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Statikbe\Surveyhero\Services\SurveyWebhookService;
88
use Statikbe\Surveyhero\SurveyheroRegistrar;
99

10-
class SurveyheroWebhookCommand extends Command
10+
class SurveyheroWebhookAddCommand extends Command
1111
{
1212
public $signature = 'surveyhero:add-webhooks
1313
{--survey=all : The Surveyhero survey ID}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Statikbe\Surveyhero\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Statikbe\Surveyhero\Contracts\SurveyContract;
7+
use Statikbe\Surveyhero\Services\SurveyWebhookService;
8+
use Statikbe\Surveyhero\SurveyheroRegistrar;
9+
10+
class SurveyheroWebhookDeleteCommand extends Command
11+
{
12+
public $signature = 'surveyhero:delete-webhook
13+
{--survey= : The Surveyhero survey ID}
14+
{--webhook= : The Surveyhero webhook ID}';
15+
16+
public $description = 'Delete Surveyhero webhooks';
17+
18+
/**
19+
* @var \Statikbe\Surveyhero\Services\SurveyWebhookService
20+
*/
21+
private SurveyWebhookService $webhookService;
22+
23+
public function __construct(SurveyWebhookService $webhookService)
24+
{
25+
parent::__construct();
26+
27+
$this->webhookService = $webhookService;
28+
}
29+
30+
public function handle(): int
31+
{
32+
$surveyId = trim($this->option('survey'));
33+
$webhookId = trim($this->option('webhook'));
34+
35+
$surveys = app(SurveyheroRegistrar::class)->getSurveyClass()::query()->where('surveyhero_id', $surveyId)->get();
36+
37+
foreach ($surveys as $survey) {
38+
/* @var SurveyContract $survey */
39+
try {
40+
$this->webhookService->deleteWebhook($survey, $webhookId);
41+
$this->comment('Webhook #'.$webhookId.' for survey #'.$surveyId.' deleted');
42+
} catch (\Exception $e) {
43+
$this->error($e->getMessage());
44+
45+
return self::FAILURE;
46+
}
47+
}
48+
49+
return self::SUCCESS;
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Statikbe\Surveyhero\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Statikbe\Surveyhero\Contracts\SurveyContract;
7+
use Statikbe\Surveyhero\Services\SurveyWebhookService;
8+
use Statikbe\Surveyhero\SurveyheroRegistrar;
9+
10+
class SurveyheroWebhookListCommand extends Command
11+
{
12+
public $signature = 'surveyhero:list-webhooks
13+
{--survey=all : The Surveyhero survey ID}';
14+
15+
public $description = 'List webhooks for Surveyhero surveys';
16+
17+
/**
18+
* @var \Statikbe\Surveyhero\Services\SurveyWebhookService
19+
*/
20+
private SurveyWebhookService $webhookService;
21+
22+
public function __construct(SurveyWebhookService $webhookService)
23+
{
24+
parent::__construct();
25+
26+
$this->webhookService = $webhookService;
27+
}
28+
29+
public function handle(): int
30+
{
31+
$surveyId = trim($this->option('survey'));
32+
33+
$surveyQuery = app(SurveyheroRegistrar::class)->getSurveyClass()::query();
34+
if ($surveyId !== 'all') {
35+
$surveyQuery->where('surveyhero_id', $surveyId);
36+
}
37+
$surveys = $surveyQuery->get();
38+
39+
foreach ($surveys as $survey) {
40+
/* @var SurveyContract $survey */
41+
try {
42+
$webhooks = $this->webhookService->listWebhooks($survey);
43+
$webhookData = [];
44+
foreach($webhooks as $webhook) {
45+
$webhookData[] = [
46+
$webhook->webhook_id,
47+
$webhook->event_type,
48+
$webhook->url,
49+
$webhook->status,
50+
$webhook->created_on,
51+
];
52+
}
53+
$this->table(
54+
['Webhook id','Event type','Url','Status','Created'],
55+
$webhookData
56+
);
57+
} catch (\Exception $e) {
58+
$this->error($e->getMessage());
59+
60+
return self::FAILURE;
61+
}
62+
}
63+
64+
return self::SUCCESS;
65+
}
66+
}

src/Http/SurveyheroClient.php

+21
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Carbon\Carbon;
66
use Illuminate\Support\Facades\Http;
7+
use stdClass;
78

89
class SurveyheroClient
910
{
@@ -77,6 +78,13 @@ public function getSurveyLanguages(string|int $surveyId): array
7778
return $languages ? $languages->languages : [];
7879
}
7980

81+
public function listWebhooks(string|int $surveyId): ?array
82+
{
83+
$webhookData = $this->fetchFromSurveyHero(sprintf('surveys/%s/webhooks', $surveyId));
84+
85+
return $webhookData->successful() ? json_decode($webhookData->body())->webhooks : null;
86+
}
87+
8088
public function createWebhook(string|int $surveyId, string $eventType, string $url, string $status = 'active')
8189
{
8290
$body = [
@@ -88,6 +96,13 @@ public function createWebhook(string|int $surveyId, string $eventType, string $u
8896
$this->postToSurveyHero(sprintf('surveys/%s/webhooks', $surveyId), $body);
8997
}
9098

99+
public function deleteWebhook(string|int $surveyId, string|int $webhookId): ?stdClass
100+
{
101+
$webhookData = $this->deleteFromSurveyHero(sprintf('surveys/%s/webhooks/%s', $surveyId, $webhookId));
102+
103+
return $webhookData->successful() ? json_decode($webhookData->body()) : null;
104+
}
105+
91106
public function deleteResponse(string|int $surveyId, string|int $responseId)
92107
{
93108
$this->deleteFromSurveyHero(sprintf('surveys/%s/responses/%s', $surveyId, $responseId));
@@ -98,6 +113,9 @@ public function transformAPITimestamp(string $surveyheroTimestamp): Carbon
98113
return Carbon::createFromFormat('Y-m-d\TH:i:s', substr($surveyheroTimestamp, 0, strpos($surveyheroTimestamp, '+')));
99114
}
100115

116+
/**
117+
* @throws \Exception
118+
*/
101119
private function fetchFromSurveyHero(string $urlPath, array $queryStringArgs = []): \Illuminate\Http\Client\Response
102120
{
103121
$this->preventThrottle();
@@ -115,6 +133,9 @@ private function fetchFromSurveyHero(string $urlPath, array $queryStringArgs = [
115133
throw new \Exception($response->body());
116134
}
117135

136+
/**
137+
* @throws \Exception
138+
*/
118139
private function postToSurveyHero(string $urlPath, array $queryStringArgs = []): \Illuminate\Http\Client\Response
119140
{
120141
$this->preventThrottle();

src/Services/SurveyWebhookService.php

+23
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66

77
class SurveyWebhookService extends AbstractSurveyheroAPIService
88
{
9+
/**
10+
* Lists all webhooks for a certain Surveyhero survey
11+
*
12+
* @param SurveyContract $survey
13+
* @return void
14+
*/
15+
public function listWebhooks(SurveyContract $survey): ?array
16+
{
17+
return $this->client->listWebhooks($survey->surveyhero_id);
18+
}
19+
920
/**
1021
* Creates a webhook for Surveyhero to notify on the given event type.
1122
*
@@ -16,4 +27,16 @@ public function createWebhook(SurveyContract $survey, string $eventType, string
1627
{
1728
$this->client->createWebhook($survey->surveyhero_id, $eventType, $url, 'active');
1829
}
30+
31+
/**
32+
* Deletes a webhooks for a certain Surveyhero survey
33+
*
34+
* @param SurveyContract $survey
35+
* @param string|int $webhookId
36+
* @return void
37+
*/
38+
public function deleteWebhook(SurveyContract $survey, string|int $webhookId): void
39+
{
40+
$this->client->deleteWebhook($survey->surveyhero_id, $webhookId);
41+
}
1942
}

src/SurveyheroServiceProvider.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
use Statikbe\Surveyhero\Commands\SurveyheroQuestionsAndAnswersImportCommand;
99
use Statikbe\Surveyhero\Commands\SurveyheroResponseImportCommand;
1010
use Statikbe\Surveyhero\Commands\SurveyheroSurveyImportCommand;
11-
use Statikbe\Surveyhero\Commands\SurveyheroWebhookCommand;
11+
use Statikbe\Surveyhero\Commands\SurveyheroWebhookAddCommand;
12+
use Statikbe\Surveyhero\Commands\SurveyheroWebhookDeleteCommand;
13+
use Statikbe\Surveyhero\Commands\SurveyheroWebhookListCommand;
1214
use Statikbe\Surveyhero\Commands\SurveyResponseExportCommand;
1315

1416
class SurveyheroServiceProvider extends PackageServiceProvider
@@ -30,7 +32,9 @@ public function configurePackage(Package $package): void
3032
SurveyheroResponseImportCommand::class,
3133
SurveyheroQuestionsAndAnswersImportCommand::class,
3234
SurveyheroMapperCommand::class,
33-
SurveyheroWebhookCommand::class,
35+
SurveyheroWebhookAddCommand::class,
36+
SurveyheroWebhookListCommand::class,
37+
SurveyheroWebhookDeleteCommand::class,
3438
SurveyResponseExportCommand::class,
3539
]);
3640
}

0 commit comments

Comments
 (0)