Maillog
This commit is contained in:
68
src/Tech/Entity/EmailLog.php
Normal file
68
src/Tech/Entity/EmailLog.php
Normal 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;
|
||||
}
|
||||
}
|
||||
54
src/Tech/EventListener/EmailLoggerListener.php
Normal file
54
src/Tech/EventListener/EmailLoggerListener.php
Normal 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();
|
||||
}
|
||||
}
|
||||
18
src/Tech/Repository/EmailLogRepository.php
Normal file
18
src/Tech/Repository/EmailLogRepository.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user