Skip to content

Commit

Permalink
fix code style and review
Browse files Browse the repository at this point in the history
  • Loading branch information
Bram Tervoort committed Mar 7, 2021
1 parent e450805 commit 81e8e37
Show file tree
Hide file tree
Showing 20 changed files with 91 additions and 139 deletions.
8 changes: 3 additions & 5 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Sulu\Bundle\FormBundle\Form\HandlerInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Mailer\Mailer;

/**
* This is the class that validates and merges configuration from your app/config files.
Expand All @@ -23,9 +22,8 @@
*/
class Configuration implements ConfigurationInterface
{
const NULL_HELPER = 'NullHelper';
const SWIFT_MAILER_HELPER = 'SwiftMailer';
const MAILER_HELPER = 'Mailer';
const SWIFT_MAILER_HELPER = 'swift_mailer';
const MAILER_HELPER = 'mailer';

/**
* {@inheritdoc}
Expand Down Expand Up @@ -58,7 +56,7 @@ public function getConfigTreeBuilder()
->children()
->scalarNode('helper')
->defaultValue(self::SWIFT_MAILER_HELPER)
->end()
->end()
->scalarNode('from')->defaultValue(null)->end()
->scalarNode('to')->defaultValue(null)->end()
->scalarNode('sender')->defaultValue(null)->end()
Expand Down
16 changes: 2 additions & 14 deletions DependencyInjection/SuluFormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@

use Sulu\Bundle\FormBundle\Controller\FormTokenController;
use Sulu\Bundle\FormBundle\Controller\FormWebsiteController;
use Sulu\Bundle\MediaBundle\Media\Storage\StorageInterface;
use Sulu\Component\HttpKernel\SuluKernel;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
Expand Down Expand Up @@ -215,21 +211,13 @@ public function load(array $configs, ContainerBuilder $container)
$this->configureHelper($config, $container);
}


private function configureHelper(array $config, ContainerBuilder $container)
{
$helper = $config['mail']['helper'];
if (\method_exists($container, 'resolveEnvPlaceholders')) {
$helper = $container->resolveEnvPlaceholders($helper, true);
}
$container->setParameter('sulu.mail.helper', $helper);

if ($helper == Configuration::MAILER_HELPER) {
$container->setAlias('sulu.mail.helper', 'sulu.mail.mailer');
} elseif ($helper == Configuration::NULL_HELPER) {
$container->setAlias('sulu.mail.helper', 'sulu_mail.null_helper');
} elseif ($helper == Configuration::SWIFT_MAILER_HELPER) {
$container->setAlias('sulu.mail.helper', 'sulu.mail.swift_mailer');
}

$container->setAlias('sulu.mail.helper', 'sulu.mail.' . $helper);
}
}
1 change: 0 additions & 1 deletion Event/RequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Sulu\Bundle\FormBundle\Form\HandlerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;

class RequestListener
Expand Down
3 changes: 0 additions & 3 deletions Form/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
use Sulu\Bundle\FormBundle\Entity\Dynamic;
use Sulu\Bundle\FormBundle\Entity\Form;
use Sulu\Bundle\FormBundle\Form\Type\DynamicFormType;
use Sulu\Bundle\FormBundle\Mail\HelperAwareInterface;
use Sulu\Bundle\FormBundle\Mail\HelperInterface;
use Sulu\Bundle\FormBundle\Repository\FormRepository;
use Sulu\Bundle\FormBundle\TitleProvider\TitleProviderPoolInterface;
use Symfony\Component\Form\FormFactory;
Expand Down Expand Up @@ -274,5 +272,4 @@ private function getWebspaceKey(): ?string

return $webspaceKey;
}

}
1 change: 0 additions & 1 deletion Form/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public function __construct(
string $honeyPotStrategy = self::HONEY_POT_STRATEGY_SPAM,
string $honeyPotField = null
) {

$this->entityManager = $entityManager;
$this->mailHelper = $mailHelper;
$this->twig = $twig;
Expand Down
42 changes: 16 additions & 26 deletions Mail/MailerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ class MailerHelper implements HelperInterface
/**
* @var null|string
*/
protected $toMail;
private $toMail;

/**
* @var null|string
*/
protected $fromMail;
private $fromMail;

/**
* @var string|null
*/
protected $sender;
private $sender;

/**
* @var LoggerInterface
*/
protected $logger;
private $logger;

public function __construct(
MailerInterface $mailer,
Expand All @@ -60,7 +60,7 @@ public function __construct(
}

/**
* @inheritDoc
* {@inheritDoc}
*/
public function sendMail(
$subject,
Expand All @@ -73,8 +73,7 @@ public function sendMail(
$ccMail = [],
$bccMail = [],
$plainText = null
): int
{
): int {
$message = new Email();

$this->setHeaders(
Expand All @@ -101,6 +100,7 @@ public function sendMail(
);

$this->mailer->send($message);

return 0;
}

Expand All @@ -113,13 +113,13 @@ public function sendMail(
* @param Email $message email message
* @param string $subject subject of the email
* @param Address[] $fromMail list of addresses already accounting for the defaults
* @param Address[] $toMail list of addresses already accounting for the defaults
* @param Address[] $toMail list of addresses already accounting for the defaults
* @param Address[] $replyTo list of addresses already accounting for the defaults may be an empty array
* @param Address[] $ccMail list of addresses already accounting for the defaults may be an empty array
* @param Address[] $bccMail list of addresses already accounting for the defaults may be an empty array
* @param Address|null $sender address already accounting for the defaults
*/
protected function setHeaders(
private function setHeaders(
Email $message,
string $subject,
array $fromMail,
Expand All @@ -128,8 +128,7 @@ protected function setHeaders(
array $ccMail,
array $bccMail,
?Address $sender
): void
{
): void {
$message->subject($subject);
$message->from(...$fromMail);
$message->to(...$toMail);
Expand All @@ -149,14 +148,12 @@ protected function setHeaders(
}

/**
* @param Email $message
* @param \SplFileInfo[] $attachments
*/
protected function setAttachments(
private function setAttachments(
Email $message,
array $attachments
): void
{
): void {
foreach ($attachments as $file) {
if (!($file instanceof \SplFileInfo)) {
continue;
Expand All @@ -173,18 +170,14 @@ protected function setAttachments(
}

/**
* @param Email $message
* @param bool $html
* @param string $body
* @param string $plainText
*/
protected function setBody(
private function setBody(
Email $message,
bool $html,
string $body,
?string $plainText
): void
{
): void {
if ($html) {
$message->html($body);
} else {
Expand All @@ -200,9 +193,6 @@ protected function setBody(
* @param string|array $fromMail
* @param string|array $toMail
* @param string|array $replyTo
* @param string $subject
* @param array $ccMail
* @param array $bccMail
* @param string $plainText
*/
private function logMessage(
Expand All @@ -213,8 +203,7 @@ private function logMessage(
array $ccMail,
array $bccMail,
?string $plainText
): void
{
): void {
$this->logger->info(sprintf(
'Try register mail from SuluFormBundle: ' . PHP_EOL .
' From: %s' . PHP_EOL .
Expand All @@ -236,6 +225,7 @@ private function logMessage(

/**
* @param string|array $fromMail email address or [email-address => name] for muliple named addresses
*
* @return Address[]
*/
private function parseToAddresses($fromMail): array
Expand Down
10 changes: 1 addition & 9 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@
</service>
<service id="Sulu\Bundle\FormBundle\Form\HandlerInterface" alias="sulu_form.handler"/>

<service id="sulu.mail.helper_aware_configurator" class="Sulu\Bundle\FormBundle\Mail\HelperAwareConfigurator">
<argument type="service" id="sulu.mail.helper" />
<argument type="tagged_iterator" tag="sulu_mail.handler" index-by="key" />
<argument>%sulu_mail.mail.helper_name%</argument>
</service>

<!-- Mail-->
<service id="sulu.mail.helper" alias="sulu.mail.swift_mailer" />

<service id="sulu.mail.swift_mailer" class="Sulu\Bundle\FormBundle\Mail\Helper">
<argument type="service" id="mailer" />
<argument>%sulu_form.mail.from%</argument>
Expand All @@ -45,7 +37,7 @@

<service id="sulu_mail.null_helper" class="Sulu\Bundle\FormBundle\Mail\NullHelper">
<argument type="service" id="logger" />
<tag name="sulu_mail.handler" key="NullHelper" />
<deprecated />
</service>

<!-- Admin -->
Expand Down
2 changes: 1 addition & 1 deletion Resources/doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Symfony mailer):
```yml
sulu_form:
mail:
helper: "SwiftMailer"
helper: "swift_mailer" # is default
```
## Create Database
Expand Down
2 changes: 0 additions & 2 deletions SuluFormBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@

use Sulu\Bundle\FormBundle\DependencyInjection\CompilerPass\DynamicListBuilderCompilerPass;
use Sulu\Bundle\FormBundle\DependencyInjection\CompilerPass\ListProviderCompilerPass;
use Sulu\Bundle\FormBundle\DependencyInjection\CompilerPass\MailHelperCompilerPass;
use Sulu\Bundle\FormBundle\DependencyInjection\CompilerPass\RemoveTaggedServiceCollectorCompilerPass;
use Sulu\Component\Symfony\CompilerPass\TaggedServiceCollectorCompilerPass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand Down
2 changes: 1 addition & 1 deletion Tests/Application/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
APP_ENV=test
DATABASE_URL=mysql://root:@127.0.0.1:3306/su_form_test
DATABASE_URL=mysql://root:root@127.0.0.1:3306/su_form_test
DATABASE_CHARSET=utf8mb4
DATABASE_COLLATE=utf8mb4_unicode_ci
4 changes: 0 additions & 4 deletions Tests/Application/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@

namespace Sulu\Bundle\FormBundle\Tests\Application;

use Sulu\Bundle\FormBundle\Tests\Application\Controller\DefaultController;
use Sulu\Bundle\FormBundle\SuluFormBundle;
use Sulu\Bundle\TestBundle\Kernel\SuluTestKernel;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

class Kernel extends SuluTestKernel
{
Expand Down
13 changes: 10 additions & 3 deletions Tests/Application/MailerKernel.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\FormBundle\Tests\Application;


use Symfony\Component\Config\Loader\LoaderInterface;

class MailerKernel extends Kernel
Expand All @@ -20,7 +27,7 @@ public function getCacheDir()
return $this->getProjectDir() . \DIRECTORY_SEPARATOR
. 'var' . \DIRECTORY_SEPARATOR
. 'cache' . \DIRECTORY_SEPARATOR
. $this->getContext() . \DIRECTORY_SEPARATOR
. $this->environment;
. $this->getContext() . \DIRECTORY_SEPARATOR
. $this->environment . '_mailer';
}
}
1 change: 1 addition & 0 deletions Tests/Application/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ parameters:

framework:
esi: ~
csrf_protection: false
profiler: { only_exceptions: false }
fragments: ~

Expand Down
5 changes: 1 addition & 4 deletions Tests/Application/config/config_test_mailer.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#import:
# - config.yml
#
sulu_form:
mail:
helper: 'Mailer'
helper: 'mailer'
12 changes: 8 additions & 4 deletions Tests/Functional/Mail/Fixtures/LoadFormFixture.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\FormBundle\Tests\Functional\Mail\Fixtures;


use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Sulu\Bundle\FormBundle\Configuration\MailConfiguration;
Expand Down Expand Up @@ -110,8 +117,5 @@ public function load(ObjectManager $manager)
$formField->setForm($form);

$manager->persist($form);
$manager->flush();
$manager->clear();
}

}
Loading

0 comments on commit 81e8e37

Please sign in to comment.