diff --git a/config/notification-subscriptions.php b/config/notification-subscriptions.php index 3a10731..4eca209 100644 --- a/config/notification-subscriptions.php +++ b/config/notification-subscriptions.php @@ -1,7 +1,7 @@ ['database'], ]; diff --git a/src/Listeners/NotificationSendingListener.php b/src/Listeners/NotificationSendingListener.php index e758e73..4249722 100644 --- a/src/Listeners/NotificationSendingListener.php +++ b/src/Listeners/NotificationSendingListener.php @@ -2,21 +2,21 @@ namespace LiranCo\NotificationSubscriptions\Listeners; -use LiranCo\NotificationSubscriptions\Traits\HasNotificationSubscriptions; use Illuminate\Notifications\Events\NotificationSending; +use LiranCo\NotificationSubscriptions\Traits\HasNotificationSubscriptions; class NotificationSendingListener { public function handle(NotificationSending $event) { - if (! in_array(HasNotificationSubscriptions::class, class_uses_recursive($event->notifiable))) { + if (!in_array(HasNotificationSubscriptions::class, class_uses_recursive($event->notifiable))) { return $event; } - + if (in_array($event->channel, config('notification-subscriptions.excluded_channels'))) { return $event; } - + if ($event->notification->ignoreSubscriptions ?? false) { return $event; } @@ -25,18 +25,18 @@ public function handle(NotificationSending $event) if (method_exists($event->notification, 'getSubscriptionModel')) { $model = $event->notification->getSubscriptionModel($event->notifiable); } - + $optin = []; if (method_exists($event->notification, 'getOptInSubscriptions')) { $optin = $event->notification->getOptInSubscriptions(); } - + $subscribed = $event->notifiable->isSubscribed(get_class($event->notification), $event->channel, $model, $optin); - - if (! $subscribed) { + + if (!$subscribed) { return false; } - + return $event; } } diff --git a/src/Models/NotificationSubscription.php b/src/Models/NotificationSubscription.php index b8799bf..f28ef8b 100644 --- a/src/Models/NotificationSubscription.php +++ b/src/Models/NotificationSubscription.php @@ -20,22 +20,22 @@ public function notifiable() { return $this->morphTo(); } - + public function scopeModel($query, $model = null) { return $query->where('model_type', $model ? get_class($model) : null)->where('model_id', optional($model)->id); } - + public function isSubscribed() { return is_null($this->unsubscribed_at); } - + public function unsubscribe() { $this->forceFill(['unsubscribed_at' => $this->freshTimestamp()])->save(); } - + public function resubscribe() { $this->forceFill(['unsubscribed_at' => null])->save(); diff --git a/src/NotificationSubscriptionsServiceProvider.php b/src/NotificationSubscriptionsServiceProvider.php index 0ffcac8..c519bbf 100644 --- a/src/NotificationSubscriptionsServiceProvider.php +++ b/src/NotificationSubscriptionsServiceProvider.php @@ -11,17 +11,17 @@ public function boot() $this->publishes([ __DIR__.'/../config/notification-subscriptions.php' => config_path('notification-subscriptions.php'), ]); - + $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); } - + public function register() { $this->mergeConfigFrom( __DIR__.'/../config/notification-subscriptions.php', 'notification-subscriptions' ); - + $this->app->register(\LiranCo\NotificationSubscriptions\Providers\EventServiceProvider::class); } } diff --git a/src/Traits/HasNotificationSubscriptions.php b/src/Traits/HasNotificationSubscriptions.php index 0b6162b..97534ed 100644 --- a/src/Traits/HasNotificationSubscriptions.php +++ b/src/Traits/HasNotificationSubscriptions.php @@ -10,60 +10,60 @@ 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 + '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); + $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; } }