From 472253989fbdf24fb56b66607fa13eac678b8f00 Mon Sep 17 00:00:00 2001 From: johnkrovitch Date: Sun, 19 Jul 2020 21:57:41 +0200 Subject: [PATCH] Fix the email sending when a new comment is added and improve the mail parameters naming --- src/DependencyInjection/Configuration.php | 4 +++- src/DependencyInjection/JKCmsExtension.php | 7 +++--- src/Entity/Article.php | 7 ++++++ src/Form/Handler/AddCommentHandler.php | 25 ++++++++++++--------- src/Resources/config/services/handlers.yaml | 3 ++- tests/CmsBundle/Fixtures/config/config.yaml | 3 +++ 6 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 2502146..2790f80 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -81,9 +81,11 @@ private function configureEmailNode(NodeBuilder $builder): void $builder ->arrayNode('email') ->addDefaultsIfNotSet() + ->isRequired() ->children() ->scalarNode('base_template')->defaultValue('@JKCms/Mail/base.html.twig')->end() - ->scalarNode('contact_email')->defaultValue('admin@admin.com')->end() + ->scalarNode('from')->defaultValue('admin@admin.com')->cannotBeEmpty()->end() + ->scalarNode('to')->defaultValue('admin@admin.com')->cannotBeEmpty()->end() ->end() ->end() ; diff --git a/src/DependencyInjection/JKCmsExtension.php b/src/DependencyInjection/JKCmsExtension.php index 98f4210..92c9caf 100644 --- a/src/DependencyInjection/JKCmsExtension.php +++ b/src/DependencyInjection/JKCmsExtension.php @@ -26,9 +26,6 @@ public function load(array $configs, ContainerBuilder $container) $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); - $container->setParameter('jk_cms.scripts.template', $accessor->getValue($config, '[scripts][template]')); - $container->setParameter('jk_cms.contact_email', $accessor->getValue($config, '[email][contact_email]')); - $siteKey = ''; $siteSecret = ''; @@ -44,6 +41,10 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter('jk_cms.front_base', $accessor->getValue($config, '[application][front_base]')); $container->setParameter('jk_cms.articles.show_route', $config['application']['articles']['show_route']); $container->setParameter('jk_cms.articles.show_route_parameters', $config['application']['articles']['show_route_parameters']); + $container->setParameter('jk_cms.scripts.template', $accessor->getValue($config, '[scripts][template]')); + $container->setParameter('jk_cms.contact_email', $accessor->getValue($config, '[email][contact_email]')); + $container->setParameter('jk_cms.email.from', $accessor->getValue($config, '[email][from]')); + $container->setParameter('jk_cms.email.to', $accessor->getValue($config, '[email][to]')); $helperDefinition = $container->getDefinition(ConfigurationHelper::class); $helperDefinition->setArgument(0, $config); diff --git a/src/Entity/Article.php b/src/Entity/Article.php index 91b49cd..91fc261 100644 --- a/src/Entity/Article.php +++ b/src/Entity/Article.php @@ -370,6 +370,13 @@ public function getComments() return $this->comments; } + public function getPublishedComments(): Collection + { + return $this->comments->filter(function (Comment $comment) { + return $comment->getIsApproved(); + }); + } + /** * @param mixed $comments */ diff --git a/src/Form/Handler/AddCommentHandler.php b/src/Form/Handler/AddCommentHandler.php index 554743a..30cf2d7 100644 --- a/src/Form/Handler/AddCommentHandler.php +++ b/src/Form/Handler/AddCommentHandler.php @@ -23,7 +23,7 @@ class AddCommentHandler /** * @var string */ - private $contactEmail; + private $fromEmail; /** * @var TranslatorInterface @@ -50,8 +50,14 @@ class AddCommentHandler */ private $router; + /** + * @var string + */ + private $toEmail; + public function __construct( - string $contactEmail, + string $fromEmail, + string $toEmail, CommentManagerInterface $manager, TranslatorInterface $translator, MailerInterface $mailer, @@ -60,12 +66,13 @@ public function __construct( RouterInterface $router ) { $this->manager = $manager; - $this->contactEmail = $contactEmail; + $this->fromEmail = $fromEmail; $this->translator = $translator; $this->mailer = $mailer; $this->helper = $helper; $this->environment = $environment; $this->router = $router; + $this->toEmail = $toEmail; } public function handle(array $data, Article $article): void @@ -85,11 +92,6 @@ public function handle(array $data, Article $article): void private function sendEmail(Comment $comment): void { - $from = $comment->getAuthorEmail(); - - if (!$from) { - $from = 'unknown-sender@lecomptoirdelecureil.fr'; - } $subject = $this->translator->trans('cms.new_comment.subject', [ ':application' => $this->helper->getApplicationName(), ':article' => $comment->getArticle()->getTitle(), @@ -101,8 +103,8 @@ private function sendEmail(Comment $comment): void ], 'mailing'); $email = (new NotificationEmail()) - ->from($from) - ->to($this->contactEmail) + ->from($this->toEmail) + ->to($this->fromEmail) ->subject($subject) ->markdown($message) ->importance(NotificationEmail::IMPORTANCE_MEDIUM) @@ -122,6 +124,9 @@ private function getShowRouteParameters(Article $article): array $parameters = []; foreach ($this->helper->getShowRouteParameters() as $name => $property) { + if (null === $property) { + $property = $name; + } $parameters[$name] = $accessor->getValue($article, $property); } diff --git a/src/Resources/config/services/handlers.yaml b/src/Resources/config/services/handlers.yaml index ddea574..16c7e39 100644 --- a/src/Resources/config/services/handlers.yaml +++ b/src/Resources/config/services/handlers.yaml @@ -5,7 +5,8 @@ services: JK\CmsBundle\Form\Handler\AddCommentHandler: arguments: - $contactEmail: '%jk_cms.contact_email%' + $fromEmail: '%jk_cms.email.from%' + $toEmail: '%jk_cms.email.to%' JK\CmsBundle\Filter\Handler\RequestFilterHandlerInterface: '@JK\CmsBundle\Filter\Handler\RequestFilterHandler' JK\CmsBundle\Filter\Handler\RequestFilterHandler: ~ diff --git a/tests/CmsBundle/Fixtures/config/config.yaml b/tests/CmsBundle/Fixtures/config/config.yaml index 3267610..67b9eae 100644 --- a/tests/CmsBundle/Fixtures/config/config.yaml +++ b/tests/CmsBundle/Fixtures/config/config.yaml @@ -16,6 +16,9 @@ jk_cms: preview_route: test.article.preview preview_route_parameters: id: ~ + email: + from: admin@admin.com + to: admin@admin.com framework: secret: 'yes'