Commit bd8f74cf by Qiang Xue

Refactored MessageInterface::send().

parent eb3b2f45
...@@ -60,21 +60,21 @@ class EmailTarget extends Target ...@@ -60,21 +60,21 @@ class EmailTarget extends Target
*/ */
public function export() public function export()
{ {
$message = $this->mail->compose(); $messages = array_map([$this, 'formatMessage'], $this->messages);
Yii::configure($message, $this->message); $body = wordwrap(implode("\n", $messages), 70);
$this->composeMessage($message); $this->composeMessage($body)->send($this->mail);
$this->mail->send($message);
} }
/** /**
* Composes the given mail message with body content. * Composes a mail message with the given body content.
* The default implementation fills the text body of the message with the log messages. * @param string $body the body content
* @param \yii\mail\MessageInterface $message * @return \yii\mail\MessageInterface $message
*/ */
protected function composeMessage($message) protected function composeMessage($body)
{ {
$messages = array_map([$this, 'formatMessage'], $this->messages); $message = $this->mail->compose();
$body = wordwrap(implode("\n", $messages), 70); Yii::configure($message, $this->message);
$message->setTextBody($body); $message->setTextBody($body);
return $message;
} }
} }
...@@ -26,19 +26,14 @@ use Yii; ...@@ -26,19 +26,14 @@ use Yii;
abstract class BaseMessage extends Object implements MessageInterface abstract class BaseMessage extends Object implements MessageInterface
{ {
/** /**
* @return MailerInterface the mailer component
*/
public function getMailer()
{
return Yii::$app->getComponent('mail');
}
/**
* {@inheritdoc} * {@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 ...@@ -204,9 +204,11 @@ interface MessageInterface
/** /**
* Sends this email message. * 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. * @return boolean whether this message is sent successfully.
*/ */
public function send(); public function send(MailerInterface $mailer = null);
/** /**
* Returns string representation of this message. * Returns string representation of this message.
......
...@@ -40,18 +40,11 @@ class BaseMessageTest extends TestCase ...@@ -40,18 +40,11 @@ class BaseMessageTest extends TestCase
// Tests : // Tests :
public function testGetMailer()
{
$mailer = $this->getMailer();
$message = $mailer->compose();
$this->assertEquals($mailer, $message->getMailer());
}
public function testSend() public function testSend()
{ {
$mailer = $this->getMailer(); $mailer = $this->getMailer();
$message = $mailer->compose(); $message = $mailer->compose();
$message->send(); $message->send($mailer);
$this->assertEquals($message, $mailer->sentMessages[0], 'Unable to send message!'); $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