Commit bd8f74cf by Qiang Xue

Refactored MessageInterface::send().

parent eb3b2f45
......@@ -60,21 +60,21 @@ class EmailTarget extends Target
*/
public function export()
{
$message = $this->mail->compose();
Yii::configure($message, $this->message);
$this->composeMessage($message);
$this->mail->send($message);
$messages = array_map([$this, 'formatMessage'], $this->messages);
$body = wordwrap(implode("\n", $messages), 70);
$this->composeMessage($body)->send($this->mail);
}
/**
* Composes the given mail message with body content.
* The default implementation fills the text body of the message with the log messages.
* @param \yii\mail\MessageInterface $message
* Composes a mail message with the given body content.
* @param string $body the body content
* @return \yii\mail\MessageInterface $message
*/
protected function composeMessage($message)
protected function composeMessage($body)
{
$messages = array_map([$this, 'formatMessage'], $this->messages);
$body = wordwrap(implode("\n", $messages), 70);
$message = $this->mail->compose();
Yii::configure($message, $this->message);
$message->setTextBody($body);
return $message;
}
}
......@@ -26,19 +26,14 @@ use Yii;
abstract class BaseMessage extends Object implements MessageInterface
{
/**
* @return MailerInterface the mailer component
*/
public function getMailer()
{
return Yii::$app->getComponent('mail');
}
/**
* {@inheritdoc}
*/
public function send()
public function send(MailerInterface $mailer = null)
{
return $this->getMailer()->send($this);
if ($mailer === null) {
$mailer = Yii::$app->getMail();
}
return $mailer->send($this);
}
/**
......
......@@ -204,9 +204,11 @@ interface MessageInterface
/**
* Sends this email message.
* @param MailerInterface $mailer the mailer that should be used to send this message.
* If null, the "mail" application component will be used instead.
* @return boolean whether this message is sent successfully.
*/
public function send();
public function send(MailerInterface $mailer = null);
/**
* Returns string representation of this message.
......
......@@ -40,18 +40,11 @@ class BaseMessageTest extends TestCase
// Tests :
public function testGetMailer()
{
$mailer = $this->getMailer();
$message = $mailer->compose();
$this->assertEquals($mailer, $message->getMailer());
}
public function testSend()
{
$mailer = $this->getMailer();
$message = $mailer->compose();
$message->send();
$message->send($mailer);
$this->assertEquals($message, $mailer->sentMessages[0], 'Unable to send message!');
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment