Skip to content

Commit

Permalink
use custom model in send command (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiancalara authored Apr 16, 2024
1 parent de570f0 commit 7a5a7d2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/Console/Commands/SendScheduledNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Thomasjohnkane\Snooze\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Thomasjohnkane\Snooze\Models\ScheduledNotification;
use Thomasjohnkane\Snooze\Models\ScheduledNotification as ScheduledNotificationModel;
use Thomasjohnkane\Snooze\ScheduledNotification;

class SendScheduledNotifications extends Command
{
Expand All @@ -31,11 +31,7 @@ public function handle(): void
{
$tolerance = config('snooze.sendTolerance');

$notifications = ScheduledNotification::whereNull('sent_at')
->whereNull('cancelled_at')
->where('send_at', '<=', Carbon::now())
->where('send_at', '>=', Carbon::now()->subSeconds($tolerance ?? 60))
->get();
$notifications = ScheduledNotification::getPendingNotifications($tolerance);

if (! $notifications->count()) {
$this->info('No Scheduled Notifications need to be sent.');
Expand All @@ -51,7 +47,7 @@ public function handle(): void

$this->info(sprintf('Sending %d scheduled notifications...', $notifications->count()));

$notifications->each(function (ScheduledNotification $notification) use ($bar) {
$notifications->each(function (ScheduledNotificationModel $notification) use ($bar) {
$bar->advance();

try {
Expand Down
12 changes: 12 additions & 0 deletions src/ScheduledNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ public static function create(
]));
}

public static function getPendingNotifications(int $tolerance = null): \Illuminate\Database\Eloquent\Collection
{
$modelClass = self::getScheduledNotificationModelClass();

return $modelClass::query()
->whereNull('sent_at')
->whereNull('cancelled_at')
->where('send_at', '<=', Carbon::now())
->where('send_at', '>=', Carbon::now()->subSeconds($tolerance ?? 60))
->get();
}

public static function find(int $scheduledNotificationId): ?self
{
$modelClass = self::getScheduledNotificationModelClass();
Expand Down
14 changes: 14 additions & 0 deletions tests/Models/CustomScheduledNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Thomasjohnkane\Snooze\Tests\Models;

use Exception;
use Thomasjohnkane\Snooze\Models\ScheduledNotification;

class CustomScheduledNotification extends ScheduledNotification
{
public function send(): void
{
throw new Exception('Custom send method');
}
}
19 changes: 19 additions & 0 deletions tests/SendCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace Thomasjohnkane\Snooze\Tests;

use Carbon\Carbon;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;
use Thomasjohnkane\Snooze\Models\ScheduledNotification as ScheduledNotificationModel;
use Thomasjohnkane\Snooze\ScheduledNotification;
use Thomasjohnkane\Snooze\Tests\Models\CustomScheduledNotification;
use Thomasjohnkane\Snooze\Tests\Models\User;
use Thomasjohnkane\Snooze\Tests\Notifications\TestNotification;
use TiMacDonald\Log\LogEntry;
Expand Down Expand Up @@ -78,4 +80,21 @@ public function testItCatchesFailedScheduledNotifications()
&& $log->message === 'Cannot Send. Unserialize Failed. (unserialize(): Error at offset 0 of 10 bytes)'
);
}

public function testUsesCustomModelWhenSending()
{
Log::swap(new LogFake());
$target = User::find(1);

Config::set('snooze.model', CustomScheduledNotification::class);
$target->notifyAt(new TestNotification(User::find(2)), Carbon::now());

$this->artisan('snooze:send')
->expectsOutput('Starting Sending Scheduled Notifications')
->assertExitCode(0);

Log::assertLogged(fn (LogEntry $log) => $log->level === 'error'
&& $log->message === 'Custom send method'
);
}
}

0 comments on commit 7a5a7d2

Please sign in to comment.