Skip to content

Commit

Permalink
Merge pull request #31 from johnkrovitch/bugfix/email-sending
Browse files Browse the repository at this point in the history
Fix the email sending when a new comment is added and improve the mail parameters naming
  • Loading branch information
johnkrovitch authored Jul 19, 2020
2 parents a30a07e + 4722539 commit 6824365
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('[email protected]')->end()
->scalarNode('from')->defaultValue('[email protected]')->cannotBeEmpty()->end()
->scalarNode('to')->defaultValue('[email protected]')->cannotBeEmpty()->end()
->end()
->end()
;
Expand Down
7 changes: 4 additions & 3 deletions src/DependencyInjection/JKCmsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';

Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/Entity/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
25 changes: 15 additions & 10 deletions src/Form/Handler/AddCommentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AddCommentHandler
/**
* @var string
*/
private $contactEmail;
private $fromEmail;

/**
* @var TranslatorInterface
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -85,11 +92,6 @@ public function handle(array $data, Article $article): void

private function sendEmail(Comment $comment): void
{
$from = $comment->getAuthorEmail();

if (!$from) {
$from = '[email protected]';
}
$subject = $this->translator->trans('cms.new_comment.subject', [
':application' => $this->helper->getApplicationName(),
':article' => $comment->getArticle()->getTitle(),
Expand All @@ -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)
Expand All @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/services/handlers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ~
Expand Down
3 changes: 3 additions & 0 deletions tests/CmsBundle/Fixtures/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ jk_cms:
preview_route: test.article.preview
preview_route_parameters:
id: ~
email:
from: [email protected]
to: [email protected]

framework:
secret: 'yes'
Expand Down

0 comments on commit 6824365

Please sign in to comment.