Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PreferenceBasedNotification
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
abstract class PreferenceBasedNotification extends Notification
{
use Queueable;
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
final public function via($notifiable)
{
$viaChannels = [];
$preference = $this->getPreference($notifiable->user_id);
if (!$preference || $preference->receive_email) {
$viaChannels[] = 'mail';
}
if (!$preference || $preference->receive_notification) {
$viaChannels[] = 'broadcast';
}
return $viaChannels;
}
final private function getPreference($userId)
{
$currentClass = \Arr::last(explode('\\', get_class($this)));
$event = \DB::table('notification_events')
->where('notification_class', $currentClass)->first();
if (!$event) {
return null;
}
return DB::table('user_notification_preferences')
->where('user_id', $userId)
->where('notification_event_id', $event->id)
->first();
}
abstract public function toMail($notifiable);
abstract public function toBroadcast($notifiable);
abstract public function getDependentNetworkID();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment