Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 367228c

Browse files
sakcaysoftwareSıddık AKÇAY
andauthored
Feature/handle model not found exception (#133)
Co-authored-by: Sıddık AKÇAY <[email protected]>
1 parent bf0d701 commit 367228c

4 files changed

Lines changed: 99 additions & 0 deletions

File tree

config/snooze.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,11 @@
5656
* Should snooze automatically register the migrations
5757
*/
5858
'registerMigrations' => env('SCHEDULED_NOTIFICATIONS_REGISTER_MIGRATIONS', true),
59+
60+
/*
61+
* Set true if you want delete notification when
62+
* any model missing during unserialization
63+
* otherwise an exception will thrown
64+
*/
65+
'deleteWhenMissingModels' => env('SCHEDULED_NOTIFICATIONS_DELETE_WHEN_MISSING_MODELS', false),
5966
];

src/Events/NotificationDeleted.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Thomasjohnkane\Snooze\Events;
4+
5+
use Illuminate\Queue\SerializesModels;
6+
use Thomasjohnkane\Snooze\Models\ScheduledNotification;
7+
8+
class NotificationDeleted
9+
{
10+
use SerializesModels;
11+
12+
public $scheduledNotification;
13+
14+
/**
15+
* Create a new event instance.
16+
*
17+
* @param \Thomasjohnkane\Snooze\Models\ScheduledNotification $scheduledNotification
18+
* @return void
19+
*/
20+
public function __construct(ScheduledNotification $scheduledNotification)
21+
{
22+
$this->scheduledNotification = $scheduledNotification;
23+
}
24+
}

src/Models/ScheduledNotification.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Carbon\Carbon;
66
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\ModelNotFoundException;
8+
use Thomasjohnkane\Snooze\Events\NotificationDeleted;
79
use Thomasjohnkane\Snooze\Events\NotificationInterrupted;
810
use Thomasjohnkane\Snooze\Events\NotificationRescheduled;
911
use Thomasjohnkane\Snooze\Events\NotificationSent;
@@ -56,6 +58,15 @@ public function send(): void
5658
try {
5759
$notifiable = $this->serializer->unserialize($this->target);
5860
$notification = $this->serializer->unserialize($this->notification);
61+
} catch(ModelNotFoundException $e) {
62+
if (config('snooze.deleteWhenMissingModels', false) === true) {
63+
$this->delete();
64+
event(new NotificationDeleted($this));
65+
66+
return;
67+
}
68+
69+
throw $e;
5970
} catch (\Exception $exception) {
6071
throw new UnserializeFailedException(sprintf('Cannot Send. Unserialize Failed. (%s)', $exception->getMessage()), 2, $exception);
6172
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Thomasjohnkane\Snooze\Tests;
4+
5+
use Carbon\Carbon;
6+
use Illuminate\Support\Facades\Event;
7+
use Illuminate\Support\Facades\Notification;
8+
use Thomasjohnkane\Snooze\Events\NotificationDeleted;
9+
use Thomasjohnkane\Snooze\Tests\Models\User;
10+
use Thomasjohnkane\Snooze\Tests\Notifications\TestNotification;
11+
12+
class CanDeleteNotificationTest extends TestCase
13+
{
14+
public function testNotificationIsDeleted()
15+
{
16+
Notification::fake();
17+
Event::fake();
18+
19+
$this->app->config->set('snooze.deleteWhenMissingModels', true);
20+
21+
$target = User::find(1);
22+
$notification = $target->notifyAt(new TestNotification(User::find(1)), Carbon::now()->subSeconds(10));
23+
$target->delete();
24+
25+
$this->assertDatabaseHas('scheduled_notifications', ['id' => $notification->getId()]);
26+
27+
$this->artisan('snooze:send');
28+
29+
$this->assertDatabaseMissing('scheduled_notifications', ['id' => $notification->getId()]);
30+
31+
Notification::assertNothingSent();
32+
Event::assertDispatched(NotificationDeleted::class, 1);
33+
}
34+
35+
public function testNotificationIsNotDeleted()
36+
{
37+
Notification::fake();
38+
Event::fake();
39+
40+
$this->app->config->set('snooze.deleteWhenMissingModels', false);
41+
42+
$target = User::find(2);
43+
44+
$notification = $target->notifyAt(new TestNotification(User::find(2)), Carbon::now()->subSeconds(10));
45+
$target->delete();
46+
47+
$this->assertDatabaseHas('scheduled_notifications', ['id' => $notification->getId()]);
48+
49+
$this->artisan('snooze:send');
50+
51+
$this->assertDatabaseHas('scheduled_notifications', ['id' => $notification->getId()]);
52+
$this->assertFalse($notification->isSent());
53+
54+
Notification::assertNothingSent();
55+
Event::assertNotDispatched(NotificationDeleted::class);
56+
}
57+
}

0 commit comments

Comments
 (0)