mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
157 lines
4.0 KiB
PHP
157 lines
4.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* 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.
|
|
*/
|
|
|
|
namespace Chill\MainBundle\Notification;
|
|
|
|
use Chill\MainBundle\Entity\User;
|
|
use Psr\Log\LoggerInterface;
|
|
use Swift_Mailer;
|
|
use Swift_Message;
|
|
use Symfony\Component\Mailer\MailerInterface;
|
|
use Symfony\Component\Mime\Email;
|
|
use Symfony\Component\Routing\RouterInterface;
|
|
use Symfony\Component\Templating\EngineInterface;
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
use Twig\Environment;
|
|
|
|
use function call_user_func;
|
|
|
|
/**
|
|
* Class Mailer
|
|
* Classe d'aide pour l'envoi de notification.
|
|
*
|
|
* Héberge toutes les méthodes pour ré-écrire les URL en fonction de la langue de l'utilisateur.
|
|
*
|
|
* @deprecated use the MailerInterface
|
|
*/
|
|
class Mailer
|
|
{
|
|
/**
|
|
* @var LoggerInterface
|
|
*/
|
|
private $logger;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
private $routeParameters;
|
|
|
|
/**
|
|
* @var RouterInterface
|
|
*/
|
|
private $router;
|
|
|
|
/**
|
|
* @var TranslatorInterface
|
|
*/
|
|
private $translator;
|
|
|
|
private EngineInterface $twig;
|
|
|
|
private MailerInterface $mailer;
|
|
|
|
/**
|
|
* Mailer constructor.
|
|
*
|
|
* @param $routeParameters
|
|
*/
|
|
public function __construct(
|
|
MailerInterface $mailer,
|
|
LoggerInterface $logger,
|
|
EngineInterface $twig,
|
|
RouterInterface $router,
|
|
TranslatorInterface $translator,
|
|
$routeParameters
|
|
) {
|
|
$this->logger = $logger;
|
|
$this->twig = $twig;
|
|
$this->mailer = $mailer;
|
|
$this->router = $router;
|
|
$this->translator = $translator;
|
|
$this->routeParameters = $routeParameters;
|
|
}
|
|
|
|
/**
|
|
* @param $template
|
|
*
|
|
* @throws \Twig\Error\LoaderError
|
|
* @throws \Twig\Error\RuntimeError
|
|
* @throws \Twig\Error\SyntaxError
|
|
*
|
|
* @return string
|
|
*/
|
|
public function renderContentToUser(User $to, $template, array $parameters = [])
|
|
{
|
|
$context = $this->router->getContext();
|
|
$previousHost = $context->getHost();
|
|
$previousScheme = $context->getScheme();
|
|
|
|
$context->setHost($this->routeParameters['host']);
|
|
$context->setScheme($this->routeParameters['scheme']);
|
|
|
|
$content = $this->twig->render($template, $parameters);
|
|
|
|
// reset the host
|
|
$context->setHost($previousHost);
|
|
$context->setScheme($previousScheme);
|
|
|
|
return $content;
|
|
}
|
|
|
|
/**
|
|
* Envoie une notification à un utilisateur.
|
|
*
|
|
* @param array $subject Subject of the message [ 0 => $message (required), 1 => $parameters (optional), 3 => $domain (optional) ]
|
|
* @param array $bodies The bodies. An array where keys are the contentType and values the bodies
|
|
* @param callable $callback a callback to customize the message (add attachment, etc.)
|
|
* @param mixed $recipient
|
|
* @param mixed $force
|
|
*/
|
|
public function sendNotification(
|
|
$recipient,
|
|
array $subject,
|
|
array $bodies,
|
|
?callable $callback = null,
|
|
$force = false
|
|
) {
|
|
$fromEmail = $this->routeParameters['from_email'];
|
|
$fromName = $this->routeParameters['from_name'];
|
|
$to = $recipient instanceof User ? $recipient->getEmail() : $recipient;
|
|
|
|
$subjectI18n = $this->translator->trans(
|
|
$subject[0],
|
|
$subject[1] ?? [],
|
|
$subject[2] ?? null
|
|
);
|
|
|
|
$email = new Email();
|
|
$email->addTo($to)->subject($subjectI18n);
|
|
|
|
foreach ($bodies as $contentType => $content) {
|
|
match ($contentType) {
|
|
'text/plain' => $email->text($content),
|
|
default => $email->text($content),
|
|
};
|
|
}
|
|
|
|
if (null !== $callback) {
|
|
call_user_func($callback, $email);
|
|
}
|
|
|
|
$this->logger->info('[notification] Sending notification', [
|
|
'to' => $email->getTo(),
|
|
'subject' => $email->getSubject()
|
|
]);
|
|
|
|
$this->mailer->send($email);
|
|
}
|
|
}
|