Skip to content

Commit

Permalink
Merge pull request #9 from liran-co/analysis-64KGwN
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
liran-co authored Mar 7, 2021
2 parents 9f2898f + 6c08eae commit d18e87d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion config/notification-subscriptions.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

return [

'excluded_channels' => ['database'],

];
18 changes: 9 additions & 9 deletions src/Listeners/NotificationSendingListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
}
8 changes: 4 additions & 4 deletions src/Models/NotificationSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/NotificationSubscriptionsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
40 changes: 20 additions & 20 deletions src/Traits/HasNotificationSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit d18e87d

Please sign in to comment.