33namespace Binarcode \LaravelMailator \Models ;
44
55use Binarcode \LaravelMailator \Actions \Action ;
6+ use Binarcode \LaravelMailator \Actions \ResolveGarbageAction ;
67use Binarcode \LaravelMailator \Constraints \SendScheduleConstraint ;
78use Binarcode \LaravelMailator \Jobs \SendMailJob ;
89use Binarcode \LaravelMailator \Models \Builders \MailatorSchedulerBuilder ;
910use Binarcode \LaravelMailator \Models \Concerns \ConstraintsResolver ;
1011use Binarcode \LaravelMailator \Models \Concerns \HasFuture ;
1112use Binarcode \LaravelMailator \Models \Concerns \HasTarget ;
1213use Binarcode \LaravelMailator \Support \ClassResolver ;
14+ use Binarcode \LaravelMailator \Support \ConverterEnum ;
1315use Carbon \Carbon ;
1416use Carbon \CarbonInterface ;
1517use Closure ;
1618use Exception ;
1719use Illuminate \Contracts \Mail \Mailable ;
1820use Illuminate \Database \Eloquent \Model ;
21+ use Illuminate \Database \Eloquent \Relations \HasMany ;
1922use Illuminate \Support \Arr ;
2023use Illuminate \Support \Facades \Validator ;
2124use Illuminate \Support \Str ;
@@ -55,24 +58,6 @@ public function getTable()
5558 return config ('mailator.schedulers_table_name ' , 'mailator_schedulers ' );
5659 }
5760
58- public const MINUTES_IN_HOUR = 60 ;
59- public const MINUTES_IN_DAY = 60 * 60 ;
60- public const MINUTES_IN_WEEK = 168 * 60 ;
61- public const HOURS_IN_DAY = 24 ;
62- public const HOURS_IN_WEEK = 168 ;
63-
64- public const FREQUENCY_IN_HOURS = [
65- 'single ' => PHP_INT_MAX ,
66- 'hourly ' => 1 ,
67- 'daily ' => self ::HOURS_IN_DAY ,
68- 'weekly ' => self ::HOURS_IN_WEEK ,
69- ];
70-
71- const DELAY_OPTIONS = [
72- '24 ' => 'Days ' ,
73- '168 ' => 'Weeks ' ,
74- ];
75-
7661 public const TIME_FRAME_ORIGIN_BEFORE = 'before ' ;
7762 public const TIME_FRAME_ORIGIN_AFTER = 'after ' ;
7863
@@ -169,14 +154,14 @@ public function hourly(): self
169154 return $ this ;
170155 }
171156
172- public function daily ()
157+ public function daily (): static
173158 {
174159 $ this ->frequency_option = static ::FREQUENCY_OPTIONS_DAILY ;
175160
176161 return $ this ;
177162 }
178163
179- public function weekly ()
164+ public function weekly (): static
180165 {
181166 $ this ->frequency_option = static ::FREQUENCY_OPTIONS_WEEKLY ;
182167
@@ -256,12 +241,12 @@ public function toDays(): int
256241 {
257242 //let's say we have 1 day and 2 hours till day job ends
258243 //so we will floor it to 1, and will send the reminder in time
259- return (int ) floor ($ this ->delay_minutes / static ::MINUTES_IN_DAY );
244+ return (int ) floor ($ this ->delay_minutes / ConverterEnum ::MINUTES_IN_DAY );
260245 }
261246
262247 public function toHours (): int
263248 {
264- return (int ) floor ($ this ->delay_minutes / static ::MINUTES_IN_HOUR );
249+ return (int ) floor ($ this ->delay_minutes / ConverterEnum ::MINUTES_IN_HOUR );
265250 }
266251
267252 public function minutes (int $ number ): self
@@ -273,21 +258,21 @@ public function minutes(int $number): self
273258
274259 public function hours (int $ number ): self
275260 {
276- $ this ->delay_minutes = $ number * static ::MINUTES_IN_HOUR ;
261+ $ this ->delay_minutes = $ number * ConverterEnum ::MINUTES_IN_HOUR ;
277262
278263 return $ this ;
279264 }
280265
281266 public function days (int $ number ): self
282267 {
283- $ this ->delay_minutes = $ number * static ::MINUTES_IN_DAY ;
268+ $ this ->delay_minutes = $ number * ConverterEnum ::MINUTES_IN_DAY ;
284269
285270 return $ this ;
286271 }
287272
288- public function weeks (int $ number )
273+ public function weeks (int $ number ): static
289274 {
290- $ this ->delay_minutes = $ number * static ::MINUTES_IN_WEEK ;
275+ $ this ->delay_minutes = $ number * ConverterEnum ::MINUTES_IN_WEEK ;
291276
292277 return $ this ;
293278 }
@@ -319,7 +304,7 @@ public function when(Closure $closure): self
319304 return $ this ;
320305 }
321306
322- public function logs ()
307+ public function logs (): HasMany
323308 {
324309 return $ this ->hasMany (MailatorLog::class, 'mailator_schedule_id ' );
325310 }
@@ -329,10 +314,13 @@ public function shouldSend(): bool
329314 try {
330315 $ this ->load ('logs ' );
331316
317+
332318 return $ this ->configurationsPasses () && $ this ->whenPasses () && $ this ->eventsPasses ();
333319 } catch (Exception | Throwable $ e ) {
334320 $ this ->markAsFailed ($ e ->getMessage ());
335321
322+ app (ResolveGarbageAction::class)->handle ($ this );
323+
336324 return false ;
337325 }
338326 }
@@ -480,4 +468,16 @@ public function markComplete(): self
480468
481469 return $ this ;
482470 }
471+
472+ public function failedLastTimes (int $ times ): bool
473+ {
474+ return $ this
475+ ->logs ()
476+ ->latest ()
477+ ->take ($ times )
478+ ->get ()
479+ ->filter
480+ ->isFailed ()
481+ ->count () === $ times ;
482+ }
483483}
0 commit comments