From b9b32922c9e38c35a500aea37ec6f07d42b76338 Mon Sep 17 00:00:00 2001 From: MammutAlex Date: Fri, 28 Jun 2024 14:02:42 +0300 Subject: [PATCH 1/4] Add mentod to WebhookMessage --- README.md | 1 + src/WebhookChannel.php | 2 +- src/WebhookMessage.php | 23 +++++++++++++++++++++++ tests/ChannelTest.php | 11 +++++++---- tests/MessageTest.php | 8 ++++++++ 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3fd40a3..5a3d580 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,7 @@ public function routeNotificationForWebhook() - `userAgent('')`: Accepts a string value for the Webhook user agent. - `header($name, $value)`: Sets additional headers to send with the POST Webhook. - `verify()`: Enable the SSL certificate verification or provide the path to a CA bundle +- `method('')`: Set the HTTP method to use for the Webhook request. Defaults to `POST`. ## Changelog diff --git a/src/WebhookChannel.php b/src/WebhookChannel.php index 3785ccd..5a27b8b 100644 --- a/src/WebhookChannel.php +++ b/src/WebhookChannel.php @@ -38,7 +38,7 @@ public function send($notifiable, Notification $notification) $webhookData = $notification->toWebhook($notifiable)->toArray(); - $response = $this->client->post($url, [ + $response = $this->client->request($webhookData['method'], $url, [ 'query' => Arr::get($webhookData, 'query'), 'body' => json_encode(Arr::get($webhookData, 'data')), 'verify' => Arr::get($webhookData, 'verify'), diff --git a/src/WebhookMessage.php b/src/WebhookMessage.php index 63c3367..873e538 100644 --- a/src/WebhookMessage.php +++ b/src/WebhookMessage.php @@ -39,6 +39,13 @@ class WebhookMessage */ protected $verify = false; + /** + * The Guzzle method option. + * + * @var string + */ + protected $method = 'POST'; + /** * @param mixed $data * @@ -85,6 +92,21 @@ public function data($data) return $this; } + + /** + * Set the Webhook method + * + * @param mixed $data + * + * @return $this + */ + public function method($method) + { + $this->method = $method; + + return $this; + } + /** * Add a Webhook request custom header. * @@ -136,6 +158,7 @@ public function toArray() 'data' => $this->data, 'headers' => $this->headers, 'verify' => $this->verify, + 'method' => $this->method, ]; } } diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index 6e57427..50bd3ee 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -18,9 +18,10 @@ public function it_can_send_a_notification() { $response = new Response(200); $client = Mockery::mock(Client::class); - $client->shouldReceive('post') + $client->shouldReceive('request') ->once() ->with( + 'POST', 'https://notifiable-webhook-url.com', [ 'query' => null, @@ -42,9 +43,10 @@ public function it_can_send_a_notification_with_2xx_status() { $response = new Response(201); $client = Mockery::mock(Client::class); - $client->shouldReceive('post') + $client->shouldReceive('request') ->once() ->with( + 'POST', 'https://notifiable-webhook-url.com', [ 'query' => null, @@ -66,9 +68,10 @@ public function it_can_send_a_notification_with_query_string() { $response = new Response(); $client = Mockery::mock(Client::class); - $client->shouldReceive('post') + $client->shouldReceive('request') ->once() ->with( + 'POST', 'https://notifiable-webhook-url.com', [ 'query' => [ @@ -100,7 +103,7 @@ public function it_throws_an_exception_when_it_could_not_send_the_notification() $this->expectExceptionCode(500); $client = Mockery::mock(Client::class); - $client->shouldReceive('post') + $client->shouldReceive('request') ->once() ->andReturn($response); $channel = new WebhookChannel($client); diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 3e5a800..ad5897f 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -67,4 +67,12 @@ public function it_can_verify_the_request() $this->message->verify(); $this->assertEquals(true, Arr::get($this->message->toArray(), 'verify')); } + + /** @test */ + public function it_can_set_the_method_to_get() + { + $webhookMessage = new WebhookMessage(); + $webhookMessage->method('GET'); + $this->assertEquals('GET', $webhookMessage->toArray()['method']); + } } From 11cac8e5d4e0f2190df9f2c172a8b51e7fbf86c1 Mon Sep 17 00:00:00 2001 From: MammutAlex Date: Fri, 28 Jun 2024 14:15:26 +0300 Subject: [PATCH 2/4] Fix codestyle --- src/WebhookChannel.php | 6 +++--- src/WebhookMessage.php | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/WebhookChannel.php b/src/WebhookChannel.php index 5a27b8b..1af2f50 100644 --- a/src/WebhookChannel.php +++ b/src/WebhookChannel.php @@ -13,7 +13,7 @@ class WebhookChannel protected $client; /** - * @param Client $client + * @param Client $client */ public function __construct(Client $client) { @@ -23,8 +23,8 @@ public function __construct(Client $client) /** * Send the given notification. * - * @param mixed $notifiable - * @param \Illuminate\Notifications\Notification $notification + * @param mixed $notifiable + * @param \Illuminate\Notifications\Notification $notification * * @return \GuzzleHttp\Psr7\Response * diff --git a/src/WebhookMessage.php b/src/WebhookMessage.php index 873e538..0393b92 100644 --- a/src/WebhookMessage.php +++ b/src/WebhookMessage.php @@ -47,7 +47,7 @@ class WebhookMessage protected $method = 'POST'; /** - * @param mixed $data + * @param mixed $data * * @return static */ @@ -57,7 +57,7 @@ public static function create($data = '') } /** - * @param mixed $data + * @param mixed $data */ public function __construct($data = '') { @@ -67,7 +67,7 @@ public function __construct($data = '') /** * Set the Webhook parameters to be URL encoded. * - * @param mixed $query + * @param mixed $query * * @return $this */ @@ -81,7 +81,7 @@ public function query($query) /** * Set the Webhook data to be JSON encoded. * - * @param mixed $data + * @param mixed $data * * @return $this */ @@ -96,7 +96,7 @@ public function data($data) /** * Set the Webhook method * - * @param mixed $data + * @param mixed $data * * @return $this */ @@ -110,8 +110,8 @@ public function method($method) /** * Add a Webhook request custom header. * - * @param string $name - * @param string $value + * @param string $name + * @param string $value * * @return $this */ @@ -125,7 +125,7 @@ public function header($name, $value) /** * Set the Webhook request UserAgent. * - * @param string $userAgent + * @param string $userAgent * * @return $this */ From 68e0521ec752c6bc724b389aa089047bbc48494b Mon Sep 17 00:00:00 2001 From: MammutAlex Date: Fri, 28 Jun 2024 14:17:21 +0300 Subject: [PATCH 3/4] Fix codestyle --- src/WebhookChannel.php | 1 - src/WebhookMessage.php | 9 +-------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/WebhookChannel.php b/src/WebhookChannel.php index 1af2f50..869b8c1 100644 --- a/src/WebhookChannel.php +++ b/src/WebhookChannel.php @@ -25,7 +25,6 @@ public function __construct(Client $client) * * @param mixed $notifiable * @param \Illuminate\Notifications\Notification $notification - * * @return \GuzzleHttp\Psr7\Response * * @throws \NotificationChannels\Webhook\Exceptions\CouldNotSendNotification diff --git a/src/WebhookMessage.php b/src/WebhookMessage.php index 0393b92..89dcf89 100644 --- a/src/WebhookMessage.php +++ b/src/WebhookMessage.php @@ -48,7 +48,6 @@ class WebhookMessage /** * @param mixed $data - * * @return static */ public static function create($data = '') @@ -68,7 +67,6 @@ public function __construct($data = '') * Set the Webhook parameters to be URL encoded. * * @param mixed $query - * * @return $this */ public function query($query) @@ -82,7 +80,6 @@ public function query($query) * Set the Webhook data to be JSON encoded. * * @param mixed $data - * * @return $this */ public function data($data) @@ -94,10 +91,9 @@ public function data($data) /** - * Set the Webhook method + * Set the Webhook method. * * @param mixed $data - * * @return $this */ public function method($method) @@ -112,7 +108,6 @@ public function method($method) * * @param string $name * @param string $value - * * @return $this */ public function header($name, $value) @@ -126,7 +121,6 @@ public function header($name, $value) * Set the Webhook request UserAgent. * * @param string $userAgent - * * @return $this */ public function userAgent($userAgent) @@ -138,7 +132,6 @@ public function userAgent($userAgent) /** * Indicate that the request should be verified. - * * @return $this */ public function verify($value = true) From 8ba2218619827bcca223fe5b264d8ad72bd81164 Mon Sep 17 00:00:00 2001 From: MammutAlex Date: Fri, 28 Jun 2024 14:18:12 +0300 Subject: [PATCH 4/4] Fix codestyle --- src/WebhookMessage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WebhookMessage.php b/src/WebhookMessage.php index 89dcf89..6b1e390 100644 --- a/src/WebhookMessage.php +++ b/src/WebhookMessage.php @@ -89,7 +89,6 @@ public function data($data) return $this; } - /** * Set the Webhook method. * @@ -132,6 +131,7 @@ public function userAgent($userAgent) /** * Indicate that the request should be verified. + * * @return $this */ public function verify($value = true)