Created
April 19, 2021 01:38
-
-
Save rishpandey/2689c481cc9e3209223cbf2e47d17449 to your computer and use it in GitHub Desktop.
PreferenceBasedNotification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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