mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,92 +1,79 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* 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 Symfony\Component\Mailer\MailerInterface;
|
||||
use Swift_Mailer;
|
||||
use Swift_Message;
|
||||
use Symfony\Component\Routing\RouterInterface;
|
||||
use Symfony\Component\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.
|
||||
*
|
||||
* @package Chill\MainBundle\Notification
|
||||
* Héberge toutes les méthodes pour ré-écrire les URL en fonction de la langue de l'utilisateur.
|
||||
*/
|
||||
class Mailer
|
||||
{
|
||||
/**
|
||||
* @var Swift_Mailer
|
||||
*/
|
||||
protected $forcedMailer;
|
||||
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
|
||||
/**
|
||||
* @var \Twig\Environment
|
||||
*/
|
||||
protected $twig;
|
||||
|
||||
/**
|
||||
* @var \Swift_Mailer
|
||||
* @var Swift_Mailer
|
||||
*/
|
||||
protected $mailer;
|
||||
|
||||
/**
|
||||
* @var \Swift_Mailer
|
||||
*/
|
||||
protected $forcedMailer;
|
||||
|
||||
/**
|
||||
* @var RouterInterface
|
||||
*/
|
||||
protected $router;
|
||||
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $routeParameters;
|
||||
|
||||
|
||||
/**
|
||||
* @var RouterInterface
|
||||
*/
|
||||
protected $router;
|
||||
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
/**
|
||||
* @var \Twig\Environment
|
||||
*/
|
||||
protected $twig;
|
||||
|
||||
/**
|
||||
* Mailer constructor.
|
||||
*
|
||||
* @param LoggerInterface $logger
|
||||
* @param Environment $twig
|
||||
* @param \Swift_Mailer $mailer
|
||||
* @param RouterInterface $router
|
||||
* @param TranslatorInterface $translator
|
||||
* @param $routeParameters
|
||||
*/
|
||||
public function __construct(
|
||||
LoggerInterface $logger,
|
||||
LoggerInterface $logger,
|
||||
Environment $twig,
|
||||
\Swift_Mailer $mailer,
|
||||
Swift_Mailer $mailer,
|
||||
// due to bug https://github.com/symfony/swiftmailer-bundle/issues/127
|
||||
// \Swift_Transport $mailerTransporter,
|
||||
RouterInterface $router,
|
||||
TranslatorInterface $translator,
|
||||
TranslatorInterface $translator,
|
||||
$routeParameters
|
||||
) {
|
||||
$this->logger = $logger;
|
||||
@@ -97,60 +84,40 @@ class Mailer
|
||||
$this->translator = $translator;
|
||||
$this->routeParameters = $routeParameters;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Envoie une notification à un utilisateur.
|
||||
*
|
||||
* @param \User $to
|
||||
* @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 $template
|
||||
*
|
||||
* @throws \Twig\Error\LoaderError
|
||||
* @throws \Twig\Error\RuntimeError
|
||||
* @throws \Twig\Error\SyntaxError
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function sendNotification(
|
||||
$recipient,
|
||||
array $subject,
|
||||
array $bodies,
|
||||
callable $callback = null,
|
||||
$force = false
|
||||
)
|
||||
public function renderContentToUser(User $to, $template, array $parameters = [])
|
||||
{
|
||||
$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
|
||||
);
|
||||
|
||||
$message = (new \Swift_Message($subjectI18n))
|
||||
->setFrom($fromEmail, $fromName)
|
||||
->setTo($to)
|
||||
;
|
||||
|
||||
foreach ($bodies as $contentType => $content) {
|
||||
$message->setBody($content, $contentType);
|
||||
}
|
||||
|
||||
if ($callback !== null) {
|
||||
\call_user_func($callback, $message);
|
||||
}
|
||||
|
||||
$this->logger->info("[notification] Sending notification", [
|
||||
'to' => $message->getTo(),
|
||||
'subject' => $message->getSubject()
|
||||
]);
|
||||
|
||||
$this->sendMessage($message, $force);
|
||||
$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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \Swift_Message $message
|
||||
* @param $force
|
||||
*
|
||||
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
|
||||
*/
|
||||
public function sendMessage(\Swift_Message $message, $force)
|
||||
public function sendMessage(Swift_Message $message, $force)
|
||||
{
|
||||
if ($force) {
|
||||
$this->forcedMailer->send($message);
|
||||
@@ -158,31 +125,50 @@ class Mailer
|
||||
$this->mailer->send($message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param User $to
|
||||
* @param $template
|
||||
* @param array $parameters
|
||||
* @return string
|
||||
* @throws \Twig\Error\LoaderError
|
||||
* @throws \Twig\Error\RuntimeError
|
||||
* @throws \Twig\Error\SyntaxError
|
||||
* 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 renderContentToUser(User $to, $template, array $parameters = array())
|
||||
{
|
||||
$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;
|
||||
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
|
||||
);
|
||||
|
||||
$message = (new Swift_Message($subjectI18n))
|
||||
->setFrom($fromEmail, $fromName)
|
||||
->setTo($to);
|
||||
|
||||
foreach ($bodies as $contentType => $content) {
|
||||
$message->setBody($content, $contentType);
|
||||
}
|
||||
|
||||
if (null !== $callback) {
|
||||
call_user_func($callback, $message);
|
||||
}
|
||||
|
||||
$this->logger->info('[notification] Sending notification', [
|
||||
'to' => $message->getTo(),
|
||||
'subject' => $message->getSubject(),
|
||||
]);
|
||||
|
||||
$this->sendMessage($message, $force);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user