mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
113 lines
3.7 KiB
PHP
113 lines
3.7 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\MainBundle\Notification\Email;
|
|
|
|
use Chill\MainBundle\Entity\Notification;
|
|
use Chill\MainBundle\Entity\NotificationComment;
|
|
use Doctrine\ORM\Event\LifecycleEventArgs;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
|
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
|
use Symfony\Component\Mailer\MailerInterface;
|
|
use Symfony\Component\Mime\Email;
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
|
|
class NotificationMailer
|
|
{
|
|
private LoggerInterface $logger;
|
|
|
|
private MailerInterface $mailer;
|
|
|
|
private TranslatorInterface $translator;
|
|
|
|
public function __construct(MailerInterface $mailer, LoggerInterface $logger, TranslatorInterface $translator)
|
|
{
|
|
$this->mailer = $mailer;
|
|
$this->logger = $logger;
|
|
$this->translator = $translator;
|
|
}
|
|
|
|
public function postPersistComment(NotificationComment $comment, LifecycleEventArgs $eventArgs): void
|
|
{
|
|
foreach (
|
|
array_merge(
|
|
$comment->getNotification()->getAddressees()->toArray(),
|
|
[$comment->getNotification()->getSender()]
|
|
) as $dest
|
|
) {
|
|
if (null === $dest->getEmail() || $comment->getCreatedBy() !== $dest) {
|
|
continue;
|
|
}
|
|
$email = new TemplatedEmail();
|
|
$email
|
|
->to($dest->getEmail())
|
|
->subject('Re: ' . $comment->getNotification()->getTitle())
|
|
->textTemplate('@ChillMain/Notification/email_notification_comment_persist.fr.md.twig')
|
|
->context([
|
|
'comment' => $comment,
|
|
'dest' => $dest,
|
|
]);
|
|
|
|
try {
|
|
$this->mailer->send($email);
|
|
} catch (TransportExceptionInterface $e) {
|
|
$this->logger->warning('[NotificationMailer] could not send an email notification about comment', [
|
|
'to' => $dest->getEmail(),
|
|
'error_message' => $e->getMessage(),
|
|
'error_trace' => $e->getTraceAsString(),
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Send a email after a notification is persisted.
|
|
*/
|
|
public function postPersistNotification(Notification $notification, LifecycleEventArgs $eventArgs): void
|
|
{
|
|
foreach ($notification->getAddressees() as $addressee) {
|
|
if (null === $addressee->getEmail()) {
|
|
continue;
|
|
}
|
|
|
|
$email = new Email();
|
|
$email
|
|
->subject($notification->getTitle());
|
|
|
|
if ($notification->isSystem()) {
|
|
$email
|
|
->text($notification->getMessage());
|
|
} else {
|
|
$email = new TemplatedEmail();
|
|
$email
|
|
->textTemplate('@ChillMain/Notification/email_non_system_notification_content.fr.md.twig')
|
|
->context([
|
|
'notification' => $notification,
|
|
'dest' => $addressee,
|
|
]);
|
|
}
|
|
|
|
$email->to($addressee->getEmail());
|
|
|
|
try {
|
|
$this->mailer->send($email);
|
|
} catch (TransportExceptionInterface $e) {
|
|
$this->logger->warning('[NotificationMailer] could not send an email notification', [
|
|
'to' => $addressee->getEmail(),
|
|
'error_message' => $e->getMessage(),
|
|
'error_trace' => $e->getTraceAsString(),
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|