Skip to content

Commit

Permalink
Feature/handle model not found exception (#133)
Browse files Browse the repository at this point in the history
Co-authored-by: Sıddık AKÇAY <[email protected]>
  • Loading branch information
sakcaysoftware and Sıddık AKÇAY authored Dec 16, 2024
1 parent bf0d701 commit 367228c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
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);
}
}

0 comments on commit 367228c

Please sign in to comment.