Skip to content

Commit

Permalink
changes based en comments #3260
Browse files Browse the repository at this point in the history
  • Loading branch information
hmeneuvrier committed Nov 21, 2024
1 parent 249f627 commit 2467d37
Show file tree
Hide file tree
Showing 81 changed files with 519 additions and 533 deletions.
2 changes: 1 addition & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ when@test:
$parameterBag: '@Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface'
$logger: '@Psr\Log\LoggerInterface'
$urlGenerator: '@Symfony\Component\Routing\Generator\UrlGeneratorInterface'
$entityManager: '@Doctrine\ORM\EntityManagerInterface'
$failedEmailManager: '@App\Manager\FailedEmailManager'
$userManager: '@App\Manager\UserManager'
tags:
- name: 'app.notification_mailer'
30 changes: 7 additions & 23 deletions migrations/Version20241112145634.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,24 @@ public function up(Schema $schema): void
id INT AUTO_INCREMENT NOT NULL,
type VARCHAR(50) NOT NULL,
to_email JSON NOT NULL,
from_email VARCHAR(255) DEFAULT NULL,
from_fullname VARCHAR(255) DEFAULT NULL,
params JSON NOT NULL,
message TEXT DEFAULT NULL,
territory_id INT DEFAULT NULL,
user_id INT DEFAULT NULL,
signalement_id INT DEFAULT NULL,
signalement_draft_id INT DEFAULT NULL,
suivi_id INT DEFAULT NULL,
intervention_id INT DEFAULT NULL,
previous_visite_date DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\',
attachment JSON DEFAULT NULL,
motif VARCHAR(255) DEFAULT NULL,
cron_label VARCHAR(255) DEFAULT NULL,
cron_count INT DEFAULT NULL,
from_email VARCHAR(255) NOT NULL,
from_fullname VARCHAR(255) NOT NULL,
reply_to VARCHAR(255) NOT NULL,
subject TEXT NOT NULL,
context JSON NOT NULL,
notify_usager TINYINT(1) DEFAULT 0 NOT NULL,
error_message TEXT DEFAULT NULL,
created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\',
is_resend_successful TINYINT(1) DEFAULT 0 NOT NULL,
retry_count INT DEFAULT 0,
last_attempt_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\',
PRIMARY KEY(id),
CONSTRAINT FK_failed_email_territory_id FOREIGN KEY (territory_id) REFERENCES territory (id),
CONSTRAINT FK_failed_email_user_id FOREIGN KEY (user_id) REFERENCES user (id),
CONSTRAINT FK_failed_email_signalement_id FOREIGN KEY (signalement_id) REFERENCES signalement (id),
CONSTRAINT FK_failed_email_suivi_id FOREIGN KEY (suivi_id) REFERENCES suivi (id),
CONSTRAINT FK_failed_email_signalement_draft_id FOREIGN KEY (signalement_draft_id) REFERENCES signalement_draft (id),
CONSTRAINT FK_failed_email_intervention_id FOREIGN KEY (intervention_id) REFERENCES intervention (id)
PRIMARY KEY(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
');
}

public function down(Schema $schema): void
{
$this->addSql('DROP TABLE failed_emails');
$this->addSql('DROP TABLE failed_email');
}
}
75 changes: 48 additions & 27 deletions src/Command/RetryFailedEmailsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
namespace App\Command;

use App\Entity\FailedEmail;
use App\Service\Mailer\NotificationMail;
use App\Service\Mailer\NotificationMailerRegistry;
use App\Repository\FailedEmailRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Mailer\Header\TagHeader;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;

#[AsCommand(
name: 'app:retry-failed-emails',
Expand All @@ -20,7 +23,8 @@ class RetryFailedEmailsCommand extends Command
{
public function __construct(
private EntityManagerInterface $entityManager,
private NotificationMailerRegistry $notificationMailerRegistry,
private FailedEmailRepository $failedEmailRepository,
private MailerInterface $mailer,
) {
parent::__construct();
}
Expand All @@ -30,36 +34,53 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io = new SymfonyStyle($input, $output);

/** @var FailedEmail[] $failedEmails */
$failedEmails = $this->entityManager->getRepository(FailedEmail::class)->findBy(['isResendSuccessful' => false]);
$failedEmails = $this->failedEmailRepository->findBy(['isResendSuccessful' => false]);

foreach ($failedEmails as $failedEmail) {
/** @var FailedEmail $failedEmail */
$notificationMail = new NotificationMail(
type: constant('App\Service\Mailer\NotificationMailerType::'.$failedEmail->getType()),
to: $failedEmail->getToEmail(),
fromEmail: $failedEmail->getFromEmail(),
fromFullname: $failedEmail->getFromFullname(),
params: $failedEmail->getParams(),
signalement: $failedEmail->getSignalement(),
suivi: $failedEmail->getSuivi(),
signalementDraft: $failedEmail->getSignalementDraft(),
message: $failedEmail->getMessage(),
territory: $failedEmail->getTerritory(),
user: $failedEmail->getUser(),
intervention: $failedEmail->getIntervention(),
previousVisiteDate: $failedEmail->getPreviousVisiteDate(),
attachment: $failedEmail->getAttachment(),
motif: $failedEmail->getMotif(),
cronLabel: $failedEmail->getCronLabel(),
cronCount: $failedEmail->getCronCount(),
);
$emailMessage = (new TemplatedEmail())
->htmlTemplate('emails/'.$failedEmail->getContext()['template'].'.html.twig')
->context($failedEmail->getContext())
->replyTo($failedEmail->getReplyTo())
->subject($failedEmail->getSubject())
->from(
new Address(
$failedEmail->getFromEmail(),
$failedEmail->getFromFullname()
)
)
;

$success = $this->notificationMailerRegistry->send($notificationMail, false);
foreach ($failedEmail->getToEmail() as $toEmail) {
$toEmail && $emailMessage->addTo($toEmail);
}
if (
\array_key_exists('tagHeader', $failedEmail->getContext())
&& null !== $failedEmail->getContext()['tagHeader']
) {
$emailMessage->getHeaders()->add(new TagHeader($failedEmail->getContext()['tagHeader']));
}
if (\array_key_exists('attach', $failedEmail->getContext())) {
if (\is_array($failedEmail->getContext()['attach'])) {
foreach ($failedEmail->getContext()['attach'] as $attachPath) {
$emailMessage->attachFromPath($attachPath);
}
} else {
$emailMessage->attachFromPath($failedEmail->getContext()['attach']);
}
}
if (\array_key_exists('attachContent', $failedEmail->getContext())) {
$emailMessage->attach(
$failedEmail->getContext()['attachContent']['content'],
$failedEmail->getContext()['attachContent']['filename']
);
}

try {
$this->mailer->send($emailMessage);

if ($success) {
$io->success(sprintf('E-mail envoyé à %s', implode(', ', $failedEmail->getToEmail())));
$failedEmail->setResendSuccessful(true);
} else {
} catch (\Throwable $exception) {
$io->error(sprintf('E-mail non envoyé à %s', implode(', ', $failedEmail->getToEmail())));
$failedEmail->setRetryCount($failedEmail->getRetryCount() + 1);
$failedEmail->setLastAttemptAt(new \DateTimeImmutable());
Expand Down
Loading

0 comments on commit 2467d37

Please sign in to comment.