Skip to content

Commit

Permalink
feat: improved flexibility and custom models
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
samatcd committed Jul 7, 2021
1 parent b3f4cde commit 6be4e31
Show file tree
Hide file tree
Showing 19 changed files with 328 additions and 126 deletions.
79 changes: 52 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
[![Packagist](https://poser.pugx.org/custom-d/webhook-registry/d/total.svg)](https://packagist.org/packages/custom-d/webhook-registry)
[![Packagist](https://img.shields.io/packagist/l/custom-d/webhook-registry.svg)](https://packagist.org/packages/custom-d/webhook-registry)

Package description: CHANGE ME

@customD version

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.

## Installation

Expand All @@ -22,23 +20,6 @@ Install via composer
composer require custom-d/webhook-registry
```

### Register Service Provider

**Note! This and next step are optional if you use laravel>=5.5 with package
auto discovery feature.**

Add service provider to `config/app.php` in `providers` section
```php
CustomD\WebhookRegistry\ServiceProvider::class,
```

### Register Facade

Register package facade in `config/app.php` in `aliases` section
```php
CustomD\WebhookRegistry\Facades\WebhookRegistry::class,
```

### Publish Configuration File

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

## Usage

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.
### 1. Trigger registered webhooks via events

Your payload must define a `body`, but can also define `tags` and `meta` information for passing to `Spatie\WebhookServer\WebhookCall`.
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.

```

This contract defines several methods that need to be defined on your event, which will help provide the payload details.

Method | Purpose
-----|-----
`getWebhookPayload` | Returns the payload to send to the Spatie Webhook call. Useful if you want to completely override the payload.
`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.
`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.
`shouldDeliverWebhook` | Whether to deliver webhooks for this event. Defaults to true.

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`.

```php
/**
* Get the payload for this webhook event
*/
public function getWebhookPayload(): array
{
$user = request()->user();
return [
'body' => [
'status' => $this->status,
'triggered_by' => $user && $user->toArray()
]
];
}
```

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`.
### 2. Register webooks

Create webhook endpoints direction using the `CustomD\WebhookRegistry\Models\WebhookEndpoint` model, or use the facade `WebhookRegistry::registerEndpoint`.

```php
$endpoint = WebhookRegistry::registerEndpoint(
'https://webhook.site/custom/endpoint',
'My webhook endpoint name'
);
```

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.

```php
$endpoint = WebhookRegistry::registerEndpoint(
'https://localhost/webhook-test',
'My insecure endpoint',
false
);
```

### 3. Bind events to an endpoint

Associate a webhook event to an endpoint with the `CustomD\WebhookRegistry\Model\WebhookEvent` model, or using the facade `WebhookRegistry::registerEvent`.

```
$event = WebhookRegistry::registerEvent($webhook->id, 'App\Events\MyEvent');
```

## Models

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`.

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.

By default this scope doesn't do anything.

## Security

Expand Down
5 changes: 5 additions & 0 deletions config/webhook-registry.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php

return [
'models' => [
'endpoint' => \CustomD\WebhookRegistry\Models\WebhookEndpoint::class,
'event' => \CustomD\WebhookRegistry\Models\WebhookEvent::class,
'request' => \CustomD\WebhookRegistry\Models\WebhookRequest::class,
],
];
24 changes: 19 additions & 5 deletions src/Contracts/ShouldDeliverWebhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@

namespace CustomD\WebhookRegistry\Contracts;

interface ShouldDeliverWebhooks {
interface ShouldDeliverWebhooks
{

/**
* Get the webhook payload
*
* @return array
* Get the payload for this webhook event
*/
public function getWebhookPayload(): array;
public function getWebhookPayload(): array;

/**
* Get the payload context for this webhook event
*/
public function getWebhookContext(): array;

/**
* Should we allow this event to deliver webhooks?
*/
public function shouldDeliverWebhook(): bool;

/**
* Gets the event name.
*/
public function getWebhookEventName(): string;
}
51 changes: 51 additions & 0 deletions src/Events/Traits/DeliversWebhooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace CustomD\WebhookRegistry\Events\Traits;

use Illuminate\Contracts\Support\Arrayable;
trait DeliversWebhooks
{
/**
* Get the webhook payload
*/
public function getWebhookPayload(): array
{
$user = request()->user();

return [
'body' => [
'event' => $this->getWebhookEventName(),
'context' => $this->getWebhookContext(),
'triggered_by' => $user ? $user->toArray() : null,
]
];
}

/**
* The value that will be returned in the `payload`
*/
public function getWebhookContext(): array
{
if ($this->context instanceof Arrayable) {
return $this->context->toArray();
}

return $this->context ?? [];
}

/**
* Should we allow this event to deliver webhooks?
*/
public function shouldDeliverWebhook(): bool
{
return true;
}

/**
* Gets the event name.
*/
public function getWebhookEventName(): string
{
return $this->webhookEventName ?? static::class;
}
}
14 changes: 8 additions & 6 deletions src/Listeners/GeneralEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ class GeneralEventSubscriber
/**
* Register the listeners for the subscriber.
*
* @param \Illuminate\Events\Dispatcher $events
* @param \Illuminate\Events\Dispatcher $events
*/
public function subscribe($events)
{
$events->listen('App\Events*', function ($eventName, array $payloads) {
foreach($payloads as $payload){
if(WebhookRegistry::has($eventName)){
WebhookRegistry::trigger($eventName, $payload->getWebhookPayload());
$events->listen(
'App\Events*', function ($eventName, array $payloads) {
foreach($payloads as $payload){
if($payload->shouldDeliverWebhook() && WebhookRegistry::has($eventName)) {
WebhookRegistry::trigger($eventName, $payload->getWebhookPayload());
}
}
}
});
);
}
}
4 changes: 2 additions & 2 deletions src/Listeners/LoggingEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace CustomD\WebhookRegistry\Listeners;

use CustomD\WebhookRegistry\Models\WebhookRequest;
use Spatie\WebhookServer\Events\WebhookCallEvent;
use CustomD\WebhookRegistry\Model\WebhookRequest;
use Spatie\WebhookServer\Events\WebhookCallFailedEvent;
use Spatie\WebhookServer\Events\WebhookCallSucceededEvent;
use Spatie\WebhookServer\Events\FinalWebhookCallFailedEvent;
Expand Down Expand Up @@ -49,7 +49,7 @@ public function makeLogEntryFromEvent(WebhookCallEvent $event, string $status)
/**
* Register the listeners for the subscriber.
*
* @param \Illuminate\Events\Dispatcher $events
* @param \Illuminate\Events\Dispatcher $events
*/
public function subscribe($events)
{
Expand Down
31 changes: 0 additions & 31 deletions src/Model/WebhookEndpoint.php

This file was deleted.

21 changes: 0 additions & 21 deletions src/Model/WebhookEvent.php

This file was deleted.

14 changes: 14 additions & 0 deletions src/Models/Contracts/WebhookEndpointContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace CustomD\WebhookRegistry\Models\Contracts;

use Illuminate\Database\Eloquent\Relations\HasMany;

interface WebhookEndpointContract
{

/**
* Get the events relationship
*/
public function events(): HasMany;
}
20 changes: 20 additions & 0 deletions src/Models/Contracts/WebhookEventContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace CustomD\WebhookRegistry\Models\Contracts;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Builder;

interface WebhookEventContract
{

/**
* Get the endpoint relationship
*/
public function endpoint(): BelongsTo;

/**
* A scope which determines whether this webhook endpoint will actually send the webhook.
*/
public function scopeWhereDispatchable(Builder $builder): void;
}
15 changes: 15 additions & 0 deletions src/Models/Traits/GeneratesSecret.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace CustomD\WebhookRegistry\Models\Traits;

trait GeneratesSecret
{
public static function bootGeneratesSecret(): void
{
static::creating(
function ($model) {
$model->secret = bin2hex(random_bytes(8));
}
);
}
}
14 changes: 14 additions & 0 deletions src/Models/Traits/HasWebhookEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace CustomD\WebhookRegistry\Models\Traits;

use CustomD\WebhookRegistry\Models\WebhookEndpoint;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

trait HasWebhookEndpoint
{
public function endpoint(): BelongsTo
{
return $this->belongsTo(WebhookEndpoint::class, 'webhook_endpoint_id');
}
}
24 changes: 24 additions & 0 deletions src/Models/Traits/HasWebhookEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace CustomD\WebhookRegistry\Models\Traits;

use CustomD\WebhookRegistry\Models\WebhookEvent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasMany;

trait HasWebhookEvent
{
public static function bootHasWebhookEndpoints(): void
{
static::creating(
function ($endpoint) {
$endpoint->secret = bin2hex(random_bytes(8));
}
);
}

public function events(): HasMany
{
return $this->hasMany(WebhookEvent::class);
}
}
Loading

0 comments on commit 6be4e31

Please sign in to comment.