diff --git a/config/notification-subscriptions.php b/config/notification-subscriptions.php index 03a5d8c..3a10731 100644 --- a/config/notification-subscriptions.php +++ b/config/notification-subscriptions.php @@ -1,7 +1,7 @@ ['database'], + + 'excluded_channels' => ['database'], ]; diff --git a/src/Listeners/NotificationSendingListener.php b/src/Listeners/NotificationSendingListener.php index 3b887c2..e758e73 100644 --- a/src/Listeners/NotificationSendingListener.php +++ b/src/Listeners/NotificationSendingListener.php @@ -33,8 +33,10 @@ public function handle(NotificationSending $event) $subscribed = $event->notifiable->isSubscribed(get_class($event->notification), $event->channel, $model, $optin); - if (! $subscribed) return false; + if (! $subscribed) { + return false; + } return $event; } -} \ No newline at end of file +} diff --git a/src/Models/NotificationSubscription.php b/src/Models/NotificationSubscription.php index 3d11442..b8799bf 100644 --- a/src/Models/NotificationSubscription.php +++ b/src/Models/NotificationSubscription.php @@ -1,4 +1,4 @@ -forceFill(['unsubscribed_at' => null])->save(); } - } diff --git a/src/NotificationSubscriptionsServiceProvider.php b/src/NotificationSubscriptionsServiceProvider.php index c68379a..0ffcac8 100644 --- a/src/NotificationSubscriptionsServiceProvider.php +++ b/src/NotificationSubscriptionsServiceProvider.php @@ -18,9 +18,10 @@ public function boot() public function register() { $this->mergeConfigFrom( - __DIR__.'/../config/notification-subscriptions.php', 'notification-subscriptions' + __DIR__.'/../config/notification-subscriptions.php', + 'notification-subscriptions' ); - $this->app->register(\LiranCo\NotificationSubscriptions\Providers\EventServiceProvider::class); + $this->app->register(\LiranCo\NotificationSubscriptions\Providers\EventServiceProvider::class); } } diff --git a/src/Providers/EventServiceProvider.php b/src/Providers/EventServiceProvider.php index ba01a82..da69b12 100644 --- a/src/Providers/EventServiceProvider.php +++ b/src/Providers/EventServiceProvider.php @@ -6,10 +6,10 @@ class EventServiceProvider extends ServiceProvider { - protected $listen = [ - 'Illuminate\Notifications\Events\NotificationSending' => [ - 'LiranCo\NotificationSubscriptions\Listeners\NotificationSendingListener', - ], + protected $listen = [ + 'Illuminate\Notifications\Events\NotificationSending' => [ + 'LiranCo\NotificationSubscriptions\Listeners\NotificationSendingListener', + ], ]; public function boot() diff --git a/src/Traits/HasNotificationSubscriptions.php b/src/Traits/HasNotificationSubscriptions.php index d27a20b..0b6162b 100644 --- a/src/Traits/HasNotificationSubscriptions.php +++ b/src/Traits/HasNotificationSubscriptions.php @@ -6,66 +6,64 @@ trait HasNotificationSubscriptions { - - public function notificationSubscriptions() - { - return $this->morphMany(NotificationSubscription::class, 'notifiable'); - } - - public function subscribe($type, $channel = '*', $model = null) - { - $subscription = $this->findSubscription($type, $channel, $model); - - if ($subscription) { - return $subscription->resubscribe(); - } - - return $this->createSubscription($type, $channel, $model); - } - - public function unsubscribe($type, $channel = '*', $model = null) - { - $subscription = $this->findSubscription($type, $channel, $model); - - if ($subscription) { - return $subscription->unsubscribe(); - } - - return $this->createSubscription($type, $channel, $model, true); - } - - public function findSubscription($type, $channel = '*', $model = null) - { - return $this->notificationSubscriptions()->where('type', $type)->where('channel', $channel)->model($model)->first(); - } - - public function createSubscription($type, $channel = '*', $model = null, $unsubscribe = false) - { - return $this->notificationSubscriptions()->create([ - 'type' => $type, - 'channel' => $channel, - 'model_type' => $model ? get_class($model) : null, - 'model_id' => optional($model)->id, - 'unsubscribed_at' => $unsubscribe ? $this->freshTimestamp() : null - ]); - } + public function notificationSubscriptions() + { + return $this->morphMany(NotificationSubscription::class, 'notifiable'); + } + + public function subscribe($type, $channel = '*', $model = null) + { + $subscription = $this->findSubscription($type, $channel, $model); + + if ($subscription) { + return $subscription->resubscribe(); + } + + return $this->createSubscription($type, $channel, $model); + } + + public function unsubscribe($type, $channel = '*', $model = null) + { + $subscription = $this->findSubscription($type, $channel, $model); + + if ($subscription) { + return $subscription->unsubscribe(); + } + + return $this->createSubscription($type, $channel, $model, true); + } + + public function findSubscription($type, $channel = '*', $model = null) + { + return $this->notificationSubscriptions()->where('type', $type)->where('channel', $channel)->model($model)->first(); + } + + public function createSubscription($type, $channel = '*', $model = null, $unsubscribe = false) + { + return $this->notificationSubscriptions()->create([ + 'type' => $type, + 'channel' => $channel, + 'model_type' => $model ? get_class($model) : null, + 'model_id' => optional($model)->id, + 'unsubscribed_at' => $unsubscribe ? $this->freshTimestamp() : null + ]); + } - public function isSubscribed($type, $channel, $model = null, $optin = []) - { - $subscription = $this->findSubscription($type, $channel, $model) ?: $this->findSubscription($type, "*", $model); - - if (! $subscription) { - return ! in_array($channel, $optin); - } - - return $subscription->isSubscribed(); - } - - public function resetSubscriptions($type, $model = null) - { - $this->notificationSubscriptions()->where('type', $type)->model($model)->delete(); - - return $this; - } - -} \ No newline at end of file + public function isSubscribed($type, $channel, $model = null, $optin = []) + { + $subscription = $this->findSubscription($type, $channel, $model) ?: $this->findSubscription($type, "*", $model); + + if (! $subscription) { + return ! in_array($channel, $optin); + } + + return $subscription->isSubscribed(); + } + + public function resetSubscriptions($type, $model = null) + { + $this->notificationSubscriptions()->where('type', $type)->model($model)->delete(); + + return $this; + } +}