-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from JustSteveKing/feature/webhook-notification
Creating a webhook channel
- Loading branch information
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JustSteveKing\Webhooks\Channels; | ||
|
||
use Illuminate\Http\Client\Response; | ||
use Illuminate\Notifications\Notification; | ||
use JustSteveKing\Webhooks\Builder\PendingWebhook; | ||
|
||
final class WebhookChannel | ||
{ | ||
/** | ||
* @param Notification $notification | ||
* @return Response|null | ||
*/ | ||
public function send(mixed $notifiable, Notification $notification): null|Response | ||
{ | ||
/** | ||
* @var PendingWebhook $webhook | ||
*/ | ||
$webhook = $notification->toWebhook($notifiable); | ||
|
||
return $webhook->send(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Http\Client\Response; | ||
use Illuminate\Support\Facades\Http; | ||
use JustSteveKing\Webhooks\Channels\WebhookChannel; | ||
use JustSteveKing\Webhooks\Tests\Stubs\TestNotifiable; | ||
use JustSteveKing\Webhooks\Tests\Stubs\TestNotification; | ||
|
||
it('can send a notification', function (): void { | ||
Http::fake(); | ||
|
||
$channel = new WebhookChannel(); | ||
|
||
$channel->send(new TestNotifiable(), new TestNotification()); | ||
|
||
Http::assertSentCount(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JustSteveKing\Webhooks\Tests\Stubs; | ||
|
||
use Illuminate\Notifications\Notifiable; | ||
|
||
final class TestNotifiable | ||
{ | ||
use Notifiable; | ||
|
||
public function routeNotificationForWebhook(): string | ||
{ | ||
return 'https://some-url.com/'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JustSteveKing\Webhooks\Tests\Stubs; | ||
|
||
use Illuminate\Notifications\Notification; | ||
use JustSteveKing\Webhooks\Builder\PendingWebhook; | ||
use JustSteveKing\Webhooks\Facades\Webhook; | ||
|
||
final class TestNotification extends Notification | ||
{ | ||
public function toWebhook(mixed $notifiable): PendingWebhook | ||
{ | ||
return Webhook::to( | ||
url: 'https://www.some-url.com/', | ||
)->with( | ||
payload: [], | ||
); | ||
} | ||
} |