Skip to content

Commit

Permalink
Add config option to enable/disable unique token (#212)
Browse files Browse the repository at this point in the history
* Add config option to enable/disable unique token

* Update WebhookClientServiceProvider.php
  • Loading branch information
it-can authored Apr 25, 2024
1 parent 919d8f8 commit 393d212
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
5 changes: 5 additions & 0 deletions config/webhook-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@
* It deletes all records after 1 week. Set to null if no models should be deleted.
*/
'delete_after_days' => 30,

/*
* Should a unique token be added to the route name
*/
'add_unique_token_to_route_name' => false,
];
18 changes: 13 additions & 5 deletions src/WebhookClientServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ public function packageBooted()
{
Route::macro('webhooks', function (string $url, string $name = 'default', $method = 'post') {

if(! in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) {
if (! in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) {
throw InvalidMethod::make($method);
}

if (config('webhook-client.add_unique_token_to_route_name', false)) {
$name .= '.' . Str::random(8);
}

return Route::{$method}($url, '\Spatie\WebhookClient\Http\Controllers\WebhookController')
->name("webhook-client-{$name}." . Str::random(8));
->name("webhook-client-{$name}");
});

$this->app->scoped(WebhookConfigRepository::class, function () {
$configRepository = new WebhookConfigRepository();
$configRepository = new WebhookConfigRepository;

collect(config('webhook-client.configs'))
->map(fn (array $config) => new WebhookConfig($config))
Expand All @@ -44,9 +48,13 @@ public function packageBooted()
$this->app->bind(WebhookConfig::class, function () {
$routeName = Route::currentRouteName() ?? '';

$routeNameSuffix = Str::after($routeName, 'webhook-client-');
$configName = Str::after($routeName, 'webhook-client-');

$configName = Str::before($routeNameSuffix, '.');
if (config('webhook-client.add_unique_token_to_route_name', false)) {
$routeNameSuffix = Str::after($routeName, 'webhook-client-');

$configName = Str::before($routeNameSuffix, '.');
}

$webhookConfig = app(WebhookConfigRepository::class)->getConfig($configName);

Expand Down
2 changes: 2 additions & 0 deletions tests/WebhookControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ public function it_can_store_none_of_the_headers()
/** @test */
public function multiple_routes_can_share_configuration()
{
config()->set('webhook-client.add_unique_token_to_route_name', true);

Route::webhooks('incoming-webhooks-additional');

$this->refreshWebhookConfigRepository();
Expand Down

0 comments on commit 393d212

Please sign in to comment.