Skip to content

Commit

Permalink
add events and add fallback interest name
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid committed Aug 10, 2016
1 parent 75289a4 commit a5f38b5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/Events/MessageSending.php
@@ -0,0 +1,30 @@
<?php

namespace NotificationChannels\PusherPushNotifications\Events;

use Illuminate\Notifications\Notification;

class MessageSending
{
/**
* @var
*/
private $notifiable;

/**
* @var \Illuminate\Notifications\Notification
*/
private $notification;

/**
* MessageSending constructor.
*
* @param $notifiable
* @param \Illuminate\Notifications\Notification $notification
*/
public function __construct($notifiable, Notification $notification)
{
$this->notifiable = $notifiable;
$this->notification = $notification;
}
}
30 changes: 30 additions & 0 deletions src/Events/MessageSent.php
@@ -0,0 +1,30 @@
<?php

namespace NotificationChannels\PusherPushNotifications\Events;

use Illuminate\Notifications\Notification;

class MessageSent
{
/**
* @var
*/
private $notifiable;

/**
* @var \Illuminate\Notifications\Notification
*/
private $notification;

/**
* MessageSending constructor.
*
* @param $notifiable
* @param \Illuminate\Notifications\Notification $notification
*/
public function __construct($notifiable, Notification $notification)
{
$this->notifiable = $notifiable;
$this->notification = $notification;
}
}
27 changes: 26 additions & 1 deletion src/PushNotificationsChannel.php
Expand Up @@ -2,6 +2,8 @@

namespace NotificationChannels\PusherPushNotifications;

use NotificationChannels\PusherPushNotifications\Events\MessageSending;
use NotificationChannels\PusherPushNotifications\Events\MessageSent;
use Illuminate\Notifications\Notification;
use Pusher;

Expand Down Expand Up @@ -37,9 +39,32 @@ public function __construct()
*/
public function send($notifiable, Notification $notification)
{
$interest = $notifiable->routeNotificationFor('PusherPushNotifications')
?: $this->interestName($notifiable);

if (event(new MessageSending($notifiable, $notification), [], true) === false) {
return;
}

$this->pusher->notify(
$notifiable->routeNotificationFor('PusherPushNotifications'),
$interest,
$notification->toPushNotification($notifiable)->toArray()
);

event(
new MessageSent($notifiable, $notification)
);
}

/**
* Get the interest name for the notifiable.
*
* @return string
*/
protected function interestName($notifiable)
{
$class = str_replace('\\', '.', get_class($notifiable));

return $class.'.'.$notifiable->getKey();
}
}

0 comments on commit a5f38b5

Please sign in to comment.