Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/handle model not found exception #133

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config/snooze.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,11 @@
* Should snooze automatically register the migrations
*/
'registerMigrations' => env('SCHEDULED_NOTIFICATIONS_REGISTER_MIGRATIONS', true),

/*
* Set true if you want delete notification when
* any model missing during unserialization
* otherwise an exception will thrown
*/
'deleteWhenMissingModels' => env('SCHEDULED_NOTIFICATIONS_DELETE_WHEN_MISSING_MODELS', false),
];
24 changes: 24 additions & 0 deletions src/Events/NotificationDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Thomasjohnkane\Snooze\Events;

use Illuminate\Queue\SerializesModels;
use Thomasjohnkane\Snooze\Models\ScheduledNotification;

class NotificationDeleted
{
use SerializesModels;

public $scheduledNotification;

/**
* Create a new event instance.
*
* @param \Thomasjohnkane\Snooze\Models\ScheduledNotification $scheduledNotification
* @return void
*/
public function __construct(ScheduledNotification $scheduledNotification)
{
$this->scheduledNotification = $scheduledNotification;
}
}
11 changes: 11 additions & 0 deletions src/Models/ScheduledNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Thomasjohnkane\Snooze\Events\NotificationDeleted;
use Thomasjohnkane\Snooze\Events\NotificationInterrupted;
use Thomasjohnkane\Snooze\Events\NotificationRescheduled;
use Thomasjohnkane\Snooze\Events\NotificationSent;
Expand Down Expand Up @@ -56,6 +58,15 @@ public function send(): void
try {
$notifiable = $this->serializer->unserialize($this->target);
$notification = $this->serializer->unserialize($this->notification);
} catch(ModelNotFoundException $e) {
if (config('snooze.deleteWhenMissingModels', false) === true) {
$this->delete();
event(new NotificationDeleted($this));

return;
}

throw $e;
} catch (\Exception $exception) {
throw new UnserializeFailedException(sprintf('Cannot Send. Unserialize Failed. (%s)', $exception->getMessage()), 2, $exception);
}
Expand Down
57 changes: 57 additions & 0 deletions tests/CanDeleteNotificationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Thomasjohnkane\Snooze\Tests;

use Carbon\Carbon;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Notification;
use Thomasjohnkane\Snooze\Events\NotificationDeleted;
use Thomasjohnkane\Snooze\Tests\Models\User;
use Thomasjohnkane\Snooze\Tests\Notifications\TestNotification;

class CanDeleteNotificationTest extends TestCase
{
public function testNotificationIsDeleted()
{
Notification::fake();
Event::fake();

$this->app->config->set('snooze.deleteWhenMissingModels', true);

$target = User::find(1);
$notification = $target->notifyAt(new TestNotification(User::find(1)), Carbon::now()->subSeconds(10));
$target->delete();

$this->assertDatabaseHas('scheduled_notifications', ['id' => $notification->getId()]);

$this->artisan('snooze:send');

$this->assertDatabaseMissing('scheduled_notifications', ['id' => $notification->getId()]);

Notification::assertNothingSent();
Event::assertDispatched(NotificationDeleted::class, 1);
}

public function testNotificationIsNotDeleted()
{
Notification::fake();
Event::fake();

$this->app->config->set('snooze.deleteWhenMissingModels', false);

$target = User::find(2);

$notification = $target->notifyAt(new TestNotification(User::find(2)), Carbon::now()->subSeconds(10));
$target->delete();

$this->assertDatabaseHas('scheduled_notifications', ['id' => $notification->getId()]);

$this->artisan('snooze:send');

$this->assertDatabaseHas('scheduled_notifications', ['id' => $notification->getId()]);
$this->assertFalse($notification->isSent());

Notification::assertNothingSent();
Event::assertNotDispatched(NotificationDeleted::class);
}
}