mirror of
				https://gitlab.com/Chill-Projet/chill-bundles.git
				synced 2025-11-04 11:18:25 +00:00 
			
		
		
		
	Notification: send an email when a notification is created
This commit is contained in:
		@@ -0,0 +1,110 @@
 | 
			
		||||
<?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: [Chill] ' . $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;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if ($notification->isSystem()) {
 | 
			
		||||
                $email = new Email();
 | 
			
		||||
                $email
 | 
			
		||||
                    ->text($notification->getMessage())
 | 
			
		||||
                    ->subject('[Chill] ' . $notification->getTitle());
 | 
			
		||||
            } 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(),
 | 
			
		||||
                ]);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user