Skip to content
This repository was archived by the owner on Feb 6, 2022. It is now read-only.

Commit 76521b9

Browse files
committed
code review
1 parent 69a961d commit 76521b9

File tree

3 files changed

+42
-57
lines changed

3 files changed

+42
-57
lines changed

DependencyInjection/Configuration.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ private function getMailersNode()
106106
})
107107
->then(function ($v) {
108108
@trigger_error('The swiftmailer.url configuration key is deprecated since version 2.5.0 and will be removed in 3.0. Use the swiftmailer.dsn configuration key instead', E_USER_DEPRECATED);
109-
$v['dsn'] = $v['url'];
109+
if (!isset($v['dsn'])) {
110+
$v['dsn'] = $v['url'];
111+
}
110112
unset($v['url']);
111113

112114
return $v;

DependencyInjection/SwiftmailerExtension.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
namespace Symfony\Bundle\SwiftmailerBundle\DependencyInjection;
1313

14-
use Symfony\Component\DependencyInjection\Definition;
1514
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1615
use Symfony\Component\DependencyInjection\ContainerInterface;
1716
use Symfony\Component\DependencyInjection\ChildDefinition;
17+
use Symfony\Component\DependencyInjection\Definition;
1818
use Symfony\Component\DependencyInjection\DefinitionDecorator;
1919
use Symfony\Component\DependencyInjection\Reference;
2020
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
@@ -72,10 +72,11 @@ protected function configureMailer($name, array $mailer, ContainerBuilder $conta
7272
->setDefinition(sprintf('swiftmailer.mailer.%s.transport.eventdispatcher', $name), $definitionDecorator)
7373
;
7474

75-
$options = [];
76-
$container->resolveEnvPlaceholders($mailer, null, $options);
75+
foreach (array('transport', 'dsn', 'username', 'password', 'host', 'port', 'timeout', 'source_ip', 'local_domain', 'encryption', 'auth_mode') as $key) {
76+
$container->resolveEnvPlaceholders($mailer[$key], null, $userEnvs);
77+
}
7778

78-
if ($options && isset($mailer['dsn'])) {
79+
if ($userEnvs) {
7980
$transportId = sprintf('swiftmailer.mailer.%s.transport.dynamic', $name);
8081
$definitionDecorator = (new Definition(\Swift_Transport::class))
8182
->setFactory(array(SwiftmailerTransportFactory::class, 'createTransport'))
@@ -93,23 +94,13 @@ protected function configureMailer($name, array $mailer, ContainerBuilder $conta
9394
->replaceArgument(0, new Reference(sprintf('swiftmailer.mailer.%s.transport', $name)))
9495
;
9596

96-
$enable = !(isset($mailer['disable_delivery']) && $mailer['disable_delivery']);
97-
$container->setParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name), $enable);
97+
$container->setParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name), empty($mailer['disable_delivery'])); // FIXME
9898
} else {
99-
$transport = SwiftmailerTransportFactory::resolveOptions($mailer);
99+
$mailer = SwiftmailerTransportFactory::resolveOptions($mailer);
100+
$transport = $mailer['transport'];
100101

101102
$container->setParameter(sprintf('swiftmailer.mailer.%s.transport.name', $name), $transport);
102-
103-
if (isset($mailer['disable_delivery']) && $mailer['disable_delivery']) {
104-
$transport = 'null';
105-
$container->setParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name), false);
106-
} else {
107-
$container->setParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name), true);
108-
}
109-
110-
if (empty($mailer['port'])) {
111-
$mailer['port'] = 'ssl' === $mailer['encryption'] ? 465 : 25;
112-
}
103+
$container->setParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name), empty($mailer['disable_delivery']));
113104

114105
$transportId = in_array($transport, array('smtp', 'mail', 'sendmail', 'null'))
115106
? sprintf('swiftmailer.mailer.%s.transport.%s', $name, $transport)

DependencyInjection/SwiftmailerTransportFactory.php

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ class SwiftmailerTransportFactory
3131
*/
3232
public static function createTransport(array $options, RequestContext $requestContext, \Swift_Events_EventDispatcher $eventDispatcher)
3333
{
34-
$transport = static::resolveOptions($options);
34+
$options = static::resolveOptions($options);
3535

36-
if ('smtp' === $transport) {
36+
if ('smtp' === $options['transport']) {
3737
$smtpAuthHandler = new \Swift_Transport_Esmtp_AuthHandler([
3838
new \Swift_Transport_Esmtp_Auth_CramMd5Authenticator(),
3939
new \Swift_Transport_Esmtp_Auth_LoginAuthenticator(),
4040
new \Swift_Transport_Esmtp_Auth_PlainAuthenticator(),
4141
]);
42-
$smtpAuthHandler->setUsername($options['user']);
43-
$smtpAuthHandler->setPassword($options['pass']);
42+
$smtpAuthHandler->setUsername($options['username']);
43+
$smtpAuthHandler->setPassword($options['password']);
4444
$smtpAuthHandler->setAuthMode($options['auth_mode']);
4545

4646
$transport = new \Swift_Transport_EsmtpTransport(
@@ -55,22 +55,22 @@ public static function createTransport(array $options, RequestContext $requestCo
5555
$transport->setSourceIp($options['source_ip']);
5656

5757
(new SmtpTransportConfigurator(null, $requestContext))->configure($transport);
58-
} elseif ('sendmail' === $transport) {
58+
} elseif ('sendmail' === $options['transport']) {
5959
$transport = new \Swift_Transport_SendmailTransport(
6060
new \Swift_Transport_StreamBuffer(new \Swift_StreamFilters_StringReplacementFilterFactory()),
6161
$eventDispatcher
6262
);
6363

6464
(new SmtpTransportConfigurator(null, $requestContext))->configure($transport);
65-
} elseif ('mail' === $transport) {
65+
} elseif ('mail' === $options['transport']) {
6666
$transport = new \Swift_Transport_MailTransport(
6767
new \Swift_Transport_SimpleMailInvoker(),
6868
$eventDispatcher
6969
);
70-
} elseif ('null' === $transport) {
70+
} elseif ('null' === $options['transport']) {
7171
$transport = new \Swift_Transport_NullTransport($eventDispatcher);
7272
} else {
73-
throw new \InvalidArgumentException(sprintf('Not a built-in Swiftmailer transport: %s.', $options['dsn']));
73+
throw new \InvalidArgumentException(sprintf('Not a built-in Swiftmailer transport: %s.', $options['transport']));
7474
}
7575

7676
return $transport;
@@ -79,60 +79,52 @@ public static function createTransport(array $options, RequestContext $requestCo
7979
/**
8080
* @param array $options
8181
*
82-
* @return string transport
82+
* @return array options
8383
*/
84-
public static function resolveOptions(array &$options)
84+
public static function resolveOptions(array $options)
8585
{
8686
if (null === $options['transport']) {
87-
$transport = 'null';
87+
$options['transport'] = 'null';
8888
} elseif ('gmail' === $options['transport']) {
8989
$options['encryption'] = 'ssl';
9090
$options['auth_mode'] = 'login';
9191
$options['host'] = 'smtp.gmail.com';
92-
$transport = 'smtp';
93-
} else {
94-
$transport = $options['transport'];
92+
$options['transport'] = 'smtp';
9593
}
9694

9795
if (isset($options['dsn'])) {
9896
$parts = parse_url($options['dsn']);
99-
if (!empty($parts['scheme'])) {
100-
$transport = $parts['scheme'];
97+
if (!isset($parts['scheme'])) {
98+
$options['transport'] = $parts['scheme'];
10199
}
102-
103-
if (!empty($parts['user'])) {
100+
if (!isset($parts['user'])) {
104101
$options['username'] = $parts['user'];
105102
}
106-
if (!empty($parts['pass'])) {
103+
if (!isset($parts['pass'])) {
107104
$options['password'] = $parts['pass'];
108105
}
109-
if (!empty($parts['host'])) {
106+
if (!isset($parts['host'])) {
110107
$options['host'] = $parts['host'];
111108
}
112-
if (!empty($parts['port'])) {
109+
if (!isset($parts['port'])) {
113110
$options['port'] = $parts['port'];
114111
}
115-
if (!empty($parts['query'])) {
116-
$query = [];
112+
if (!isset($parts['query'])) {
117113
parse_str($parts['query'], $query);
118-
if (!empty($query['encryption'])) {
119-
$options['encryption'] = $query['encryption'];
120-
}
121-
if (!empty($query['auth_mode'])) {
122-
$options['auth_mode'] = $query['auth_mode'];
123-
}
124-
if (!empty($query['timeout'])) {
125-
$options['timeout'] = $query['timeout'];
126-
}
127-
if (!empty($query['source_ip'])) {
128-
$options['source_ip'] = $query['source_ip'];
129-
}
130-
if (!empty($query['local_domain'])) {
131-
$options['local_domain'] = $query['local_domain'];
114+
$values = array('encryption' => null, 'auth_mode' => null, 'timeout' => null, 'source_ip' => null, 'local_domain' => null);
115+
foreach ($values as $key => $value) {
116+
if (!isset($query[$key])) {
117+
$values[$key] = $query[$key];
118+
}
132119
}
120+
$options += $values;
133121
}
134122
}
135123

136-
return $transport;
124+
if (isset($options['port'])) {
125+
$options['port'] = 'ssl' === $options['encryption'] ? 465 : 25;
126+
}
127+
128+
return $options;
137129
}
138130
}

0 commit comments

Comments
 (0)