Skip to content

Commit d1560de

Browse files
committed
Log unserialize scheduler mails.
1 parent fadfc21 commit d1560de

File tree

4 files changed

+66
-21
lines changed

4 files changed

+66
-21
lines changed

src/Actions/SendMailAction.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Binarcode\LaravelMailator\Models\MailatorSchedule;
77
use Binarcode\LaravelMailator\Support\ClassResolver;
88
use Exception;
9+
use Illuminate\Contracts\Mail\Mailable;
910
use Illuminate\Support\Facades\Mail;
1011

1112
class SendMailAction implements Action
@@ -28,12 +29,14 @@ public function handle(MailatorSchedule $schedule)
2829
protected function sendMail(MailatorSchedule $schedule)
2930
{
3031
//todo - apply replacers for variables maybe
31-
Mail::to($schedule->getRecipients())->send(
32-
$schedule->getMailable()
33-
);
32+
$mailable = $schedule->getMailable();
3433

35-
$schedule->markAsSent();
34+
if ($mailable instanceof Mailable) {
35+
Mail::to($schedule->getRecipients())->send($mailable);
3636

37-
event(new ScheduleMailSentEvent($schedule));
37+
$schedule->markAsSent();
38+
39+
event(new ScheduleMailSentEvent($schedule));
40+
}
3841
}
3942
}

src/Models/MailatorSchedule.php

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Illuminate\Support\Str;
2525
use Opis\Closure\SerializableClosure;
2626
use Throwable;
27+
use TypeError;
2728

2829
/**
2930
* Class MailatorSchedule.
@@ -289,7 +290,7 @@ public function recipients(...$recipients): self
289290
{
290291
$this->recipients = array_merge(collect($recipients)
291292
->flatten()
292-
->filter(fn ($email) => $this->ensureValidEmail($email))
293+
->filter(fn($email) => $this->ensureValidEmail($email))
293294
->unique()
294295
->toArray(), $this->recipients ?? []);
295296

@@ -363,18 +364,24 @@ public static function run(): void
363364
static::query()
364365
->ready()
365366
->cursor()
366-
->filter(fn (self $schedule) => $schedule->shouldSend())
367-
->each(fn (self $schedule) => $schedule->execute());
367+
->filter(fn(self $schedule) => $schedule->shouldSend())
368+
->each(fn(self $schedule) => $schedule->execute());
368369
}
369370

370371
public function hasCustomAction(): bool
371372
{
372-
return ! is_null($this->action);
373+
return !is_null($this->action);
373374
}
374375

375-
public function getMailable(): Mailable
376+
public function getMailable(): ?Mailable
376377
{
377-
return unserialize($this->mailable_class);
378+
try {
379+
return unserialize($this->mailable_class);
380+
} catch (Throwable | TypeError $e) {
381+
$this->markAsFailed($e->getMessage());
382+
}
383+
384+
return null;
378385
}
379386

380387
public function markAsSent(): self
@@ -418,13 +425,13 @@ public function markAsFailed(string $failureReason): self
418425
public function getRecipients(): array
419426
{
420427
return collect($this->recipients)
421-
->filter(fn ($email) => $this->ensureValidEmail($email))
428+
->filter(fn($email) => $this->ensureValidEmail($email))
422429
->toArray();
423430
}
424431

425432
protected function ensureValidEmail(string $email): bool
426433
{
427-
return ! Validator::make(
434+
return !Validator::make(
428435
compact('email'),
429436
['email' => 'required|email']
430437
)->fails();
@@ -437,7 +444,7 @@ public function actionClass(Action $action): self
437444
return $this;
438445
}
439446

440-
public function tag(string | array $tag): self
447+
public function tag(string|array $tag): self
441448
{
442449
if (is_array($tag)) {
443450
$tag = implode(',', $tag);
@@ -485,12 +492,12 @@ public function markComplete(): self
485492
public function failedLastTimes(int $times): bool
486493
{
487494
return $this
488-
->logs()
489-
->latest()
490-
->take($times)
491-
->get()
492-
->filter
493-
->isFailed()
494-
->count() === $times;
495+
->logs()
496+
->latest()
497+
->take($times)
498+
->get()
499+
->filter
500+
->isFailed()
501+
->count() === $times;
495502
}
496503
}

tests/Feature/Models/MailatorScheduleTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Binarcode\LaravelMailator\Models\MailatorSchedule;
66
use Binarcode\LaravelMailator\Tests\database\Factories\UserFactory;
77
use Binarcode\LaravelMailator\Tests\Fixtures\InvoiceReminderMailable;
8+
use Binarcode\LaravelMailator\Tests\Fixtures\User;
89
use Binarcode\LaravelMailator\Tests\TestCase;
910
use Illuminate\Support\Facades\Mail;
1011
use Spatie\TestTime\TestTime;
@@ -114,4 +115,30 @@ public function test_recipients_merges(): void
114115

115116
self::assertSame([$mail3, $mail2, $mail], $scheduler->recipients);
116117
}
118+
119+
public function test_unserialized_exception_store_exception_log(): void
120+
{
121+
Mail::fake();
122+
Mail::assertNothingSent();
123+
124+
$scheduler = MailatorSchedule::init('Invoice reminder.')
125+
->recipients([
126+
127+
])
128+
->mailable(
129+
(new InvoiceReminderMailable($user = User::factory()->create()))->to('[email protected]')
130+
)
131+
->days(1)
132+
->after(now()->addDays(7));
133+
134+
$scheduler->save();
135+
136+
$user->forceDelete();
137+
138+
$this->assertCount(0, $scheduler->logs);
139+
140+
$scheduler->getMailable();
141+
142+
$this->assertCount(1, $scheduler->logs()->get());
143+
}
117144
}

tests/Fixtures/InvoiceReminderMailable.php

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

55
use Illuminate\Bus\Queueable;
66
use Illuminate\Mail\Mailable;
7+
use Illuminate\Queue\SerializesModels;
78

89
class InvoiceReminderMailable extends Mailable
910
{
1011
use Queueable;
12+
use SerializesModels;
13+
14+
public function __construct(
15+
private ?User $user = null,
16+
)
17+
{
18+
}
1119

1220
public function build()
1321
{

0 commit comments

Comments
 (0)