44
55namespace App \Command \Email ;
66
7+ use App \Entity \EmailDto \EmailDto ;
8+ use App \Service \EmailService ;
79use App \Service \StrategicEmailService ;
810use Symfony \Component \Console \Attribute \AsCommand ;
911use Symfony \Component \Console \Command \Command ;
@@ -17,40 +19,59 @@ class SendLoginReminderCommand extends Command
1719{
1820 public function __construct (
1921 private readonly StrategicEmailService $ strategicEmailService ,
22+ private readonly EmailService $ emailService ,
2023 ) {
2124 parent ::__construct ();
2225 }
2326
2427 protected function configure (): void
2528 {
2629 $ this ->addOption ('dry-run ' , 'd ' , InputOption::VALUE_NONE , 'Whether to send the email or not ' );
30+ $ this ->addOption ('test-email ' , 't ' , InputOption::VALUE_NONE , 'Send the test email to the test email address ' );
2731 }
2832
2933 protected function execute (InputInterface $ input , OutputInterface $ output ): int
3034 {
3135 $ io = new SymfonyStyle ($ input , $ output );
3236 $ dryRun = $ input ->getOption ('dry-run ' );
37+ $ testEmail = $ input ->getOption ('test-email ' );
3338
34- $ result = $ this ->strategicEmailService ->sendLoginReminderEmail ($ dryRun );
39+ if (!$ dryRun && $ testEmail ) {
40+ $ io ->error ('The --test-email option can only be used with the --dry-run option. ' );
3541
36- if (null !== $ result ) {
37- $ table = $ io ->createTable ();
38-
39- $ table ->setHeaderTitle ('Email to be sent ' );
40- $ table ->setHeaders (['Field ' , 'Value ' ]);
41- $ table ->addRow (['Subject ' , $ result ->getSubject ()]);
42- $ table ->addRow (['To ' , $ result ->getToAddress ()->toString ()]);
42+ return Command::FAILURE ;
43+ }
4344
44- $ bcc = implode (', ' , array_map (static fn ($ address ) => $ address ->toString (), $ result ->getBcc ()));
45- $ table ->addRow (['Bcc ' , $ bcc ]);
45+ $ target = ($ dryRun && $ testEmail )
46+ ? fn (EmailDto $ emailDto ) => $ this ->sendTestEmailDto ($ io , $ emailDto )
47+ : ($ dryRun
48+ ? fn (EmailDto $ emailDto ) => $ this ->printEmailDto ($ io , $ emailDto )
49+ : fn (EmailDto $ emailDto ) => $ this ->sendEmailDto ($ io , $ emailDto ));
4650
47- $ table ->addRow (['Kind ' , $ result ->getKind ()->value ]);
48- $ table ->addRow (['Content ' , $ result ->getText ()]);
49- $ table ->render ();
50- } else {
51- $ io ->success ('Emails have been sent successfully. ' );
52- }
51+ $ this ->strategicEmailService ->sendLoginReminderEmail ($ target );
5352
5453 return Command::SUCCESS ;
5554 }
55+
56+ private function printEmailDto (SymfonyStyle $ io , EmailDto $ emailDto ): void
57+ {
58+ $ io ->writeln ('Email to be sent: ' );
59+ $ io ->writeln ('Subject: ' .$ emailDto ->getSubject ());
60+ $ io ->writeln ('To: ' .$ emailDto ->getToAddress ()->toString ());
61+ $ io ->writeln ('Bcc: ' .implode (', ' , array_map (static fn ($ address ) => $ address ->toString (), $ emailDto ->getBcc ())));
62+ $ io ->writeln ('Kind: ' .$ emailDto ->getKind ()->value );
63+ $ io ->writeln ('Content: ' .$ emailDto ->getText ());
64+ }
65+
66+ private function sendEmailDto (SymfonyStyle $ io , EmailDto $ emailDto ): void
67+ {
68+ $ this ->emailService ->send ($ emailDto );
69+ $ io ->success ('Email sent successfully. ' );
70+ }
71+
72+ private function sendTestEmailDto (SymfonyStyle $ io , EmailDto $ emailDto ): void
73+ {
74+ $ this ->emailService ->sendToTest ($ emailDto );
75+ $ io ->success ('Test email sent successfully. ' );
76+ }
5677}
0 commit comments