Skip to content

Commit 08a34ee

Browse files
authored
Merge pull request #124 from regnerisch/main
feat: add dispatchIf, dispatchUnless, dispatchSyncIf and dispatchSync…
2 parents 1f0bac8 + d3b7f24 commit 08a34ee

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,28 @@ WebhookCall::create()
118118
->dispatchSync();
119119
```
120120

121+
### Conditionally sending webhooks
122+
123+
If you would like to conditionally dispatch a webhook, you may use the `dispatchIf`, `dispatchUnless`, `dispatchSyncIf` and `dispatchSyncUnless` methods:
124+
125+
```php
126+
WebhookCall::create()
127+
...
128+
->dispatchIf($condition);
129+
130+
WebhookCall::create()
131+
...
132+
->dispatchUnless($condition);
133+
134+
WebhookCall::create()
135+
...
136+
->dispatchSyncIf($condition);
137+
138+
WebhookCall::create()
139+
...
140+
->dispatchSyncUnless($condition);
141+
```
142+
121143
### How signing requests works
122144

123145
When setting up, it's common to generate, store, and share a secret between your app and the app that wants to receive webhooks. Generating the secret could be done with `Illuminate\Support\Str::random()`, but it's entirely up to you. The package will use the secret to sign a webhook call.

src/WebhookCall.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,39 @@ public function dispatch(): PendingDispatch
200200
return dispatch($this->callWebhookJob);
201201
}
202202

203+
public function dispatchIf($condition): PendingDispatch|null
204+
{
205+
if ($condition) {
206+
return $this->dispatch();
207+
}
208+
209+
return null;
210+
}
211+
212+
public function dispatchUnless($condition): PendingDispatch|null
213+
{
214+
return $this->dispatchIf(!$condition);
215+
}
216+
203217
public function dispatchSync(): void
204218
{
205219
$this->prepareForDispatch();
206220

207221
dispatch_sync($this->callWebhookJob);
208222
}
209223

224+
public function dispatchSyncIf($condition): void
225+
{
226+
if ($condition) {
227+
$this->dispatchSync();
228+
}
229+
}
230+
231+
public function dispatchSyncUnless($condition): void
232+
{
233+
$this->dispatchSyncIf(!$condition);
234+
}
235+
210236
protected function prepareForDispatch(): void
211237
{
212238
if (! $this->callWebhookJob->webhookUrl) {

tests/WebhookTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,44 @@ public function it_can_get_the_uuid_property()
151151
$this->assertIsString($webhookCall->getUuid());
152152
$this->assertSame('my-unique-identifier', $webhookCall->getUuid());
153153
}
154+
155+
/** @test */
156+
public function it_can_dispatch_a_job_that_calls_a_webhook_if_condition_true()
157+
{
158+
$url = 'https://localhost';
159+
160+
WebhookCall::create()->url($url)->useSecret('123')->dispatchIf(true);
161+
162+
Queue::assertPushed(CallWebhookJob::class);
163+
}
164+
165+
/** @test */
166+
public function it_can_not_dispatch_a_job_that_calls_a_webhook_if_condition_false()
167+
{
168+
$url = 'https://localhost';
169+
170+
WebhookCall::create()->url($url)->useSecret('123')->dispatchIf(false);
171+
172+
Queue::assertNotPushed(CallWebhookJob::class);
173+
}
174+
175+
/** @test */
176+
public function it_can_not_dispatch_a_job_that_calls_a_webhook_unless_condition_true()
177+
{
178+
$url = 'https://localhost';
179+
180+
WebhookCall::create()->url($url)->useSecret('123')->dispatchUnless(true);
181+
182+
Queue::assertNotPushed(CallWebhookJob::class);
183+
}
184+
185+
/** @test */
186+
public function it_can_dispatch_a_job_that_calls_a_webhook_unless_condition_false()
187+
{
188+
$url = 'https://localhost';
189+
190+
WebhookCall::create()->url($url)->useSecret('123')->dispatchUnless(false);
191+
192+
Queue::assertPushed(CallWebhookJob::class);
193+
}
154194
}

0 commit comments

Comments
 (0)