22
33namespace Binarcode \LaravelMailator \Models ;
44
5+ use App \Domains \ResultDocuments \Models \ResultDocument ;
56use Binarcode \LaravelMailator \Actions \Action ;
67use Binarcode \LaravelMailator \Actions \ResolveGarbageAction ;
78use Binarcode \LaravelMailator \Actions \RunSchedulersAction ;
3233/**
3334 * Class MailatorSchedule.
3435 *
35- * @property string tags
36- * @property string name
37- * @property string stopable
38- * @property string targetable_type
39- * @property string targetable_id
40- * @property string mailable_class
41- * @property string delay_minutes
42- * @property string time_frame_origin
43- * @property array constraints
44- * @property Carbon timestamp_target
45- * @property array recipients
46- * @property string action
47- * @property Closure when
48- * @property Carbon last_failed_at
49- * @property Carbon last_sent_at
50- * @property Carbon completed_at
51- * @property string frequency_option
36+ * @property string $tags
37+ * @property string $name
38+ * @property string $stopable
39+ * @property string $unique
40+ * @property string $targetable_type
41+ * @property string $targetable_id
42+ * @property string $mailable_class
43+ * @property string $delay_minutes
44+ * @property string $time_frame_origin
45+ * @property array $constraints
46+ * @property Carbon $timestamp_target
47+ * @property array $recipients
48+ * @property string $action
49+ * @property Closure $when
50+ * @property Carbon $last_failed_at
51+ * @property Carbon $last_sent_at
52+ * @property Carbon $completed_at
53+ * @property string $frequency_option
5254 * @method static MailatorSchedulerBuilder query()
5355 */
5456class MailatorSchedule extends Model
@@ -88,6 +90,7 @@ public function getTable()
8890 'end_at ' => 'datetime ' ,
8991 'completed_at ' => 'datetime ' ,
9092 'stopable ' => 'boolean ' ,
93+ 'unique ' => 'boolean ' ,
9194 ];
9295
9396 protected $ attributes = [
@@ -103,8 +106,8 @@ public function mailable(Mailable $mailable): self
103106 {
104107 if ($ mailable instanceof Constraintable) {
105108 collect ($ mailable ->constraints ())
106- ->filter (fn ($ constraint ) => $ constraint instanceof SendScheduleConstraint)
107- ->each (fn (SendScheduleConstraint $ constraint ) => $ this ->constraint ($ constraint ));
109+ ->filter (fn ($ constraint ) => $ constraint instanceof SendScheduleConstraint)
110+ ->each (fn (SendScheduleConstraint $ constraint ) => $ this ->constraint ($ constraint ));
108111 }
109112
110113 $ this ->mailable_class = serialize ($ mailable );
@@ -183,6 +186,18 @@ public function isStopable(): bool
183186 return (bool ) $ this ->stopable ;
184187 }
185188
189+ public function unique (): self
190+ {
191+ $ this ->unique = true ;
192+
193+ return $ this ;
194+ }
195+
196+ public function isUnique (): bool
197+ {
198+ return (bool ) $ this ->unique ;
199+ }
200+
186201 public function after (CarbonInterface $ date = null ): self
187202 {
188203 $ this ->time_frame_origin = static ::TIME_FRAME_ORIGIN_AFTER ;
@@ -303,7 +318,7 @@ public function recipients(...$recipients): self
303318 {
304319 $ this ->recipients = array_merge (collect ($ recipients )
305320 ->flatten ()
306- ->filter (fn ($ email ) => $ this ->ensureValidEmail ($ email ))
321+ ->filter (fn ($ email ) => $ this ->ensureValidEmail ($ email ))
307322 ->unique ()
308323 ->toArray (), $ this ->recipients ?? []);
309324
@@ -329,15 +344,15 @@ public function shouldSend(): bool
329344 try {
330345 $ this ->load ('logs ' );
331346
332- if (! $ this ->configurationsPasses ()) {
347+ if (!$ this ->configurationsPasses ()) {
333348 return false ;
334349 }
335350
336- if (! $ this ->whenPasses ()) {
351+ if (!$ this ->whenPasses ()) {
337352 return false ;
338353 }
339354
340- if (! $ this ->eventsPasses ()) {
355+ if (!$ this ->eventsPasses ()) {
341356 if ($ this ->isStopable ()) {
342357 $ this ->markComplete ();
343358 }
@@ -394,7 +409,7 @@ public static function run(): void
394409
395410 public function hasCustomAction (): bool
396411 {
397- return ! is_null ($ this ->action );
412+ return !is_null ($ this ->action );
398413 }
399414
400415 public function getMailable (): ?Mailable
@@ -449,13 +464,13 @@ public function markAsFailed(string $failureReason): self
449464 public function getRecipients (): array
450465 {
451466 return collect ($ this ->recipients )
452- ->filter (fn ($ email ) => $ this ->ensureValidEmail ($ email ))
467+ ->filter (fn ($ email ) => $ this ->ensureValidEmail ($ email ))
453468 ->toArray ();
454469 }
455470
456471 protected function ensureValidEmail (string $ email ): bool
457472 {
458- return ! Validator::make (
473+ return !Validator::make (
459474 compact ('email ' ),
460475 ['email ' => 'required|email ' ]
461476 )->fails ();
@@ -468,7 +483,7 @@ public function actionClass(Action $action): self
468483 return $ this ;
469484 }
470485
471- public function tag (string | array $ tag ): self
486+ public function tag (string | array $ tag ): self
472487 {
473488 if (is_array ($ tag )) {
474489 $ tag = implode (', ' , $ tag );
@@ -519,7 +534,7 @@ public function markComplete(): self
519534
520535 public function isCompleted (): bool
521536 {
522- return ! is_null ($ this ->completed_at );
537+ return !is_null ($ this ->completed_at );
523538 }
524539
525540 public function failedLastTimes (int $ times ): bool
@@ -541,16 +556,36 @@ public function timestampTarget(): ?CarbonInterface
541556
542557 public function isRepetitive (): bool
543558 {
544- return ! $ this ->isOnce ();
559+ return !$ this ->isOnce ();
545560 }
546561
547562 public function wasSentOnce (): bool
548563 {
549- return ! is_null ($ this ->last_sent_at );
564+ return !is_null ($ this ->last_sent_at );
550565 }
551566
552567 public function getConstraints (): ConstraintsCollection
553568 {
554569 return ConstraintsCollection::make ($ this ->constraints );
555570 }
571+
572+ public function save (array $ options = [])
573+ {
574+ if (!$ this ->isUnique ()) {
575+ return parent ::save ($ options );
576+ }
577+
578+ $ mailable = get_class (unserialize ($ this ->mailable_class ));
579+
580+ $ exists = static :: targetableType ($ this ->targetable_type )
581+ ->targetableId ($ this ->targetable_id )
582+ ->mailableClass ($ mailable )
583+ ->exists ();
584+
585+ if ($ exists ) {
586+ return false ;
587+ }
588+
589+ return parent ::save ($ options );
590+ }
556591}
0 commit comments