Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility the change how a WebookCall model is stored #15

Merged
merged 3 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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