Skip to content
This repository was archived by the owner on Oct 15, 2025. It is now read-only.

Commit 6b16695

Browse files
committed
refactor(controller): Make StrategicEmailService more flexible
1 parent 71171c0 commit 6b16695

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ SQLRUNNER_URL=http://sqlrunner.app-sf.orb.local:8080
7070

7171
# Email
7272
SERVER_EMAIL=no-reply@dbplay.pan93.com
73+
SERVER_EMAIL_FOR_TEST=dbplay@pan93.com

src/Command/Email/SendLoginReminderCommand.php

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace App\Command\Email;
66

7+
use App\Entity\EmailDto\EmailDto;
8+
use App\Service\EmailService;
79
use App\Service\StrategicEmailService;
810
use Symfony\Component\Console\Attribute\AsCommand;
911
use 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
}

src/Service/StrategicEmailService.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,17 @@
1212
public function __construct(
1313
private StatisticsService $statisticsService,
1414
private EmailTemplateService $emailTemplateService,
15-
private EmailService $emailService,
1615
) {}
1716

1817
/**
1918
* Send login reminder emails to users who have not logged in for a long time.
2019
*
21-
* @template T of bool
22-
*
23-
* @param T $dryRun whether to send the email or not
24-
*
25-
* @return ($dryRun is true ? EmailDto : null)
20+
* @param callable(EmailDto): void $target the function that sends the email
21+
* according to the given EmailDto
2622
*
2723
* @throws \Throwable if the email content cannot be rendered
2824
*/
29-
public function sendLoginReminderEmail(bool $dryRun = false): ?EmailDto
25+
public function sendLoginReminderEmail(callable $target): void
3026
{
3127
$lastLoginAt = array_filter(
3228
// Filter out users who have logged in within the last 7 days
@@ -47,12 +43,7 @@ static function (LastLoginDto $lastLoginDto): bool {
4743
);
4844

4945
$emailDto = $this->emailTemplateService->createLoginReminderDto($bccUsers);
50-
if ($dryRun) {
51-
return $emailDto;
52-
}
53-
54-
$this->emailService->send($emailDto);
5546

56-
return null;
47+
$target($emailDto);
5748
}
5849
}

0 commit comments

Comments
 (0)