5
5
namespace Stackkit \LaravelDatabaseEmails ;
6
6
7
7
use Illuminate \Console \Command ;
8
- use Illuminate \Support \Arr ;
9
- use Illuminate \Support \LazyCollection ;
10
8
use Throwable ;
11
9
12
10
class SendEmailsCommand extends Command
@@ -20,43 +18,28 @@ public function handle(Store $store): void
20
18
$ emails = $ store ->getQueue ();
21
19
22
20
if ($ emails ->isEmpty ()) {
23
- $ this ->line ('There is nothing to send. ' );
21
+ $ this ->components -> info ('There is nothing to send. ' );
24
22
25
23
return ;
26
24
}
27
25
28
- $ progress = $ this ->output -> createProgressBar ( $ emails-> count () );
26
+ $ this ->components -> info ( ' Sending ' . count ( $ emails). ' e-mail(s). ' );
29
27
30
28
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 );
32
31
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 ();
38
34
39
- $ progress ->finish ();
35
+ $ this ->components ->twoColumnDetail ($ line , '<fg=green;options=bold>DONE</> ' );
36
+ }, function (Throwable $ e ) use ($ email , $ line ) {
37
+ $ email ->markAsFailed ($ e );
40
38
41
- $ this ->result ($ emails );
42
- }
39
+ $ this ->components ->twoColumnDetail ($ line , '<fg=red;options=bold>FAIL</> ' );
40
+ });
41
+ }
43
42
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 ();
61
44
}
62
45
}
0 commit comments