Skip to content

Commit

Permalink
work on channel speific messages
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Aug 8, 2016
1 parent 3e60b28 commit dce28b4
Show file tree
Hide file tree
Showing 20 changed files with 186 additions and 129 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -27,6 +27,7 @@
"nesbot/carbon": "~1.20",
"paragonie/random_compat": "~1.4|~2.0",
"psy/psysh": "0.7.*",
"ramsey/uuid": "~3.0",
"swiftmailer/swiftmailer": "~5.1",
"symfony/console": "3.1.*",
"symfony/debug": "3.1.*",
Expand Down
10 changes: 6 additions & 4 deletions src/Illuminate/Auth/Notifications/ResetPassword.php
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Auth\Notifications;

use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;

class ResetPassword extends Notification
{
Expand Down Expand Up @@ -36,13 +37,14 @@ public function via($notifiable)
}

/**
* Build the notification message.
* Build the mail representation of the notification.
*
* @return \Illuminate\Notifications\Message
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function message()
public function toMail()
{
return $this->line([
return (new MailMessage)
->line([
'You are receiving this email because we received a password reset request for your account.',
'Click the button below to reset your password:',
])
Expand Down
25 changes: 21 additions & 4 deletions src/Illuminate/Foundation/Console/stubs/notification.stub
Expand Up @@ -5,6 +5,9 @@ namespace DummyNamespace;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\DatabaseMessage;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class DummyClass extends Notification
{
Expand Down Expand Up @@ -32,15 +35,29 @@ class DummyClass extends Notification
}

/**
* Get the notification message.
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Message
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function message($notifiable)
public function toMail($notifiable)
{
return $this->line('The introduction to the notification.')
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', 'https://laravel.com')
->line('Thank you for using our application!');
}

/**
* Get the database representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\DatabaseMessage
*/
public function toDatabase($notifiable)
{
return new DatabaseMessage([
//
]);
}
}
46 changes: 0 additions & 46 deletions src/Illuminate/Notifications/Channels/BroadcastChannel.php

This file was deleted.

38 changes: 31 additions & 7 deletions src/Illuminate/Notifications/Channels/DatabaseChannel.php
Expand Up @@ -3,9 +3,30 @@
namespace Illuminate\Notifications\Channels;

use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Notifications\Events\DatabaseNotificationCreated;

class DatabaseChannel
{
/**
* The event dispatcher.
*
* @var \Illuminate\Contracts\Events\Dispatcher
*/
protected $events;

/**
* Create a new database channel.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @return void
*/
public function __construct(Dispatcher $events)
{
$this->events = $events;
}

/**
* Send the given notification.
*
Expand All @@ -16,7 +37,13 @@ class DatabaseChannel
public function send($notifiables, Notification $notification)
{
foreach ($notifiables as $notifiable) {
$this->createNotification($notifiable, $notification);
$databaseNotification = $this->createNotification($notifiable, $notification);

if ($notification instanceof ShouldBroadcast) {
$this->events->fire(new DatabaseNotificationCreated(
$notifiable, $notification, $databaseNotification
));
}
}
}

Expand All @@ -29,15 +56,12 @@ public function send($notifiables, Notification $notification)
*/
protected function createNotification($notifiable, Notification $notification)
{
$message = $notification->message($notifiable);
$message = $notification->toDatabase($notifiable);

return $notifiable->routeNotificationFor('database')->create([
'id' => $message->id,
'type' => get_class($notification),
'level' => $message->level,
'intro' => $message->introLines,
'outro' => $message->outroLines,
'action_text' => $message->actionText,
'action_url' => $message->actionUrl,
'data' => $message->data,
'read' => false,
]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Notifications/Channels/MailChannel.php
Expand Up @@ -40,7 +40,7 @@ public function send($notifiables, Notification $notification)
continue;
}

$message = $notification->asMail($notifiable);
$message = $notification->toMail($notifiable);

$this->mailer->send($message->view, $message->toArray(), function ($m) use ($notifiable, $notification, $message) {
$m->to($notifiable->routeNotificationFor('mail'));
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Notifications/Channels/NexmoSmsChannel.php
Expand Up @@ -65,7 +65,7 @@ public function send($notifiables, Notification $notification)
*/
protected function formatNotification($notifiable, $notification)
{
$message = $notification->message($notifiable);
$message = $notification->toNexmo($notifiable);

$actionText = $message->actionText
? $message->actionText.': ' : '';
Expand Down
8 changes: 4 additions & 4 deletions src/Illuminate/Notifications/Channels/SlackWebhookChannel.php
Expand Up @@ -3,8 +3,8 @@
namespace Illuminate\Notifications\Channels;

use GuzzleHttp\Client as HttpClient;
use Illuminate\Notifications\Message;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\SlackMessage;

class SlackWebhookChannel
{
Expand Down Expand Up @@ -40,7 +40,7 @@ public function send($notifiables, Notification $notification)
continue;
}

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

$this->http->post($url, [
'json' => [
Expand All @@ -60,10 +60,10 @@ public function send($notifiables, Notification $notification)
/**
* Format the given notification message.
*
* @param \Illuminate\Notifications\Message $message
* @param \Illuminate\Notifications\Messages\SlackMessage $message
* @return string
*/
protected function format(Message $message)
protected function format(SlackMessage $message)
{
$text = trim(implode(PHP_EOL.PHP_EOL, $message->introLines));

Expand Down
10 changes: 8 additions & 2 deletions src/Illuminate/Notifications/DatabaseNotification.php
Expand Up @@ -6,6 +6,13 @@

class DatabaseNotification extends Model
{
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;

/**
* The table associated with the model.
*
Expand All @@ -26,8 +33,7 @@ class DatabaseNotification extends Model
* @var array
*/
protected $casts = [
'intro' => 'array',
'outro' => 'array',
'data' => 'array',
'read' => 'boolean',
];

Expand Down
Expand Up @@ -55,7 +55,11 @@ public function __construct($notifiable, $notification, $databaseNotification)
*/
public function broadcastOn()
{
return [new PrivateChannel($this->channelName())];
if (! empty($channels = $this->notification->broadcastOn())) {
return $channels;
} else {
return [new PrivateChannel($this->channelName())];
}
}

/**
Expand Down
36 changes: 36 additions & 0 deletions src/Illuminate/Notifications/Messages/DatabaseMessage.php
@@ -0,0 +1,36 @@
<?php

namespace Illuminate\Notifications\Messages;

use Ramsey\Uuid\Uuid;
use Illuminate\Support\Arr;

class DatabaseMessage
{
/**
* The message's unique identifier.
*
* @var string
*/
public $id;

/**
* The data that should be stored with the notification.
*
* @var array
*/
public $data = [];

/**
* Create a new database message.
*
* @param array $data
* @return void
*/
public function __construct(array $data = [])
{
$this->id = Arr::pull($data, 'id') ?: Uuid::uuid4();

$this->data = $data;
}
}
8 changes: 8 additions & 0 deletions src/Illuminate/Notifications/Messages/NexmoMessage.php
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Notifications\Messages;

class NexmoMessage extends SimpleMessage
{
//
}
8 changes: 8 additions & 0 deletions src/Illuminate/Notifications/Messages/SlackMessage.php
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Notifications\Messages;

class SlackMessage extends SimpleMessage
{
//
}
10 changes: 10 additions & 0 deletions src/Illuminate/Notifications/Notification.php
Expand Up @@ -8,4 +8,14 @@
class Notification
{
use SerializesModels;

/**
* Get the channels the event should broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return [];
}
}
43 changes: 0 additions & 43 deletions tests/Notifications/NotificationBroadcastChannelTest.php

This file was deleted.

0 comments on commit dce28b4

Please sign in to comment.