Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] An easy way to route notifications on the go #19998

Merged
merged 3 commits into from
Jul 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/Illuminate/Notifications/InstantNotifiable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Illuminate\Notifications;

use Illuminate\Contracts\Notifications\Dispatcher;

class InstantNotifiable
{
/**
* The array of routes a notification should be sent to.
*
* @var array
*/
public $routes;

/**
* Send the given notification.
*
* @param mixed $instance
* @return void
*/
public function notify($instance)
{
app(Dispatcher::class)->send($this, $instance);
}

/**
* Get the notification routing information for the given driver.
*
* @param string $driver
* @return mixed
*/
public function routeNotificationFor($driver)
{
return $this->routes[$driver] ?? null;
}

/**
* Register the given route.
*
* @param mixed $route
* @param string $channel
* @return $this
*/
public function addRoute($route, $channel)
{
$this->routes[$channel] = $route;

return $this;
}
}
2 changes: 1 addition & 1 deletion src/Illuminate/Notifications/NotificationSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ protected function queueNotification($notifiables, $notification)
$notification->id = $notificationId;

$this->bus->dispatch(
(new SendQueuedNotifications($this->formatNotifiables($notifiable), $notification, [$channel]))
(new SendQueuedNotifications($notifiable, $notification, [$channel]))
->onConnection($notification->connection)
->onQueue($notification->queue)
->delay($notification->delay)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

use Orchestra\Testbench\TestCase;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\InstantNotifiable;
use Illuminate\Support\Facades\Notification as NotificationFacade;

/**
* @group integration
*/
class SendingNotificationsViaInstantNotifiableTest extends TestCase
{
public $mailer;

protected function getEnvironmentSetUp($app)
{
$app['config']->set('app.debug', 'true');
}

public function test_mail_is_sent()
{
$notifiable = (new InstantNotifiable())
->addRoute('enzo', 'testchannel')
->addRoute('[email protected]', 'anothertestchannel');

NotificationFacade::send(
$notifiable,
new TestMailNotificationForInstantNotifiable()
);

$this->assertEquals([
'enzo', '[email protected]',
], $_SERVER['__notifiable.route']);
}
}

class TestMailNotificationForInstantNotifiable extends Notification
{
public function via($notifiable)
{
return [TestCustomChannel::class, AnotherTestCustomChannel::class];
}
}

class TestCustomChannel
{
public function send($notifiable, $notification)
{
$_SERVER['__notifiable.route'][] = $notifiable->routeNotificationFor('testchannel');
}
}

class AnotherTestCustomChannel
{
public function send($notifiable, $notification)
{
$_SERVER['__notifiable.route'][] = $notifiable->routeNotificationFor('anothertestchannel');
}
}