Skip to content

Commit d2d048c

Browse files
authored
Merge pull request #2550 from BinarCode/2.x
2.x
2 parents 19b9b54 + 5057b77 commit d2d048c

File tree

13 files changed

+345
-35
lines changed

13 files changed

+345
-35
lines changed

README.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
<a href="https://packagist.org/packages/binarcode/laravel-mailator"><img src="https://poser.pugx.org/binarcode/laravel-mailator/license" alt="License"></a>
77
</p>
88

9-
Laravel Mailator provides a featherweight system for configure email scheduler and email templates based on application events.
9+
Laravel Mailator provides a featherweight system for configure email scheduler and email templates based on application
10+
events.
1011

1112
## Installation
1213

@@ -30,7 +31,6 @@ It has mainly 2 directions of usage:
3031

3132
2. Email Scheduler
3233

33-
3434
## Templating
3535

3636
To create an email template:
@@ -58,11 +58,14 @@ $template->placeholders()->create(
5858

5959
To use the template, you simply have to add the `WithMailTemplate` trait to your mailable.
6060

61-
This will enforce you to implement the `getReplacers` method, this should return an array of replacers to your template. The array may contain instances of `Binarcode\LaravelMailator\Replacers\Replacer` or even `Closure` instances.
61+
This will enforce you to implement the `getReplacers` method, this should return an array of replacers to your template.
62+
The array may contain instances of `Binarcode\LaravelMailator\Replacers\Replacer` or even `Closure` instances.
6263

63-
Mailator shipes with a builtin replacer `ModelAttributesReplacer`, it will automaticaly replace attributes from the model you provide to placeholders.
64+
Mailator shipes with a builtin replacer `ModelAttributesReplacer`, it will automaticaly replace attributes from the
65+
model you provide to placeholders.
6466

65-
The last step is how to say to your mailable what template to use. This could be done into the build method as shown bellow:
67+
The last step is how to say to your mailable what template to use. This could be done into the build method as shown
68+
bellow:
6669

6770
```php
6871
class WelcomeMailatorMailable extends Mailable
@@ -94,10 +97,9 @@ class WelcomeMailatorMailable extends Mailable
9497
}
9598
```
9699

97-
98100
## Scheduler
99101

100-
To setup a mail to be sent after or before an event, you can do this by using `MailatorSchedule`.
102+
To setup a mail to be sent after or before an event, you can do this by using `MailatorSchedule`.
101103

102104
Firstly lets setup a mail scheduler:
103105

@@ -116,7 +118,8 @@ Binarcode\LaravelMailator\Models\MailatorSchedule::init('Invoice reminder.')
116118
->save();
117119
```
118120

119-
The `constraint` mutator accept an instance of `Binarcode\LaravelMailator\Constraints\SendScheduleConstraint`, based on this the Mailator will decide to send or to not send the email.
121+
The `constraint` mutator accept an instance of `Binarcode\LaravelMailator\Constraints\SendScheduleConstraint`, based on
122+
this the Mailator will decide to send or to not send the email.
120123

121124
Let's assume we have this `BeforeInvoiceExpiresConstraint` constraint:
122125

@@ -139,6 +142,18 @@ Binarcode\LaravelMailator\Models\MailatorSchedule::run();
139142

140143
The Mailator will take care of all your mails that needs to be sent, and it will send them.
141144

145+
Using `Scheduler` you can even define your custom action:
146+
147+
```php
148+
$scheduler = MailatorSchedule::init('Invoice reminder.')
149+
->days(1)
150+
->before(now()->addWeek())
151+
->actionClass(CustomAction::class)
152+
->save();
153+
```
154+
155+
The `CustomAction` should implement the `Binarcode\LaravelMailator\Actions\Action` class.
156+
142157
### Testing
143158

144159
``` bash
@@ -155,7 +170,8 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
155170

156171
### Security
157172

158-
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
173+
If you discover any security related issues, please email [email protected] instead of using the issue
174+
tracker.
159175

160176
## Credits
161177

database/migrations/create_mailator_tables.php.stub

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

1616
$table->string('name', 100)->nullable();
17-
$table->text('mailable_class');
17+
$table->text('mailable_class')->nullable();
18+
$table->text('action')->nullable();
1819
$table->unsignedInteger('delay_minutes')->nullable()->comment('Number of hours/days.');
1920
$table->enum('time_frame_origin', [
2021
MailatorSchedule::TIME_FRAME_ORIGIN_BEFORE,
@@ -24,12 +25,7 @@ class CreateMailatorTables extends Migration
2425
$table->json('constraints')->nullable()->comment('Offset target.');
2526
$table->json('recipients')->nullable();
2627
$table->text('when')->nullable();
27-
$table->enum('frequency_option', [
28-
MailatorSchedule::FREQUENCY_OPTIONS_ONCE,
29-
MailatorSchedule::FREQUENCY_OPTIONS_HOURLY,
30-
MailatorSchedule::FREQUENCY_OPTIONS_DAILY,
31-
MailatorSchedule::FREQUENCY_OPTIONS_WEEKLY,
32-
])->default(MailatorSchedule::FREQUENCY_OPTIONS_ONCE)->comment('How often send email notification.');
28+
$table->string('frequency_option')->default(MailatorSchedule::FREQUENCY_OPTIONS_ONCE)->comment('How often send email notification.');
3329

3430
$table->timestamp('last_sent_at')->nullable();
3531
$table->timestamp('last_failed_at')->nullable();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelMailator\Constraints;
4+
5+
use Binarcode\LaravelMailator\Models\MailatorSchedule;
6+
use Illuminate\Support\Collection;
7+
8+
class AfterConstraint implements SendScheduleConstraint
9+
{
10+
public function canSend(MailatorSchedule $schedule, Collection $logs): bool
11+
{
12+
if (! $schedule->isAfter()) {
13+
return true;
14+
}
15+
16+
if (is_null($schedule->timestamp_target)) {
17+
return true;
18+
}
19+
20+
// it's in the future
21+
if (now()->lt($schedule->timestamp_target)) {
22+
return false;
23+
}
24+
25+
//till ends we should have at least toDays days
26+
return $schedule->isOnce()
27+
? $schedule->timestamp_target->diffInDays(now()) === $schedule->toDays()
28+
: $schedule->timestamp_target->diffInDays(now()) > $schedule->toDays();
29+
}
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelMailator\Constraints;
4+
5+
use Binarcode\LaravelMailator\Models\MailatorLog;
6+
use Binarcode\LaravelMailator\Models\MailatorSchedule;
7+
use Illuminate\Support\Collection;
8+
9+
class DailyConstraint implements SendScheduleConstraint
10+
{
11+
public function canSend(MailatorSchedule $schedule, Collection $logs): bool
12+
{
13+
if (! $schedule->isDaily()) {
14+
return true;
15+
}
16+
17+
if ($logs->count() === 0) {
18+
return true;
19+
}
20+
21+
$lastLog = $logs
22+
->filter(fn (MailatorLog $log) => $log->isSent())
23+
->last();
24+
25+
if ($lastLog instanceof MailatorLog) {
26+
return $lastLog->created_at->diffInDays(now()) >= 1;
27+
} else {
28+
return true;
29+
}
30+
}
31+
}

src/Constraints/ManyConstraint.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelMailator\Constraints;
4+
5+
use Binarcode\LaravelMailator\Models\MailatorSchedule;
6+
use Illuminate\Support\Collection;
7+
8+
class ManyConstraint implements SendScheduleConstraint
9+
{
10+
public function canSend(MailatorSchedule $schedule, Collection $logs): bool
11+
{
12+
return $schedule->isMany()
13+
? true
14+
: true;
15+
}
16+
}

src/Constraints/OnceConstraint.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelMailator\Constraints;
4+
5+
use Binarcode\LaravelMailator\Models\MailatorSchedule;
6+
use Illuminate\Support\Collection;
7+
8+
class OnceConstraint implements SendScheduleConstraint
9+
{
10+
public function canSend(MailatorSchedule $schedule, Collection $logs): bool
11+
{
12+
return $schedule->isOnce()
13+
? $logs->count() === 0
14+
: true;
15+
}
16+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelMailator\Constraints;
4+
5+
use Binarcode\LaravelMailator\Models\MailatorLog;
6+
use Binarcode\LaravelMailator\Models\MailatorSchedule;
7+
use Illuminate\Support\Collection;
8+
9+
class WeeklyConstraint implements SendScheduleConstraint
10+
{
11+
public function canSend(MailatorSchedule $schedule, Collection $logs): bool
12+
{
13+
if (! $schedule->isWeekly()) {
14+
return true;
15+
}
16+
17+
if ($logs->count() === 0) {
18+
return true;
19+
}
20+
21+
$lastLog = $logs
22+
->filter(fn (MailatorLog $log) => $log->isSent())
23+
->last();
24+
25+
if ($lastLog instanceof MailatorLog) {
26+
return $lastLog->created_at->diffInDays(now()) >= 7;
27+
} else {
28+
return true;
29+
}
30+
}
31+
}

src/Jobs/SendMailJob.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(MailatorSchedule $schedule)
3232
public function handle()
3333
{
3434
/** * @var SendMailAction $sendMailAction */
35-
$sendMailAction = $this->schedule->action;
35+
$sendMailAction = app(Config::get('mailator.scheduler.send_mail_action', SendMailAction::class));
3636

3737
$sendMailAction->handle($this->schedule);
3838
}

src/Models/Concerns/ConstraintsResolver.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33

44
namespace Binarcode\LaravelMailator\Models\Concerns;
55

6+
use Binarcode\LaravelMailator\Constraints\AfterConstraint;
67
use Binarcode\LaravelMailator\Constraints\BeforeConstraint;
8+
use Binarcode\LaravelMailator\Constraints\DailyConstraint;
9+
use Binarcode\LaravelMailator\Constraints\ManyConstraint;
10+
use Binarcode\LaravelMailator\Constraints\OnceConstraint;
711
use Binarcode\LaravelMailator\Constraints\SendScheduleConstraint;
12+
use Binarcode\LaravelMailator\Constraints\WeeklyConstraint;
813
use Binarcode\LaravelMailator\Models\MailatorSchedule;
914

1015
/**
@@ -18,6 +23,11 @@ public function configurationsPasses(): bool
1823
{
1924
return collect([
2025
BeforeConstraint::class,
26+
AfterConstraint::class,
27+
OnceConstraint::class,
28+
ManyConstraint::class,
29+
DailyConstraint::class,
30+
WeeklyConstraint::class,
2131
])
2232
->map(fn ($class) => app($class))
2333
->every(fn (SendScheduleConstraint $event) => $event->canSend($this, $this->logs));

src/Models/MailatorLog.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
namespace Binarcode\LaravelMailator\Models;
44

5+
use Carbon\Carbon;
56
use Illuminate\Database\Eloquent\Model;
67

8+
/**
9+
* Class MailatorLog
10+
* @property Carbon $created_at
11+
* @package Binarcode\LaravelMailator\Models
12+
*/
713
class MailatorLog extends Model
814
{
915
public function getTable()
@@ -31,4 +37,9 @@ public function getTable()
3137
'updated_at' => 'datetime',
3238
'recipients' => 'array',
3339
];
40+
41+
public function isSent(): bool
42+
{
43+
return $this->status === static::STATUS_SENT;
44+
}
3445
}

0 commit comments

Comments
 (0)