55namespace Stackkit \LaravelDatabaseEmails ;
66
77use Illuminate \Console \Command ;
8- use Illuminate \Support \Arr ;
9- use Illuminate \Support \LazyCollection ;
108use Throwable ;
119
1210class SendEmailsCommand extends Command
@@ -20,43 +18,28 @@ public function handle(Store $store): void
2018 $ emails = $ store ->getQueue ();
2119
2220 if ($ emails ->isEmpty ()) {
23- $ this ->line ('There is nothing to send. ' );
21+ $ this ->components -> info ('There is nothing to send. ' );
2422
2523 return ;
2624 }
2725
28- $ progress = $ this ->output -> createProgressBar ( $ emails-> count () );
26+ $ this ->components -> info ( ' Sending ' . count ( $ emails). ' e-mail(s). ' );
2927
3028 foreach ($ emails as $ email ) {
31- $ progress ->advance ();
29+ $ recipients = implode (', ' , array_keys ($ email ->recipient ));
30+ $ line = str ($ email ->subject )->limit (40 ).' - ' .str ($ recipients )->limit (40 );
3231
33- rescue (
34- callback: fn () => $ email ->send (),
35- rescue: fn (Throwable $ e ) => $ email ->markAsFailed ($ e )
36- );
37- }
32+ rescue (function () use ($ email , $ line ) {
33+ $ email ->send ();
3834
39- $ progress ->finish ();
35+ $ this ->components ->twoColumnDetail ($ line , '<fg=green;options=bold>DONE</> ' );
36+ }, function (Throwable $ e ) use ($ email , $ line ) {
37+ $ email ->markAsFailed ($ e );
4038
41- $ this ->result ($ emails );
42- }
39+ $ this ->components ->twoColumnDetail ($ line , '<fg=red;options=bold>FAIL</> ' );
40+ });
41+ }
4342
44- /**
45- * Output a table with the cronjob result.
46- */
47- protected function result (LazyCollection $ emails ): void
48- {
49- $ headers = ['ID ' , 'Recipient ' , 'Subject ' , 'Status ' ];
50-
51- $ this ->line ("\n" );
52-
53- $ this ->table ($ headers , $ emails ->map (function (Email $ email ) {
54- return [
55- $ email ->id ,
56- implode (', ' , array_column (Arr::wrap ($ email ->recipient ), 'recipient ' )),
57- $ email ->subject ,
58- $ email ->failed ? 'Failed ' : 'OK ' ,
59- ];
60- }));
43+ $ this ->newLine ();
6144 }
6245}
0 commit comments