Skip to content

Commit

Permalink
Event implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
samatcd committed Sep 30, 2020
1 parent 229f1fa commit f9a9069
Show file tree
Hide file tree
Showing 16 changed files with 326 additions and 150 deletions.
3 changes: 1 addition & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2019 <>
Copyright (c) 2020 Custom D

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -19,4 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,34 @@ php artisan vendor:publish --provider="CustomD\WebhookRegistry\ServiceProvider"

## Usage

CHANGE ME
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.

Your payload must define a `body`, but can also define `tags` and `meta` information for passing to `Spatie\WebhookServer\WebhookCall`.

```
/**
* Get the webhook payload
*
* @return string
*/
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`.

## Security

If you discover any security related issues, please email
If you discover any security related issues, please email
instead of using the issue tracker.

## Credits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateWebhookConsumersTable extends Migration
class CreateWebhookEndpointsTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::create('webhook_consumers', function (Blueprint $table) {
Schema::create('webhook_endpoints', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('token');
$table->string('description')->nullable();
$table->string('secret');
$table->string('base_url');
$table->smallInteger('verify_ssl');
$table->smallInteger('verify_ssl')->default(1);
$table->softDeletes();
$table->timestamps();
});
}
Expand All @@ -26,6 +27,6 @@ public function up()
*/
public function down()
{
Schema::dropIfExists('webhook_consumers');
Schema::dropIfExists('webhook_endpoints');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,19 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateWebhookCallsTable extends Migration
class CreateWebhookEventsTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::create('webhook_calls', function (Blueprint $table) {
Schema::create('webhook_events', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('url_path');
$table->string('event');
$table->unsignedBigInteger('webhook_consumer_id');
$table->foreignId('webhook_endpoint_id')->constrained('webhook_endpoints')->onDelete('cascade')->onUpdate('restrict');
$table->softDeletes();
$table->timestamps();

$table->foreign('webhook_consumer_id')->references('id')->on('webhook_calls')->onDelete('CASCADE')->onUpdate('RESTRICT');
});
}

Expand All @@ -29,6 +25,6 @@ public function up()
*/
public function down()
{
Schema::dropIfExists('webhook_calls');
Schema::dropIfExists('webhook_events');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateWebhookRequestsTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::create('webhook_requests', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('uuid');
$table->string('httpVerb');
$table->string('webhookUrl');
$table->string('status');
$table->string('payload')->nullable();
$table->string('meta')->nullable();
$table->string('responseBody')->nullable();
$table->integer('attempt');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down()
{
Schema::dropIfExists('webhook_requests');
}
}
13 changes: 13 additions & 0 deletions src/Contracts/ShouldDeliverWebhooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace CustomD\WebhookRegistry\Contracts;

interface ShouldDeliverWebhooks {

/**
* Get the webhook payload
*
* @return array
*/
public function getWebhookPayload(): array;
}
25 changes: 25 additions & 0 deletions src/Listeners/GeneralEventSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace CustomD\WebhookRegistry\Listeners;

use CustomD\WebhookRegistry\Facades\WebhookRegistry;

class GeneralEventSubscriber
{

/**
* Register the listeners for the subscriber.
*
* @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());
}
}
});
}
}
71 changes: 71 additions & 0 deletions src/Listeners/LoggingEventSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace CustomD\WebhookRegistry\Listeners;

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;

class LoggingEventSubscriber
{

public function recordWebhookSuccess(WebhookCallSucceededEvent $event)
{
WebhookRequest::create(
$this->makeLogEntryFromEvent($event, 'success')
);
}

public function recordWebhookFailed(WebhookCallFailedEvent $event)
{
WebhookRequest::create(
$this->makeLogEntryFromEvent($event, 'failed')
);
}

public function recordWebhookFailedFinal(FinalWebhookCallFailedEvent $event)
{
WebhookRequest::create(
$this->makeLogEntryFromEvent($event, 'failed')
);
}

public function makeLogEntryFromEvent(WebhookCallEvent $event, string $status)
{
return [
'uuid' => $event->uuid,
'httpVerb' => $event->httpVerb,
'webhookUrl' => $event->webhookUrl,
'meta' => $event->meta ? json_encode($event->meta) : null,
'payload' => json_encode($event->payload),
'responseBody' => $event->response ? $event->response->getBody() : null,
'attempt' => $event->attempt,
'status' => $status,
];
}

/**
* Register the listeners for the subscriber.
*
* @param \Illuminate\Events\Dispatcher $events
*/
public function subscribe($events)
{
$className = self::class;

$events->listen(
WebhookCallSucceededEvent::class,
"$className@recordWebhookSuccess"
);
$events->listen(
WebhookCallFailedEvent::class,
"$className@recordWebhookFailed"
);
$events->listen(
FinalWebhookCallFailedEvent::class,
"$className@recordWebhookFailedFinal"
);
}
}
50 changes: 0 additions & 50 deletions src/Model/WebhookCalls.php

This file was deleted.

40 changes: 0 additions & 40 deletions src/Model/WebhookConsumers.php

This file was deleted.

31 changes: 31 additions & 0 deletions src/Model/WebhookEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace CustomD\WebhookRegistry\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class WebhookEndpoint extends Model
{
use SoftDeletes;

protected $fillable = [
'description',
'base_url',
'verify_ssl',
];

protected static function boot()
{
parent::boot();

static::creating(function ($endpoint) {
$endpoint->secret = bin2hex(random_bytes(8));
});
}

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

0 comments on commit f9a9069

Please sign in to comment.