This commit is contained in:
Frank
2026-01-03 22:35:56 +01:00
parent 5b6bfaf5ad
commit c8b0a6e966
5 changed files with 173 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20260103212025 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE email_log (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, email_identifier VARCHAR(255) NOT NULL, sent_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_6FB4883A76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE email_log ADD CONSTRAINT FK_6FB4883A76ED395 FOREIGN KEY (user_id) REFERENCES `user` (id)');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE email_log DROP FOREIGN KEY FK_6FB4883A76ED395');
$this->addSql('DROP TABLE email_log');
}
}

View File

Before

Width:  |  Height:  |  Size: 1.5 MiB

After

Width:  |  Height:  |  Size: 1.5 MiB

View File

@@ -0,0 +1,68 @@
<?php
namespace App\Tech\Entity;
use App\Tech\Repository\EmailLogRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: EmailLogRepository::class)]
#[ORM\Table(name: 'email_log')]
class EmailLog
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null;
#[ORM\Column(length: 255)]
private ?string $emailIdentifier = null;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
private ?\DateTimeImmutable $sentAt = null;
public function getId(): ?int
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): static
{
$this->user = $user;
return $this;
}
public function getEmailIdentifier(): ?string
{
return $this->emailIdentifier;
}
public function setEmailIdentifier(string $emailIdentifier): static
{
$this->emailIdentifier = $emailIdentifier;
return $this;
}
public function getSentAt(): ?\DateTimeImmutable
{
return $this->sentAt;
}
public function setSentAt(\DateTimeImmutable $sentAt): static
{
$this->sentAt = $sentAt;
return $this;
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace App\Tech\EventListener;
use App\Tech\Entity\EmailLog;
use App\Tech\Entity\User;
use App\Tech\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mime\Address;
#[AsEventListener(event: MessageEvent::class, method: 'onMessage')]
class EmailLoggerListener
{
public function __construct(
private EntityManagerInterface $entityManager,
private UserRepository $userRepository
) {
}
public function onMessage(MessageEvent $event): void
{
$message = $event->getMessage();
if (!$message instanceof TemplatedEmail) {
return;
}
$recipients = $message->getTo();
foreach ($recipients as $recipient) {
if (!$recipient instanceof Address) {
continue;
}
$user = $this->userRepository->findOneBy(['email' => $recipient->getAddress()]);
if (!$user) {
continue;
}
$emailLog = new EmailLog();
$emailLog->setUser($user);
$emailLog->setSentAt(new \DateTimeImmutable());
// Try to get the template name, or use the subject as identifier
$identifier = $message->getHtmlTemplate() ?: $message->getTextTemplate() ?: $message->getSubject();
$emailLog->setEmailIdentifier($identifier);
$this->entityManager->persist($emailLog);
}
$this->entityManager->flush();
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace App\Tech\Repository;
use App\Tech\Entity\EmailLog;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<EmailLog>
*/
class EmailLogRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, EmailLog::class);
}
}