Skip to content

Commit 0b88b07

Browse files
authored
Merge pull request #2518 from BinarCode/setup
wip
2 parents 66980f8 + 432d4c5 commit 0b88b07

20 files changed

+297
-67
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ coverage
66
output
77
docs/node_modules
88
docs/.vuepress/dist
9+
.php_cs.cache

.php_cs.cache

Lines changed: 0 additions & 1 deletion
This file was deleted.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"require": {
2020
"php": "^7.4",
2121
"illuminate/support": "^7.0",
22-
"opis/closure": "^3.5"
22+
"opis/closure": "^3.5",
23+
"spatie/test-time": "^1.2"
2324
},
2425
"require-dev": {
2526
"friendsofphp/php-cs-fixer": "^2.16",

config/mailator.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,22 @@
1111
*/
1212
'logs_table' => 'mailator_logs',
1313

14-
/**
15-
* > The base model for the mail schedule.
16-
*/
17-
'scheduler_model' => Binarcode\LaravelMailator\Models\MailatorSchedule::class,
14+
'scheduler' => [
15+
/**
16+
* > The base model for the mail schedule.
17+
*/
18+
'model' => Binarcode\LaravelMailator\Models\MailatorSchedule::class,
19+
20+
/**
21+
> The queue used for sending emails.
22+
*/
23+
'send_mail_job_queue' => 'default',
24+
25+
/**
26+
> The email sender class. It will be executed from the sender job.
27+
*/
28+
'send_mail_action' => Binarcode\LaravelMailator\Actions\SendMailAction::class,
29+
],
30+
31+
'log_model' => Binarcode\LaravelMailator\Models\MailatorLog::class,
1832
];

database/migrations/create_mailator_tables.php.stub

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ class CreateMailatorTables extends Migration
1414
$table->bigIncrements('id');
1515

1616
$table->string('name', 100)->nullable();
17-
$table->string('mailable_class');
17+
$table->text('mailable_class');
1818
$table->unsignedInteger('delay_minutes')->nullable()->comment('Number of hours/days.');
1919
$table->enum('time_frame_origin', [
2020
MailatorSchedule::TIME_FRAME_ORIGIN_BEFORE,
2121
MailatorSchedule::TIME_FRAME_ORIGIN_AFTER,
2222
])->nullable()->comment('Before or after event.');
2323
$table->json('events')->nullable()->comment('Offset target.');
24+
$table->json('recipients')->nullable();
2425
$table->text('when')->nullable();
2526
$table->enum('frequency_option', [
2627
MailatorSchedule::FREQUENCY_OPTIONS_ONCE,
@@ -29,13 +30,16 @@ class CreateMailatorTables extends Migration
2930
MailatorSchedule::FREQUENCY_OPTIONS_WEEKLY,
3031
])->default(MailatorSchedule::FREQUENCY_OPTIONS_ONCE)->comment('How often send email notification.');
3132

33+
$table->timestamp('last_sent_at')->nullable();
34+
$table->timestamp('last_failed_at')->nullable();
35+
$table->text('failure_reason')->nullable();
3236
$table->timestamps();
3337
});
3438

3539
Schema::create(config('mailator.logs_table', 'mailator_logs'), function (Blueprint $table) {
3640
$table->bigIncrements('id');
3741
$table->string('name')->nullable();
38-
$table->string('recipient_email')->nullable();
42+
$table->json('recipients')->nullable();
3943
$table->unsignedInteger('mailator_schedule_id')->nullable();
4044
$table->enum('status', [
4145
MailatorLog::STATUS_FAILED,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Explicabo iusto laudantium molestias quae ratione vitae? Architecto dignissimos inventore laudantium omnis? Beatae blanditiis ex neque obcaecati quos voluptate voluptatem. Ducimus, quo.

src/Actions/SendMailAction.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelMailator\Actions;
4+
5+
use Binarcode\LaravelMailator\Events\ScheduleMailSentEvent;
6+
use Binarcode\LaravelMailator\Models\MailatorSchedule;
7+
use Exception;
8+
use Illuminate\Support\Facades\Mail;
9+
10+
class SendMailAction
11+
{
12+
public function handle(MailatorSchedule $schedule)
13+
{
14+
try {
15+
$this->sendMail($schedule);
16+
} catch (Exception $exception) {
17+
report($exception);
18+
19+
$schedule->markAsFailed($exception->getMessage());
20+
}
21+
}
22+
23+
protected function sendMail(MailatorSchedule $schedule)
24+
{
25+
//todo - apply replacers for variables maybe
26+
Mail::to($schedule->getRecipients())->send(
27+
$schedule->getMailable()
28+
);
29+
30+
$schedule->markAsSent();
31+
32+
event(new ScheduleMailSentEvent($schedule));
33+
}
34+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelMailator\Constraints;
4+
5+
use Binarcode\LaravelMailator\Models\MailatorSchedule;
6+
use Illuminate\Support\Collection;
7+
8+
interface SendScheduleConstraint
9+
{
10+
public function canSend(MailatorSchedule $mailatorSchedule, Collection $logs): bool;
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelMailator\Events;
4+
5+
use Binarcode\LaravelMailator\Models\MailatorSchedule;
6+
7+
class ScheduleMailSentEvent
8+
{
9+
public MailatorSchedule $schedule;
10+
11+
public function __construct(MailatorSchedule $schedule)
12+
{
13+
$this->schedule = $schedule;
14+
}
15+
}

src/Exceptions/InstanceException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace Binarcode\LaravelMailator\Exceptions;
44

5-
use Binarcode\LaravelMailator\MailatorEvent;
5+
use Binarcode\LaravelMailator\Constraints\SendScheduleConstraint;
66
use Exception;
77

88
class InstanceException extends Exception
99
{
1010
public static function throw(string $actual)
1111
{
12-
return new static('Expected instance of '.MailatorEvent::class.", given [$actual].");
12+
return new static('Expected instance of ' . SendScheduleConstraint::class . ", given [$actual].");
1313
}
1414
}

0 commit comments

Comments
 (0)