Skip to content

Commit

Permalink
Allow sending notifications via slack.
Browse files Browse the repository at this point in the history
  • Loading branch information
binaryk committed Jun 30, 2021
1 parent 58ea99e commit 4459a87
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
17 changes: 15 additions & 2 deletions src/Notifications/Slack.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Binarcode\LaravelDeveloper\Notifications;

use App\Notifications\OrderPlacedNotification;
use Binarcode\LaravelDeveloper\Dtos\DevNotificationDto;
use Binarcode\LaravelDeveloper\Models\ExceptionLog;
use Binarcode\LaravelDeveloper\Telescope\TelescopeDev;
use Binarcode\LaravelDeveloper\Telescope\TelescopeException;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Notification as NotificationFacade;
use Throwable;
Expand Down Expand Up @@ -68,7 +70,7 @@ private function send($item)
if ($item instanceof Throwable) {
if ($this->persist) {
$dto = DevNotificationDto::makeFromExceptionLog(
tap(ExceptionLog::makeFromException($item), fn (ExceptionLog $log) => $log->save())
tap(ExceptionLog::makeFromException($item), fn(ExceptionLog $log) => $log->save())
);

if ($this->telescope && TelescopeDev::allow()) {
Expand All @@ -91,14 +93,20 @@ private function send($item)
$dto = $dto::makeFromExceptionLog($item);
}

if ($item instanceof Notification) {
NotificationFacade::route('slack', $this->guessChannel())->notify(
$item
);
}

$notification = new $class($dto);


if (is_callable($cb = static::$notifyUsing)) {
return call_user_func($cb, $notification);
}

NotificationFacade::route('slack', $this->channel ?? config('developer.slack_dev_hook'))->notify(
NotificationFacade::route('slack', $this->guessChannel())->notify(
$notification
);

Expand All @@ -116,4 +124,9 @@ public static function notifyUsing(?callable $notificator)
{
Slack::$notifyUsing = $notificator;
}

private function guessChannel(): ?string
{
return $this->channel ?? config('developer.slack_dev_hook');
}
}
32 changes: 32 additions & 0 deletions tests/Fixtures/DummyNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Binarcode\LaravelDeveloper\Tests\Fixtures;

use App\Domains\Discounts\Models\DiscountUses;
use App\Domains\Items\Models\Item;
use App\Domains\Orders\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;

class DummyNotification extends Notification implements ShouldQueue
{
use Queueable;


public function via($notifiable)
{
return ['slack'];
}

public function toSlack()
{
return (new SlackMessage())
->from('test')
->content("test")
->attachment(function ($att) {
$att->title("Title")->content("Content");
});
}
}
25 changes: 22 additions & 3 deletions tests/Helpers/SlackHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
use Binarcode\LaravelDeveloper\Models\ExceptionLog;
use Binarcode\LaravelDeveloper\Notifications\DevNotification;
use Binarcode\LaravelDeveloper\Notifications\Slack;
use Binarcode\LaravelDeveloper\Tests\Fixtures\DummyNotification;
use Binarcode\LaravelDeveloper\Tests\TestCase;
use Exception;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Facades\Notification;
use Illuminate\Notifications\Notification as NotificationBase;

class SlackHelperTest extends TestCase
{
Expand Down Expand Up @@ -48,8 +50,25 @@ public function test_slack_helper_can_send_throwable_to_slack()

$uuid = ExceptionLog::latest()->first()->id;

Notification::assertSentTo(new AnonymousNotifiable, DevNotification::class, function (DevNotification $class) use ($uuid) {
return $class->notificationDto->attachment_link === "app.test/{$uuid}";
});
Notification::assertSentTo(new AnonymousNotifiable, DevNotification::class,
function (DevNotification $class) use ($uuid) {
return $class->notificationDto->attachment_link === "app.test/{$uuid}";
});
}

public function test_slack_helper_can_send_notifications_to_slack()
{
Notification::fake();

config([
'developer.exception_log_base_url' => 'app.test/{id}',
'developer.slack_dev_hook' => 'https://test.com',
]);

$this->assertInstanceOf(Slack::class, slack(
new DummyNotification()
)->persist());

Notification::assertSentTo(new AnonymousNotifiable, DummyNotification::class);
}
}

0 comments on commit 4459a87

Please sign in to comment.