Skip to content

Commit 49d9d1d

Browse files
committed
Add event that is being fired upon the webhook's dispatch
1 parent 5b90694 commit 49d9d1d

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Spatie\WebhookServer\Events;
4+
5+
class WebhookCallDispatchedEvent
6+
{
7+
public function __construct(
8+
public string $httpVerb,
9+
public string $webhookUrl,
10+
public array|string $payload,
11+
public array $headers,
12+
public array $meta,
13+
public array $tags,
14+
public string $uuid,
15+
) {
16+
}
17+
}

src/WebhookCall.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Foundation\Bus\PendingDispatch;
66
use Illuminate\Support\Str;
77
use Spatie\WebhookServer\BackoffStrategy\BackoffStrategy;
8+
use Spatie\WebhookServer\Events\WebhookCallDispatchedEvent;
89
use Spatie\WebhookServer\Exceptions\CouldNotCallWebhook;
910
use Spatie\WebhookServer\Exceptions\InvalidBackoffStrategy;
1011
use Spatie\WebhookServer\Exceptions\InvalidSigner;
@@ -229,6 +230,16 @@ public function dispatch(): PendingDispatch
229230
{
230231
$this->prepareForDispatch();
231232

233+
event(new WebhookCallDispatchedEvent(
234+
$this->callWebhookJob->httpVerb,
235+
$this->callWebhookJob->webhookUrl,
236+
$this->callWebhookJob->payload,
237+
$this->callWebhookJob->headers,
238+
$this->callWebhookJob->meta,
239+
$this->callWebhookJob->tags,
240+
$this->callWebhookJob->uuid,
241+
));
242+
232243
return dispatch($this->callWebhookJob);
233244
}
234245

tests/WebhookTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

3+
use Illuminate\Support\Facades\Event;
34
use Illuminate\Support\Facades\Queue;
5+
use Spatie\WebhookServer\Events\WebhookCallDispatchedEvent;
46
use function PHPUnit\Framework\assertTrue;
57
use Spatie\WebhookServer\CallWebhookJob;
68
use Spatie\WebhookServer\Exceptions\CouldNotCallWebhook;
@@ -155,3 +157,13 @@
155157

156158
Queue::assertPushed(CallWebhookJob::class);
157159
});
160+
161+
it('will fire an event when a webhook is initially dispatched', function () {
162+
Event::fake();
163+
164+
$url = 'https://localhost';
165+
166+
WebhookCall::create()->url($url)->useSecret('123')->dispatchIf(true);
167+
168+
Event::assertDispatched(WebhookCallDispatchedEvent::class, 1);
169+
});

0 commit comments

Comments
 (0)