-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
19 changed files
with
328 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.