Skip to content

Commit

Permalink
Merge pull request #15 from spatie/store-model
Browse files Browse the repository at this point in the history
Add possibility the change how a WebookCall model is stored
  • Loading branch information
freekmurze authored Jul 9, 2019
2 parents 2fb3697 + 8a0595c commit 5b2afed
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ After the signature is validated and the webhook profile has determined that the

The request will first be stored in the `webhook_calls` table. This is done using the `WebhookCall` model. Should you want to customize the table name or anything on the storage behavior, you can let the package use an alternative model. A webhook storing model can be specified in the `webhook_model`. Make sure you model extends `Spatie\WebhookClient\Models\WebhookCall`.

You can change how the `WebhookCall` model is stored, by overriding the `storeWebhook` method of `WebhookCall`. In the `storeWebhook` method you should return a in the database stored model.

Next, the newly created `WebhookCall` model will be passed to a queued job that will process the request. Any class that extends `\Spatie\WebhookClient\ProcessWebhookJob` is a valid job. Here's an example:

```php
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/WebhookFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class WebhookFailed extends Exception
{
public static function invalidSignature(): WebhookFailed
{
return new static("The signature is invalid.");
return new static('The signature is invalid.');
}

public static function signingSecretNotSet(): WebhookFailed
Expand Down
10 changes: 10 additions & 0 deletions src/Models/WebhookCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Spatie\WebhookClient\Models;

use Exception;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use Spatie\WebhookClient\WebhookConfig;

class WebhookCall extends Model
{
Expand All @@ -14,6 +16,14 @@ class WebhookCall extends Model
'exception' => 'array',
];

public static function storeWebhook(WebhookConfig $config, Request $request): WebhookCall
{
return self::create([
'name' => $config->name,
'payload' => $request->input(),
]);
}

public function saveException(Exception $exception)
{
$this->exception = [
Expand Down
3 changes: 1 addition & 2 deletions src/SignatureValidator/DefaultSignatureValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Spatie\WebhookClient\SignatureValidator;

use Illuminate\Http\Request;
use Spatie\WebhookClient\Events\InvalidSignatureEvent;
use Spatie\WebhookClient\WebhookConfig;
use Spatie\WebhookClient\Exceptions\WebhookFailed;

Expand All @@ -14,7 +13,7 @@ public function isValid(Request $request, WebhookConfig $config): bool
$signature = $request->header($config->signatureHeaderName);

if (! $signature) {
return false;
return false;
}

$signingSecret = $config->signingSecret;
Expand Down
5 changes: 1 addition & 4 deletions src/WebhookProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ protected function ensureValidSignature()

protected function storeWebhook(): WebhookCall
{
return $this->config->webhookModel::create([
'name' => $this->config->name,
'payload' => $this->request->input(),
]);
return $this->config->webhookModel::storeWebhook($this->config, $this->request);
}

protected function processWebhook(WebhookCall $webhookCall): void
Expand Down
6 changes: 2 additions & 4 deletions tests/TestClasses/EverythingIsValidSignatureValidator.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
<?php


namespace Spatie\WebhookClient\Tests\TestClasses;


use Illuminate\Http\Request;
use Spatie\WebhookClient\SignatureValidator\SignatureValidator;
use Spatie\WebhookClient\WebhookConfig;
use Spatie\WebhookClient\SignatureValidator\SignatureValidator;

class EverythingIsValidSignatureValidator implements SignatureValidator
{
public function isValid(Request $request, WebhookConfig $config): bool
{
return true;
}
}
}
6 changes: 2 additions & 4 deletions tests/TestClasses/NothingIsValidSignatureValidator.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
<?php


namespace Spatie\WebhookClient\Tests\TestClasses;


use Illuminate\Http\Request;
use Spatie\WebhookClient\SignatureValidator\SignatureValidator;
use Spatie\WebhookClient\WebhookConfig;
use Spatie\WebhookClient\SignatureValidator\SignatureValidator;

class NothingIsValidSignatureValidator implements SignatureValidator
{
public function isValid(Request $request, WebhookConfig $config): bool
{
return false;
}
}
}
18 changes: 18 additions & 0 deletions tests/TestClasses/WebhookModelWithoutPayloadSaved.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Spatie\WebhookClient\Tests\TestClasses;

use Illuminate\Http\Request;
use Spatie\WebhookClient\WebhookConfig;
use Spatie\WebhookClient\Models\WebhookCall;

class WebhookModelWithoutPayloadSaved extends WebhookCall
{
public static function storeWebhook(WebhookConfig $config, Request $request): WebhookCall
{
return WebhookCall::create([
'name' => $config->name,
'payload' => [],
]);
}
}
18 changes: 16 additions & 2 deletions tests/WebhookControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
use Illuminate\Support\Facades\Route;
use Spatie\WebhookClient\Models\WebhookCall;
use Spatie\WebhookClient\Events\InvalidSignatureEvent;
use Spatie\WebhookClient\Tests\TestClasses\EverythingIsValidSignatureValidator;
use Spatie\WebhookClient\Tests\TestClasses\NothingIsValidSignatureValidator;
use Spatie\WebhookClient\Tests\TestClasses\ProcessWebhookJobTestClass;
use Spatie\WebhookClient\Tests\TestClasses\ProcessNothingWebhookProfile;
use Spatie\WebhookClient\Tests\TestClasses\WebhookModelWithoutPayloadSaved;
use Spatie\WebhookClient\Tests\TestClasses\NothingIsValidSignatureValidator;
use Spatie\WebhookClient\Tests\TestClasses\EverythingIsValidSignatureValidator;

class WebhookControllerTest extends TestCase
{
Expand Down Expand Up @@ -124,6 +125,19 @@ public function it_can_work_with_an_alternative_config()
->assertSuccessful();
}

/** @test */
public function it_can_work_with_an_alternative_model()
{
config()->set('webhook-client.configs.0.webhook_model', WebhookModelWithoutPayloadSaved::class);

$this
->postJson('incoming-webhooks', $this->payload, $this->headers)
->assertSuccessful();

$this->assertCount(1, WebhookCall::get());
$this->assertEquals([], WebhookCall::first()->payload);
}

private function determineSignature(array $payload): string
{
$secret = config('webhook-client.configs.0.signing_secret');
Expand Down

0 comments on commit 5b2afed

Please sign in to comment.