Skip to content

Commit 6be4e31

Browse files
committed
feat: improved flexibility and custom models
- Allow more flexibility in when webhooks are fired - Allow implementors to use custom models - Improved documentation - Provide model contracts - No longer need to manually implement webhook payload methods, can now use DeliversWebhooks trait BREAKING: Changed existing model namespaces, and method names. Changed ShouldDeliverWebhooks contract.
1 parent b3f4cde commit 6be4e31

19 files changed

+328
-126
lines changed

README.md

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
[![Packagist](https://poser.pugx.org/custom-d/webhook-registry/d/total.svg)](https://packagist.org/packages/custom-d/webhook-registry)
1111
[![Packagist](https://img.shields.io/packagist/l/custom-d/webhook-registry.svg)](https://packagist.org/packages/custom-d/webhook-registry)
1212

13-
Package description: CHANGE ME
14-
15-
@customD version
1613

14+
A wrapper for [spatie/laravel-webhook-server](https://github.com/spatie/laravel-webhook-server) that provides a simple & flexible webhook registry, allows you to expose any Laravel Event as a webhook, and provides out-of-the-box logging of webhook results.
1715

1816
## Installation
1917

@@ -22,23 +20,6 @@ Install via composer
2220
composer require custom-d/webhook-registry
2321
```
2422

25-
### Register Service Provider
26-
27-
**Note! This and next step are optional if you use laravel>=5.5 with package
28-
auto discovery feature.**
29-
30-
Add service provider to `config/app.php` in `providers` section
31-
```php
32-
CustomD\WebhookRegistry\ServiceProvider::class,
33-
```
34-
35-
### Register Facade
36-
37-
Register package facade in `config/app.php` in `aliases` section
38-
```php
39-
CustomD\WebhookRegistry\Facades\WebhookRegistry::class,
40-
```
41-
4223
### Publish Configuration File
4324

4425
```bash
@@ -47,28 +28,72 @@ php artisan vendor:publish --provider="CustomD\WebhookRegistry\ServiceProvider"
4728

4829
## Usage
4930

50-
Implement the `CustomD\WebhookRegistry\Contracts\ShouldDeliverWebhooks` contract on any event you'd like to trigger a web-hook. This contract requires a `getWebhookPayload` method to be defined on your event, which will provide the payload details.
31+
### 1. Trigger registered webhooks via events
5132

52-
Your payload must define a `body`, but can also define `tags` and `meta` information for passing to `Spatie\WebhookServer\WebhookCall`.
33+
Implement the `CustomD\WebhookRegistry\Contracts\ShouldDeliverWebhooks` contract on any Laravel Event, and you'll be able to register webhooks that will be fired on this event.
5334

54-
```
35+
36+
This contract defines several methods that need to be defined on your event, which will help provide the payload details.
37+
38+
Method | Purpose
39+
-----|-----
40+
`getWebhookPayload` | Returns the payload to send to the Spatie Webhook call. Useful if you want to completely override the payload.
41+
`getWebhookContext` | Returns the payload `context` field. Useful if you want to set a different property. Defaults to returning the `->context` property from the event. If this property is `Arrayable`, we'll call `->toArray()`. You can overload this method on your event if you want to customise this behaviour.
42+
`getWebhookEventName` | Returns the event name you want to expose in the payload. Useful for endpoints who recieve multiple different types of event to determine how to handle calls. By default, this is the value of the `->webhookEventName` property from the event, otherwise we fall back to the event class name.
43+
`shouldDeliverWebhook` | Whether to deliver webhooks for this event. Defaults to true.
44+
45+
Your payload must define a `body`, but can also define [tags](https://github.com/spatie/laravel-webhook-server#adding-tags) and [meta](https://github.com/spatie/laravel-webhook-server#adding-meta-information) information for passing to `Spatie\WebhookServer\WebhookCall`.
46+
47+
```php
5548
/**
5649
* Get the payload for this webhook event
5750
*/
5851
public function getWebhookPayload(): array
5952
{
60-
$user = request()->user();
61-
6253
return [
6354
'body' => [
6455
'status' => $this->status,
65-
'triggered_by' => $user && $user->toArray()
6656
]
6757
];
6858
}
6959
```
7060

71-
Create webhook endpoints uing `CustomD\WebhookRegistry\Model\WebhookEndpoint` or using the facade `WebhookRegistry::registerEndpoint` method, and associate a webhook event to an endpoint with the `CustomD\WebhookRegistry\Model\WebhookEvent` model, or using the facade `WebhookRegistry::registerEvent`.
61+
### 2. Register webooks
62+
63+
Create webhook endpoints direction using the `CustomD\WebhookRegistry\Models\WebhookEndpoint` model, or use the facade `WebhookRegistry::registerEndpoint`.
64+
65+
```php
66+
$endpoint = WebhookRegistry::registerEndpoint(
67+
'https://webhook.site/custom/endpoint',
68+
'My webhook endpoint name'
69+
);
70+
```
71+
72+
By default, we'll verify SSL certificates on outgoing webhook connections. If you want to disable SSL verification, you can pass `false` to the `registerEndpoint` function.
73+
74+
```php
75+
$endpoint = WebhookRegistry::registerEndpoint(
76+
'https://localhost/webhook-test',
77+
'My insecure endpoint',
78+
false
79+
);
80+
```
81+
82+
### 3. Bind events to an endpoint
83+
84+
Associate a webhook event to an endpoint with the `CustomD\WebhookRegistry\Model\WebhookEvent` model, or using the facade `WebhookRegistry::registerEvent`.
85+
86+
```
87+
$event = WebhookRegistry::registerEvent($webhook->id, 'App\Events\MyEvent');
88+
```
89+
90+
## Models
91+
92+
Customise the WebhookEvent model in order to add logic about when events should fire. In `config/webhook-registry.php` you will see the model name being used in `models` -> `events`. You can make your own model by implementing the `CustomD\WebhookRegistry\Models\Contracts\WebhookEventContract`.
93+
94+
To specify which events are dispatchable in a given run, you must set a `scopeWhereDispatchable` on the model. This scope will be called before firing webhooks to endpoints with this event.
95+
96+
By default this scope doesn't do anything.
7297

7398
## Security
7499

config/webhook-registry.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<?php
22

33
return [
4+
'models' => [
5+
'endpoint' => \CustomD\WebhookRegistry\Models\WebhookEndpoint::class,
6+
'event' => \CustomD\WebhookRegistry\Models\WebhookEvent::class,
7+
'request' => \CustomD\WebhookRegistry\Models\WebhookRequest::class,
8+
],
49
];

src/Contracts/ShouldDeliverWebhooks.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,26 @@
22

33
namespace CustomD\WebhookRegistry\Contracts;
44

5-
interface ShouldDeliverWebhooks {
5+
interface ShouldDeliverWebhooks
6+
{
67

78
/**
8-
* Get the webhook payload
9-
*
10-
* @return array
9+
* Get the payload for this webhook event
1110
*/
12-
public function getWebhookPayload(): array;
11+
public function getWebhookPayload(): array;
12+
13+
/**
14+
* Get the payload context for this webhook event
15+
*/
16+
public function getWebhookContext(): array;
17+
18+
/**
19+
* Should we allow this event to deliver webhooks?
20+
*/
21+
public function shouldDeliverWebhook(): bool;
22+
23+
/**
24+
* Gets the event name.
25+
*/
26+
public function getWebhookEventName(): string;
1327
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace CustomD\WebhookRegistry\Events\Traits;
4+
5+
use Illuminate\Contracts\Support\Arrayable;
6+
trait DeliversWebhooks
7+
{
8+
/**
9+
* Get the webhook payload
10+
*/
11+
public function getWebhookPayload(): array
12+
{
13+
$user = request()->user();
14+
15+
return [
16+
'body' => [
17+
'event' => $this->getWebhookEventName(),
18+
'context' => $this->getWebhookContext(),
19+
'triggered_by' => $user ? $user->toArray() : null,
20+
]
21+
];
22+
}
23+
24+
/**
25+
* The value that will be returned in the `payload`
26+
*/
27+
public function getWebhookContext(): array
28+
{
29+
if ($this->context instanceof Arrayable) {
30+
return $this->context->toArray();
31+
}
32+
33+
return $this->context ?? [];
34+
}
35+
36+
/**
37+
* Should we allow this event to deliver webhooks?
38+
*/
39+
public function shouldDeliverWebhook(): bool
40+
{
41+
return true;
42+
}
43+
44+
/**
45+
* Gets the event name.
46+
*/
47+
public function getWebhookEventName(): string
48+
{
49+
return $this->webhookEventName ?? static::class;
50+
}
51+
}

src/Listeners/GeneralEventSubscriber.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ class GeneralEventSubscriber
1010
/**
1111
* Register the listeners for the subscriber.
1212
*
13-
* @param \Illuminate\Events\Dispatcher $events
13+
* @param \Illuminate\Events\Dispatcher $events
1414
*/
1515
public function subscribe($events)
1616
{
17-
$events->listen('App\Events*', function ($eventName, array $payloads) {
18-
foreach($payloads as $payload){
19-
if(WebhookRegistry::has($eventName)){
20-
WebhookRegistry::trigger($eventName, $payload->getWebhookPayload());
17+
$events->listen(
18+
'App\Events*', function ($eventName, array $payloads) {
19+
foreach($payloads as $payload){
20+
if($payload->shouldDeliverWebhook() && WebhookRegistry::has($eventName)) {
21+
WebhookRegistry::trigger($eventName, $payload->getWebhookPayload());
22+
}
2123
}
2224
}
23-
});
25+
);
2426
}
2527
}

src/Listeners/LoggingEventSubscriber.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace CustomD\WebhookRegistry\Listeners;
44

5+
use CustomD\WebhookRegistry\Models\WebhookRequest;
56
use Spatie\WebhookServer\Events\WebhookCallEvent;
6-
use CustomD\WebhookRegistry\Model\WebhookRequest;
77
use Spatie\WebhookServer\Events\WebhookCallFailedEvent;
88
use Spatie\WebhookServer\Events\WebhookCallSucceededEvent;
99
use Spatie\WebhookServer\Events\FinalWebhookCallFailedEvent;
@@ -49,7 +49,7 @@ public function makeLogEntryFromEvent(WebhookCallEvent $event, string $status)
4949
/**
5050
* Register the listeners for the subscriber.
5151
*
52-
* @param \Illuminate\Events\Dispatcher $events
52+
* @param \Illuminate\Events\Dispatcher $events
5353
*/
5454
public function subscribe($events)
5555
{

src/Model/WebhookEndpoint.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Model/WebhookEvent.php

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace CustomD\WebhookRegistry\Models\Contracts;
4+
5+
use Illuminate\Database\Eloquent\Relations\HasMany;
6+
7+
interface WebhookEndpointContract
8+
{
9+
10+
/**
11+
* Get the events relationship
12+
*/
13+
public function events(): HasMany;
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace CustomD\WebhookRegistry\Models\Contracts;
4+
5+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
6+
use Illuminate\Database\Eloquent\Builder;
7+
8+
interface WebhookEventContract
9+
{
10+
11+
/**
12+
* Get the endpoint relationship
13+
*/
14+
public function endpoint(): BelongsTo;
15+
16+
/**
17+
* A scope which determines whether this webhook endpoint will actually send the webhook.
18+
*/
19+
public function scopeWhereDispatchable(Builder $builder): void;
20+
}

0 commit comments

Comments
 (0)