Skip to content

Commit 6736853

Browse files
committed
Use method to getTransport()
1 parent 49c2968 commit 6736853

File tree

1 file changed

+42
-27
lines changed

1 file changed

+42
-27
lines changed

app/code/Magento/Email/Model/Transport.php

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Magento\Store\Model\ScopeInterface;
1919
use Laminas\Mail\Message;
2020
use Laminas\Mail\Transport\Sendmail;
21+
use Laminas\Mail\Transport\TransportInterface as LaminasTransportInterface;
2122
use Psr\Log\LoggerInterface;
2223

2324
/**
@@ -90,10 +91,20 @@ class Transport implements TransportInterface
9091
private $returnPathValue;
9192

9293
/**
93-
* @var Sendmail
94+
* @var ScopeConfigInterface
95+
*/
96+
private $scopeConfig;
97+
98+
/**
99+
* @var LaminasTransportInterface|null
94100
*/
95101
private $laminasTransport;
96102

103+
/**
104+
* @var null|string|array|\Traversable
105+
*/
106+
private $parameters;
107+
97108
/**
98109
* @var MessageInterface
99110
*/
@@ -124,20 +135,28 @@ public function __construct(
124135
self::XML_PATH_SENDING_RETURN_PATH_EMAIL,
125136
ScopeInterface::SCOPE_STORE
126137
);
138+
$this->message = $message;
139+
$this->scopeConfig = $scopeConfig;
140+
$this->parameters = $parameters;
141+
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
142+
}
127143

128-
$transport = $scopeConfig->getValue(
129-
self::XML_PATH_TRANSPORT,
130-
ScopeInterface::SCOPE_STORE
131-
);
132-
133-
if ($transport === 'smtp') {
134-
$this->laminasTransport = $this->createSmtpTransport($scopeConfig);
135-
} else {
136-
$this->laminasTransport = $this->createSendmailTransport($parameters);
144+
public function getTransport(): LaminasTransportInterface
145+
{
146+
if ($this->laminasTransport === null) {
147+
$transport = $this->scopeConfig->getValue(
148+
self::XML_PATH_TRANSPORT,
149+
ScopeInterface::SCOPE_STORE
150+
);
151+
152+
if ($transport === 'smtp') {
153+
$this->laminasTransport = $this->createSmtpTransport();
154+
} else {
155+
$this->laminasTransport = $this->createSendmailTransport();
156+
}
137157
}
138158

139-
$this->message = $message;
140-
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
159+
return $this->laminasTransport;
141160
}
142161

143162
/**
@@ -155,7 +174,7 @@ public function sendMessage()
155174
$laminasMessage->setSender($fromAddressList->current()->getEmail());
156175
}
157176

158-
$this->laminasTransport->send($laminasMessage);
177+
$this->getTransport()->send($laminasMessage);
159178
} catch (\Exception $e) {
160179
$this->logger->error($e);
161180
throw new MailException(new Phrase('Unable to send mail. Please try again later.'));
@@ -170,38 +189,34 @@ public function getMessage()
170189
return $this->message;
171190
}
172191

173-
/**
174-
* @param $parameters
175-
* @return Smtp
176-
*/
177-
private function createSmtpTransport($scopeConfig)
192+
private function createSmtpTransport(): Smtp
178193
{
179-
$host = $scopeConfig->getValue(
194+
$host = $this->scopeConfig->getValue(
180195
self::XML_PATH_HOST,
181196
ScopeInterface::SCOPE_STORE
182197
);
183198

184-
$port = $scopeConfig->getValue(
199+
$port = $this->scopeConfig->getValue(
185200
self::XML_PATH_PORT,
186201
ScopeInterface::SCOPE_STORE
187202
);
188203

189-
$username = $scopeConfig->getValue(
204+
$username = $this->scopeConfig->getValue(
190205
self::XML_PATH_USERNAME,
191206
ScopeInterface::SCOPE_STORE
192207
);
193208

194-
$password = $scopeConfig->getValue(
209+
$password = $this->scopeConfig->getValue(
195210
self::XML_PATH_PASSWORD,
196211
ScopeInterface::SCOPE_STORE
197212
);
198213

199-
$auth = $scopeConfig->getValue(
214+
$auth = $this->scopeConfig->getValue(
200215
self::XML_PATH_AUTH,
201216
ScopeInterface::SCOPE_STORE
202217
);
203218

204-
$ssl = $scopeConfig->getValue(
219+
$ssl = $this->scopeConfig->getValue(
205220
self::XML_PATH_SSL,
206221
ScopeInterface::SCOPE_STORE
207222
);
@@ -218,7 +233,7 @@ private function createSmtpTransport($scopeConfig)
218233
];
219234

220235
if ($ssl && $ssl !== 'none') {
221-
$connectionConfig['ssl'] = $ssl;
236+
$options['connection_config']['ssl'] = $ssl;
222237
}
223238

224239
return new Smtp(new SmtpOptions($options));
@@ -228,8 +243,8 @@ private function createSmtpTransport($scopeConfig)
228243
* @param $parameters
229244
* @return Sendmail
230245
*/
231-
private function createSendmailTransport($parameters)
246+
private function createSendmailTransport(): Sendmail
232247
{
233-
return new Sendmail($parameters);
248+
return new Sendmail($this->parameters);
234249
}
235250
}

0 commit comments

Comments
 (0)