Skip to content

Commit 7fd2dc8

Browse files
committed
MC-15295: Implementation of MailAddress and MailAddressList DTO, refactoring EmailMessage and Transport builder
1 parent d44080c commit 7fd2dc8

File tree

5 files changed

+444
-67
lines changed

5 files changed

+444
-67
lines changed

lib/internal/Magento/Framework/Mail/EmailMessage.php

Lines changed: 77 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\Framework\Mail;
99

10+
use Magento\Framework\Exception\MailException;
11+
use Zend\Mail\Address as ZendAddress;
1012
use Zend\Mail\AddressList;
1113
use Zend\Mail\Message as ZendMessage;
1214
use Zend\Mime\Message as ZendMimeMessage;
@@ -26,33 +28,46 @@ class EmailMessage implements EmailMessageInterface
2628
*/
2729
private $mimeMessageFactory;
2830

31+
/**
32+
* @var MailAddressListFactory
33+
*/
34+
private $mailAddressListFactory;
35+
36+
/**
37+
* @var MailAddressFactory
38+
*/
39+
private $mailAddressFactory;
40+
2941
/**
3042
* EmailMessage constructor
3143
*
3244
* @param MimeMessageInterface $body
45+
* @param MailAddressList $to
3346
* @param MimeMessageInterfaceFactory $mimeMessageFactory
34-
* @param array|null $to
35-
* @param array|null $from
36-
* @param array|null $cc
37-
* @param array|null $bcc
38-
* @param array|null $replyTo
39-
* @param string|null $sender
40-
* @param string|null $senderName
47+
* @param MailAddressListFactory $mailAddressListFactory
48+
* @param MailAddressFactory $mailAddressFactory
49+
* @param MailAddressList|null $from
50+
* @param MailAddressList|null $cc
51+
* @param MailAddressList|null $bcc
52+
* @param MailAddressList|null $replyTo
53+
* @param MailAddress|null $sender
4154
* @param string|null $subject
4255
* @param string|null $encoding
4356
*
57+
* @throws MailException
4458
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
4559
*/
4660
public function __construct(
4761
MimeMessageInterface $body,
62+
MailAddressList $to,
4863
MimeMessageInterfaceFactory $mimeMessageFactory,
49-
?array $to = [],
50-
?array $from = [],
51-
?array $cc = [],
52-
?array $bcc = [],
53-
?array $replyTo = [],
54-
?string $sender = '',
55-
?string $senderName = '',
64+
MailAddressListFactory $mailAddressListFactory,
65+
MailAddressFactory $mailAddressFactory,
66+
?MailAddressList $from = null,
67+
?MailAddressList $cc = null,
68+
?MailAddressList $bcc = null,
69+
?MailAddressList $replyTo = null,
70+
?MailAddress $sender = null,
5671
?string $subject = '',
5772
?string $encoding = ''
5873
) {
@@ -67,14 +82,20 @@ public function __construct(
6782
$this->message->setSubject($subject);
6883
}
6984
if ($sender) {
70-
$this->message->setSender($sender, $senderName);
85+
$this->message->setSender($sender);
7186
}
7287
$this->message->setReplyTo($replyTo);
88+
if ($to->count() < 1) {
89+
throw new MailException(__('Email message must have at list one addressee'));
90+
}
91+
7392
$this->message->setTo($to);
7493
$this->message->setFrom($from);
7594
$this->message->setCc($cc);
7695
$this->message->setBcc($bcc);
7796
$this->mimeMessageFactory = $mimeMessageFactory;
97+
$this->mailAddressListFactory = $mailAddressListFactory;
98+
$this->mailAddressFactory = $mailAddressFactory;
7899
}
79100

80101
/**
@@ -96,51 +117,60 @@ public function getHeaders(): array
96117
/**
97118
* @inheritDoc
98119
*/
99-
public function getFrom(): array
120+
public function getFrom(): ?MailAddressList
100121
{
101-
return $this->convertAddressListToArray($this->message->getFrom());
122+
return $this->convertAddressListToMailAddressList($this->message->getFrom());
102123
}
103124

104125
/**
105126
* @inheritDoc
106127
*/
107-
public function getTo(): array
128+
public function getTo(): MailAddressList
108129
{
109-
return $this->convertAddressListToArray($this->message->getTo());
130+
return $this->convertAddressListToMailAddressList($this->message->getTo());
110131
}
111132

112133
/**
113134
* @inheritDoc
114135
*/
115-
public function getCc(): array
136+
public function getCc(): ?MailAddressList
116137
{
117-
return $this->convertAddressListToArray($this->message->getCc());
138+
return $this->convertAddressListToMailAddressList($this->message->getCc());
118139
}
119140

120141
/**
121-
* Retrieve list of BCC recipients
122-
*
123-
* @return array
142+
* @inheritDoc
124143
*/
125-
public function getBcc(): array
144+
public function getBcc(): ?MailAddressList
126145
{
127-
return $this->convertAddressListToArray($this->message->getBcc());
146+
return $this->convertAddressListToMailAddressList($this->message->getBcc());
128147
}
129148

130149
/**
131150
* @inheritDoc
132151
*/
133-
public function getReplyTo(): array
152+
public function getReplyTo(): ?MailAddressList
134153
{
135-
return $this->convertAddressListToArray($this->message->getReplyTo());
154+
return $this->convertAddressListToMailAddressList($this->message->getReplyTo());
136155
}
137156

138157
/**
139158
* @inheritDoc
140159
*/
141-
public function getSender(): ?string
160+
public function getSender(): ?MailAddress
142161
{
143-
return $this->message->getSender();
162+
/** @var ZendAddress $zendSender */
163+
if (!$zendSender = $this->message->getSender()) {
164+
return null;
165+
}
166+
167+
return $this->mailAddressFactory->create(
168+
[
169+
'email' => $zendSender->getEmail(),
170+
'name' => $zendSender->getName(),
171+
'comment' => $zendSender->getComment()
172+
]
173+
);
144174
}
145175

146176
/**
@@ -154,7 +184,7 @@ public function getSubject(): ?string
154184
/**
155185
* @inheritDoc
156186
*/
157-
public function getBody(): ?MimeMessageInterface
187+
public function getBody(): MimeMessageInterface
158188
{
159189
return $this->mimeMessageFactory->create(
160190
['parts' => $this->message->getBody()->getParts()]
@@ -178,9 +208,7 @@ public function getRawMessage(): string
178208
}
179209

180210
/**
181-
* Serialize to string
182-
*
183-
* @return string
211+
* @inheritDoc
184212
*/
185213
public function toString(): string
186214
{
@@ -191,17 +219,23 @@ public function toString(): string
191219
* Converts AddressList to array
192220
*
193221
* @param AddressList $addressList
194-
* @return array
222+
* @return MailAddressList
223+
* @throws MailException
195224
*/
196-
private function convertAddressListToArray(AddressList $addressList): array
225+
private function convertAddressListToMailAddressList(AddressList $addressList): MailAddressList
197226
{
198-
$arrayList = [];
199-
foreach ($addressList as $email => $address) {
200-
if ($address->getName()) {
201-
$arrayList[$email] = $address->getName();
202-
} else {
203-
$arrayList[] = $address->getEmail();
204-
}
227+
/** @var MailAddressList $arrayList */
228+
$arrayList = $this->mailAddressListFactory->create();
229+
foreach ($addressList as $address) {
230+
$arrayList->add(
231+
$this->mailAddressFactory->create(
232+
[
233+
'email' => $address->getEmail(),
234+
'name' => $address->getName(),
235+
'comment' => $address->getComment()
236+
]
237+
)
238+
);
205239
}
206240

207241
return $arrayList;

lib/internal/Magento/Framework/Mail/EmailMessageInterface.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,44 +28,44 @@ public function getHeaders(): array;
2828
/**
2929
* Retrieve list of From senders
3030
*
31-
* @return array
31+
* @return MailAddressList|null
3232
*/
33-
public function getFrom(): array;
33+
public function getFrom(): ?MailAddressList;
3434

3535
/**
3636
* Access the address list of the To header
3737
*
38-
* @return array
38+
* @return MailAddressList
3939
*/
40-
public function getTo(): array;
40+
public function getTo(): MailAddressList;
4141

4242
/**
4343
* Retrieve list of CC recipients
4444
*
45-
* @return array
45+
* @return MailAddressList|null
4646
*/
47-
public function getCc(): array;
47+
public function getCc(): ?MailAddressList;
4848

4949
/**
5050
* Retrieve list of BCC recipients
5151
*
52-
* @return array
52+
* @return MailAddressList|null
5353
*/
54-
public function getBcc(): array;
54+
public function getBcc(): ?MailAddressList;
5555

5656
/**
5757
* Access the address list of the Reply-To header
5858
*
59-
* @return array
59+
* @return MailAddressList|null
6060
*/
61-
public function getReplyTo(): array;
61+
public function getReplyTo(): ?MailAddressList;
6262

6363
/**
6464
* Retrieve the sender address, if any
6565
*
66-
* @return null|array
66+
* @return MailAddress|null
6767
*/
68-
public function getSender(): ?string;
68+
public function getSender(): ?MailAddress;
6969

7070
/**
7171
* Get the message subject header value
@@ -77,9 +77,9 @@ public function getSubject(): ?string;
7777
/**
7878
* Return the currently set message body
7979
*
80-
* @return MimeMessageInterface|null
80+
* @return MimeMessageInterface
8181
*/
82-
public function getBody(): ?MimeMessageInterface;
82+
public function getBody(): MimeMessageInterface;
8383

8484
/**
8585
* Get the string-serialized message body text
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\Mail;
9+
10+
use Magento\Framework\Exception\MailException;
11+
use Zend\Mail\Address;
12+
use Zend\Mail\Exception\InvalidArgumentException;
13+
14+
/**
15+
* Class MailAddress
16+
*/
17+
class MailAddress extends Address
18+
{
19+
/**
20+
* @inheritDoc
21+
*
22+
* @return MailAddress
23+
* @throws MailException
24+
*/
25+
public static function fromString($address, $comment = null)
26+
{
27+
try {
28+
return parent::fromString($address, $comment);
29+
} catch (InvalidArgumentException $e) {
30+
throw new MailException(__($e->getMessage()));
31+
}
32+
}
33+
34+
35+
}

0 commit comments

Comments
 (0)