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.3] Granular notification queue jobs #15681

Merged
merged 1 commit into from
Sep 29, 2016
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
26 changes: 18 additions & 8 deletions src/Illuminate/Notifications/ChannelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function send($notifiables, $notification)
* @param mixed $notification
* @return void
*/
public function sendNow($notifiables, $notification)
public function sendNow($notifiables, $notification, array $channels = null)
{
if (! $notifiables instanceof Collection && ! is_array($notifiables)) {
$notifiables = [$notifiables];
Expand All @@ -61,7 +61,7 @@ public function sendNow($notifiables, $notification)
foreach ($notifiables as $notifiable) {
$notificationId = (string) Uuid::uuid4();

$channels = $notification->via($notifiable);
$channels = $channels ?: $notification->via($notifiable);

if (empty($channels)) {
continue;
Expand Down Expand Up @@ -109,12 +109,22 @@ protected function shouldSendNotification($notifiable, $notification, $channel)
*/
protected function queueNotification($notifiables, $notification)
{
$this->app->make(Bus::class)->dispatch(
(new SendQueuedNotifications($notifiables, $notification))
->onConnection($notification->connection)
->onQueue($notification->queue)
->delay($notification->delay)
);
if (! $notifiables instanceof Collection && ! is_array($notifiables)) {
$notifiables = [$notifiables];
}

$bus = $this->app->make(Bus::class);

foreach ($notifiables as $notifiable) {
foreach ($notification->via($notifiable) as $channel) {
$bus->dispatch(
(new SendQueuedNotifications($notifiables, $notification, [$channel]))
->onConnection($notification->connection)
->onQueue($notification->queue)
->delay($notification->delay)
);
}
}
}

/**
Expand Down
13 changes: 11 additions & 2 deletions src/Illuminate/Notifications/SendQueuedNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,24 @@ class SendQueuedNotifications implements ShouldQueue
*/
protected $notification;

/**
* All of the channels to send the notification too.
*
* @var array
*/
protected $channels = null;

/**
* Create a new job instance.
*
* @param \Illuminate\Support\Collection $notifiables
* @param \Illuminate\Notifications\Notification $notification
* @param array $channels
* @return void
*/
public function __construct($notifiables, $notification)
public function __construct($notifiables, $notification, array $channels = null)
{
$this->channels = $channels;
Copy link
Contributor

@reinink reinink Sep 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: Should you put this line after the other two, since it was the last item in the method signature?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I sort by length, signature order.

$this->notifiables = $notifiables;
$this->notification = $notification;
}
Expand All @@ -45,6 +54,6 @@ public function __construct($notifiables, $notification)
*/
public function handle(ChannelManager $manager)
{
$manager->sendNow($this->notifiables, $this->notification);
$manager->sendNow($this->notifiables, $this->notification, $this->channels);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function testNotificationsCanBeSent()
{
$job = new SendQueuedNotifications('notifiables', 'notification');
$manager = Mockery::mock('Illuminate\Notifications\ChannelManager');
$manager->shouldReceive('sendNow')->once()->with('notifiables', 'notification');
$manager->shouldReceive('sendNow')->once()->with('notifiables', 'notification', null);
$job->handle($manager);
}
}