Skip to content

Commit

Permalink
Fix formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
liran-co committed Jan 15, 2021
1 parent 6cc6687 commit 9f2898f
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 75 deletions.
4 changes: 2 additions & 2 deletions config/notification-subscriptions.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

return [
'excluded_channels' => ['database'],
'excluded_channels' => ['database'],

];
6 changes: 4 additions & 2 deletions src/Listeners/NotificationSendingListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
4 changes: 1 addition & 3 deletions src/Models/NotificationSubscription.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php
<?php

namespace LiranCo\NotificationSubscriptions\Models;

use Illuminate\Database\Eloquent\Model;

class NotificationSubscription extends Model
{

public $incrementing = true;

protected $table = 'notification_subscriptions';
Expand Down Expand Up @@ -41,5 +40,4 @@ public function resubscribe()
{
$this->forceFill(['unsubscribed_at' => null])->save();
}

}
5 changes: 3 additions & 2 deletions src/NotificationSubscriptionsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 4 additions & 4 deletions src/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
122 changes: 60 additions & 62 deletions src/Traits/HasNotificationSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
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;
}
}

0 comments on commit 9f2898f

Please sign in to comment.