Skip to content

Commit

Permalink
Refactor notification component.
Browse files Browse the repository at this point in the history
Mainly just cleaning. Extraacting some methods and classes.
  • Loading branch information
taylorotwell committed Dec 20, 2016
1 parent 4d8090b commit 5f93133
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 117 deletions.
110 changes: 8 additions & 102 deletions src/Illuminate/Notifications/ChannelManager.php
Expand Up @@ -11,6 +11,7 @@
use GuzzleHttp\Client as HttpClient;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Bus\Dispatcher as Bus;
use Nexmo\Client\Credentials\Basic as NexmoCredentials;
use Illuminate\Database\Eloquent\Collection as ModelCollection;
Expand All @@ -35,119 +36,24 @@ class ChannelManager extends Manager implements DispatcherContract, FactoryContr
*/
public function send($notifiables, $notification)
{
$notifiables = $this->formatNotifiables($notifiables);

if ($notification instanceof ShouldQueue) {
return $this->queueNotification($notifiables, $notification);
}

return $this->sendNow($notifiables, $notification);
return (new NotificationSender(
$this, $this->app->make(Bus::class), $this->app->make(Dispatcher::class))
)->send($notifiables, $notification);
}

/**
* Send the given notification immediately.
*
* @param \Illuminate\Support\Collection|array|mixed $notifiables
* @param mixed $notification
* @param array|null $channels
* @return void
*/
public function sendNow($notifiables, $notification, array $channels = null)
{
$notifiables = $this->formatNotifiables($notifiables);

$original = clone $notification;

foreach ($notifiables as $notifiable) {
$notificationId = Uuid::uuid4()->toString();

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

if (empty($channels)) {
continue;
}

foreach ($channels as $channel) {
$notification = clone $original;

if (! $notification->id) {
$notification->id = $notificationId;
}

if (! $this->shouldSendNotification($notifiable, $notification, $channel)) {
continue;
}

$response = $this->driver($channel)->send($notifiable, $notification);

$this->app->make('events')->fire(
new Events\NotificationSent($notifiable, $notification, $channel, $response)
);
}
}
}

/**
* Determines if the notification can be sent.
*
* @param mixed $notifiable
* @param mixed $notification
* @param string $channel
* @return bool
*/
protected function shouldSendNotification($notifiable, $notification, $channel)
{
return $this->app->make('events')->until(
new Events\NotificationSending($notifiable, $notification, $channel)
) !== false;
}

/**
* Queue the given notification instances.
*
* @param mixed $notifiables
* @param array[\Illuminate\Notifications\Channels\Notification] $notification
* @return void
*/
protected function queueNotification($notifiables, $notification)
{
$notifiables = $this->formatNotifiables($notifiables);

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

$original = clone $notification;

foreach ($notifiables as $notifiable) {
$notificationId = Uuid::uuid4()->toString();

foreach ($notification->via($notifiable) as $channel) {
$notification = clone $original;

$notification->id = $notificationId;

$bus->dispatch(
(new SendQueuedNotifications($this->formatNotifiables($notifiable), $notification, [$channel]))
->onConnection($notification->connection)
->onQueue($notification->queue)
->delay($notification->delay)
);
}
}
}

/**
* Format the notifiables into a Collection / array if necessary.
*
* @param mixed $notifiables
* @return ModelCollection|array
*/
protected function formatNotifiables($notifiables)
{
if (! $notifiables instanceof Collection && ! is_array($notifiables)) {
return $notifiables instanceof Model
? new ModelCollection([$notifiables]) : [$notifiables];
}

return $notifiables;
return (new NotificationSender(
$this, $this->app->make(Bus::class), $this->app->make(Dispatcher::class))
)->sendNow($notifiables, $notification, $channels);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Illuminate/Notifications/Channels/DatabaseChannel.php
Expand Up @@ -36,10 +36,11 @@ public function send($notifiable, Notification $notification)
protected function getData($notifiable, Notification $notification)
{
if (method_exists($notification, 'toDatabase')) {
$data = $notification->toDatabase($notifiable);
return is_array($data = $notification->toDatabase($notifiable))
? $data : $data->data;
}

return is_array($data) ? $data : $data->data;
} elseif (method_exists($notification, 'toArray')) {
if (method_exists($notification, 'toArray')) {
return $notification->toArray($notifiable);
}

Expand Down
37 changes: 30 additions & 7 deletions src/Illuminate/Notifications/Channels/MailChannel.php
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Notifications\Channels;

use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Contracts\Mail\Mailable;
Expand Down Expand Up @@ -113,13 +114,9 @@ protected function buildMessage($mailMessage, $notifiable, $notification, $messa
*/
protected function addressMessage($mailMessage, $notifiable, $message)
{
$recipients = empty($message->to) ? $notifiable->routeNotificationFor('mail') : $message->to;
$this->addSender($mailMessage, $message);

if (! empty($message->from)) {
$mailMessage->from($message->from[0], isset($message->from[1]) ? $message->from[1] : null);
}

if (is_array($recipients)) {
if (is_array($recipients = $this->getRecipients($notifiable, $message))) {
$mailMessage->bcc($recipients);
} else {
$mailMessage->to($recipients);
Expand All @@ -128,12 +125,38 @@ protected function addressMessage($mailMessage, $notifiable, $message)
if ($message->cc) {
$mailMessage->cc($message->cc);
}
}

/**
* Add the "from" and "reply to" addresses to the message.
*
* @param \Illuminate\Mail\Message $mailMessage
* @param \Illuminate\Notifications\Messages\MailMessage $message
* @return void
*/
protected function addSender($mailMessage, $message)
{
if (! empty($message->from)) {
$mailMessage->from($message->from[0], Arr::get($message->from, 1));
}

if (! empty($message->replyTo)) {
$mailMessage->replyTo($message->replyTo[0], isset($message->replyTo[1]) ? $message->replyTo[1] : null);
$mailMessage->replyTo($message->replyTo[0], Arr::get($message->replyTo, 1));
}
}

/**
* Get the recipients of the given message.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Messages\MailMessage $message
* @return mixed
*/
protected function getRecipients($notifiable, $message)
{
return empty($message->to) ? $notifiable->routeNotificationFor('mail') : $message->to;
}

/**
* Add the attachments to the message.
*
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Notifications/Channels/SlackWebhookChannel.php
Expand Up @@ -41,9 +41,9 @@ public function send($notifiable, Notification $notification)
return;
}

$message = $notification->toSlack($notifiable);

$this->http->post($url, $this->buildJsonPayload($message));
$this->http->post($url, $this->buildJsonPayload(
$notification->toSlack($notifiable)
));
}

/**
Expand Down

0 comments on commit 5f93133

Please sign in to comment.