Skip to content

Commit 19b9b54

Browse files
authored
Merge pull request #2549 from BinarCode/2.x
Changing the scheduler.
2 parents b977ba8 + fe4cdf1 commit 19b9b54

File tree

12 files changed

+275
-51
lines changed

12 files changed

+275
-51
lines changed

CHANGELOG.md

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

33
All notable changes to `laravel-mailator` will be documented in this file
44

5+
## 2.0.0 - 2021-08-03
6+
7+
- The `before` and `after` methods now accept only an instance of Carbon
8+
- You can add custom contraints using `->contraint( Binarcode\LaravelMailator\Constraints\SendScheduleConstraint)` method.
9+
10+
- The scheduler will now take care of your configurations, so if you don't specify any constraint, it will send the email based on the scheduler configurations.
11+
- Changed `events` column name to `constraints`
12+
- Added `timestamp_target` timestamp column
13+
14+
15+
516
## 1.0.0 - 2020-02-07
617

718
- initial release

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,21 @@ To setup a mail to be sent after or before an event, you can do this by using `M
102102
Firstly lets setup a mail scheduler:
103103

104104
```php
105-
use Binarcode\LaravelMailator\Tests\Fixtures\InvoiceReminderMailable;
105+
use Binarcode\LaravelMailator\Tests\Fixtures\InvoiceReminderMailable;use Binarcode\LaravelMailator\Tests\Fixtures\SerializedConditionCondition;
106106

107107
Binarcode\LaravelMailator\Models\MailatorSchedule::init('Invoice reminder.')
108108
->mailable(new InvoiceReminderMailable())
109109
->recipients(['[email protected]'])
110110
->days(1)
111-
->before(BeforeInvoiceExpiresConstraint::class)
111+
->constraint(new SerializedConditionCondition)
112+
->before(now()->addYear())
112113
->when(function () {
113114
return 'Working.';
114115
})
115116
->save();
116117
```
117118

118-
The `before` mutator accept an instance of `Binarcode\LaravelMailator\Constraints\SendScheduleConstraint`, based on this the Mailator will decide to send or to not send the email.
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.
119120

120121
Let's assume we have this `BeforeInvoiceExpiresConstraint` constraint:
121122

config/mailator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
'model' => Binarcode\LaravelMailator\Models\MailatorSchedule::class,
1919

2020
/**
21-
> The queue used for sending emails.
22-
*/
21+
* > The queue used for sending emails.
22+
*/
2323
'send_mail_job_queue' => 'default',
2424

2525
/**

database/migrations/create_mailator_tables.php.stub

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class CreateMailatorTables extends Migration
2020
MailatorSchedule::TIME_FRAME_ORIGIN_BEFORE,
2121
MailatorSchedule::TIME_FRAME_ORIGIN_AFTER,
2222
])->nullable()->comment('Before or after event.');
23-
$table->json('events')->nullable()->comment('Offset target.');
23+
$table->timestamp('timestamp_target')->nullable();
24+
$table->json('constraints')->nullable()->comment('Offset target.');
2425
$table->json('recipients')->nullable();
2526
$table->text('when')->nullable();
2627
$table->enum('frequency_option', [

src/Actions/Action.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelMailator\Actions;
4+
5+
use Binarcode\LaravelMailator\Models\MailatorSchedule;
6+
7+
interface Action
8+
{
9+
public function handle(MailatorSchedule $schedule);
10+
}

src/Actions/SendMailAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Exception;
88
use Illuminate\Support\Facades\Mail;
99

10-
class SendMailAction
10+
class SendMailAction implements Action
1111
{
1212
public function handle(MailatorSchedule $schedule)
1313
{
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 BeforeConstraint implements SendScheduleConstraint
9+
{
10+
public function canSend(MailatorSchedule $schedule, Collection $logs): bool
11+
{
12+
if (! $schedule->isBefore()) {
13+
return true;
14+
}
15+
16+
if (is_null($schedule->timestamp_target)) {
17+
return true;
18+
}
19+
20+
// if already expired
21+
if ($schedule->timestamp_target->lte(now())) {
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+
}

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 = app(Config::get('mailator.scheduler.send_mail_action', SendMailAction::class));
35+
$sendMailAction = $this->schedule->action;
3636

3737
$sendMailAction->handle($this->schedule);
3838
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
4+
namespace Binarcode\LaravelMailator\Models\Concerns;
5+
6+
use Binarcode\LaravelMailator\Constraints\BeforeConstraint;
7+
use Binarcode\LaravelMailator\Constraints\SendScheduleConstraint;
8+
use Binarcode\LaravelMailator\Models\MailatorSchedule;
9+
10+
/**
11+
* Trait ConstraintsResolver
12+
* @mixin MailatorSchedule
13+
* @package Binarcode\LaravelMailator\Models\Concerns
14+
*/
15+
trait ConstraintsResolver
16+
{
17+
public function configurationsPasses(): bool
18+
{
19+
return collect([
20+
BeforeConstraint::class,
21+
])
22+
->map(fn ($class) => app($class))
23+
->every(fn (SendScheduleConstraint $event) => $event->canSend($this, $this->logs));
24+
}
25+
26+
public function whenPasses(): bool
27+
{
28+
return true;
29+
}
30+
31+
public function eventsPasses(): bool
32+
{
33+
return collect($this->constraints)
34+
->map(fn (string $event) => unserialize($event))
35+
->filter(fn ($event) => is_subclass_of($event, SendScheduleConstraint::class))
36+
->every(fn (SendScheduleConstraint $event) => $event->canSend($this, $this->logs));
37+
}
38+
}

0 commit comments

Comments
 (0)