Skip to content

Commit

Permalink
Merge branch 'allowBroadcastingNotificationNow' of https://github.com…
Browse files Browse the repository at this point in the history
…/themsaid/framework into themsaid-allowBroadcastingNotificationNow
  • Loading branch information
taylorotwell committed Dec 21, 2016
2 parents 30867d3 + c92a722 commit feac7dd
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/Illuminate/Notifications/Channels/BroadcastChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use RuntimeException;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Events\BroadcastNotificationCreated;

class BroadcastChannel
Expand Down Expand Up @@ -36,17 +37,25 @@ public function __construct(Dispatcher $events)
*/
public function send($notifiable, Notification $notification)
{
return $this->events->fire(new BroadcastNotificationCreated(
$notifiable, $notification, $this->getData($notifiable, $notification)
));
$message = $this->getData($notifiable, $notification);

$event = new BroadcastNotificationCreated(
$notifiable, $notification, is_array($message) ? $message : $message->data
);

if ($message instanceof BroadcastMessage) {
$event->connection = 'sync';
}

return $this->events->fire($event);
}

/**
* Get the data for the notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return array
* @return mixed
*
* @throws \RuntimeException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ class BroadcastNotificationCreated implements ShouldBroadcast
*/
public $notification;

/**
* The queue connection.
*
* @var string
*/
public $connection;

/**
* The notification data.
*
Expand Down
56 changes: 56 additions & 0 deletions src/Illuminate/Notifications/Messages/BroadcastMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Illuminate\Notifications\Messages;

class BroadcastMessage
{
/**
* The data for the notification.
*
* @var array
*/
public $data;

/**
* Determine if the message should be broadcasted immediately.
*
* @var array
*/
public $broadcastNow = false;

/**
* Create a new message instance.
*
* @param string $content
* @return void
*/
public function __construct(array $data)
{
$this->data = $data;
}

/**
* Set the message data.
*
* @param array $data
* @return $this
*/
public function data($data)
{
$this->data = $data;

return $this;
}

/**
* Broadcast this message immediately.
*
* @return $this
*/
public function now()
{
$this->broadcastNow = true;

return $this;
}
}
27 changes: 27 additions & 0 deletions tests/Notifications/NotificationBroadcastChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ public function testNotificationIsBroadcastedOnCustomChannels()

$this->assertEquals(new PrivateChannel('custom-channel'), $channels[0]);
}

public function testNotificationIsBroadcastedNow()
{
$notification = new TestNotificationBroadCastedNow;
$notification->id = 1;
$notifiable = Mockery::mock();

$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
$events->shouldReceive('fire')->once()->with(Mockery::on(function ($event) {
return $event->connection == 'sync';
}));
$channel = new BroadcastChannel($events);
$channel->send($notifiable, $notification);
}
}

class NotificationBroadcastChannelTestNotification extends Notification
Expand All @@ -59,3 +73,16 @@ public function broadcastOn()
return [new PrivateChannel('custom-channel')];
}
}

class TestNotificationBroadCastedNow extends Notification
{
public function toArray($notifiable)
{
return ['invoice_id' => 1];
}

public function toBroadcast()
{
return (new \Illuminate\Notifications\Messages\BroadcastMessage([]))->now();
}
}

0 comments on commit feac7dd

Please sign in to comment.