Skip to content

Commit e42925a

Browse files
authored
Merge pull request #41 from wayofdev/feat/support-multiple-http-methods
2 parents dbbae5c + 9983a30 commit e42925a

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/Bridge/Laravel/Providers/WebhookClientServiceProvider.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
use WayOfDev\WebhookClient\ConfigRepository;
1616
use WayOfDev\WebhookClient\Contracts\WebhookCallRepository;
1717
use WayOfDev\WebhookClient\Exceptions\InvalidConfig;
18+
use WayOfDev\WebhookClient\Exceptions\InvalidMethod;
1819
use WayOfDev\WebhookClient\Persistence\ORMWebhookCallRepository;
1920

21+
use function in_array;
2022
use function is_null;
2123

2224
final class WebhookClientServiceProvider extends ServiceProvider
@@ -34,8 +36,13 @@ public function boot(): void
3436

3537
public function register(): void
3638
{
37-
Route::macro('webhooks', function (string $url, string $name = 'default') {
38-
return Route::post($url, WebhookController::class)->name("webhook-client-{$name}");
39+
Route::macro('webhooks', function (string $url, string $name = 'default', $method = 'post') {
40+
if (! in_array($method, ['get', 'post', 'put', 'patch', 'delete'], true)) {
41+
throw InvalidMethod::make($method);
42+
}
43+
44+
// @phpstan-ignore-next-line
45+
return Route::{$method}($url, WebhookController::class)->name("webhook-client-{$name}");
3946
});
4047

4148
$this->app->scoped(ConfigRepository::class, function () {

src/Exceptions/InvalidMethod.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WayOfDev\WebhookClient\Exceptions;
6+
7+
use Exception;
8+
9+
final class InvalidMethod extends Exception
10+
{
11+
public static function make($method): self
12+
{
13+
return new self("The method $method is not allowed.");
14+
}
15+
}

0 commit comments

Comments
 (0)