From 6730ddfd997b67d3f9ffa322aabd3eb380d6d8c8 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Thu, 5 Mar 2020 00:40:42 +0100 Subject: [PATCH 1/2] Remove redundant body creation in case of only a preamble is set --- src/Util/EMailer/SystemMailBuilder.php | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Util/EMailer/SystemMailBuilder.php b/src/Util/EMailer/SystemMailBuilder.php index 59ebb59b1..3d715906b 100644 --- a/src/Util/EMailer/SystemMailBuilder.php +++ b/src/Util/EMailer/SystemMailBuilder.php @@ -36,11 +36,11 @@ use Psr\Log\LoggerInterface; class SystemMailBuilder extends MailBuilder { /** @var string */ - protected $subject; + protected $subject = ''; /** @var string */ - protected $preamble; + protected $preamble = ''; /** @var string */ - protected $body; + protected $body = null; /** @var string */ protected $siteAdmin; @@ -71,10 +71,6 @@ class SystemMailBuilder extends MailBuilder */ public function withMessage(string $subject, string $preamble, string $body = null) { - if (!isset($body)) { - $body = $preamble; - } - $this->subject = $subject; $this->preamble = $preamble; $this->body = $body; @@ -98,7 +94,7 @@ class SystemMailBuilder extends MailBuilder */ protected function getHtmlMessage() { - $htmlVersion = BBCode::convert($this->body); + $htmlVersion = !empty($this->body) ? BBCode::convert($this->body) : ''; // load the template for private message notifications $tpl = Renderer::getMarkupTemplate('email/system/html.tpl'); @@ -117,7 +113,7 @@ class SystemMailBuilder extends MailBuilder */ protected function getPlaintextMessage() { - $textVersion = BBCode::toPlaintext($this->body); + $textVersion = !empty($this->body) ? BBCode::toPlaintext($this->body) : ''; // load the template for private message notifications $tpl = Renderer::getMarkupTemplate('email/system/text.tpl'); From 1d15f6f84853c8b3d786e4945339fd883234adb4 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sat, 7 Mar 2020 12:48:25 +0100 Subject: [PATCH 2/2] Optimize "withMessage()" default --- src/Util/EMailer/SystemMailBuilder.php | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/Util/EMailer/SystemMailBuilder.php b/src/Util/EMailer/SystemMailBuilder.php index 3d715906b..a5dd799ae 100644 --- a/src/Util/EMailer/SystemMailBuilder.php +++ b/src/Util/EMailer/SystemMailBuilder.php @@ -40,7 +40,7 @@ class SystemMailBuilder extends MailBuilder /** @var string */ protected $preamble = ''; /** @var string */ - protected $body = null; + protected $body = ''; /** @var string */ protected $siteAdmin; @@ -63,13 +63,13 @@ class SystemMailBuilder extends MailBuilder /** * Adds a message * - * @param string $subject The subject of the email - * @param string $preamble The preamble of the email - * @param string|null $body The body of the email (if not set, the preamble will get used as body) + * @param string $subject The subject of the email + * @param string $preamble The preamble of the email + * @param string $body The body of the email (optional) * * @return static */ - public function withMessage(string $subject, string $preamble, string $body = null) + public function withMessage(string $subject, string $preamble, string $body = '') { $this->subject = $subject; $this->preamble = $preamble; @@ -94,15 +94,13 @@ class SystemMailBuilder extends MailBuilder */ protected function getHtmlMessage() { - $htmlVersion = !empty($this->body) ? BBCode::convert($this->body) : ''; - // load the template for private message notifications $tpl = Renderer::getMarkupTemplate('email/system/html.tpl'); return Renderer::replaceMacros($tpl, [ '$preamble' => str_replace("\n", "
\n", $this->preamble), '$thanks' => $this->l10n->t('thanks'), '$site_admin' => $this->siteAdmin, - '$htmlversion' => $htmlVersion, + '$htmlversion' => BBCode::convert($this->body), ]); } @@ -113,15 +111,13 @@ class SystemMailBuilder extends MailBuilder */ protected function getPlaintextMessage() { - $textVersion = !empty($this->body) ? BBCode::toPlaintext($this->body) : ''; - // load the template for private message notifications $tpl = Renderer::getMarkupTemplate('email/system/text.tpl'); return Renderer::replaceMacros($tpl, [ '$preamble' => $this->preamble, '$thanks' => $this->l10n->t('thanks'), '$site_admin' => $this->siteAdmin, - '$textversion' => $textVersion, + '$textversion' => BBCode::toPlaintext($this->body), ]); } }