Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors lib/private/Mail. #38987

Merged
merged 1 commit into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 4 additions & 19 deletions lib/private/Mail/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,15 @@
* @since 13.0.0
*/
class Attachment implements IAttachment {
private ?string $body;
private ?string $name;
private ?string $contentType;
private ?string $path;

public function __construct(
?string $body,
?string $name,
?string $contentType,
?string $path = null
private ?string $body,
private ?string $name,
private ?string $contentType,
private ?string $path = null
) {
$this->body = $body;
$this->name = $name;
$this->contentType = $contentType;
$this->path = $path;
}

/**
* @param string $filename
* @return $this
* @since 13.0.0
*/
public function setFilename(string $filename): IAttachment {
Expand All @@ -64,8 +53,6 @@ public function setFilename(string $filename): IAttachment {
}

/**
* @param string $contentType
* @return $this
* @since 13.0.0
*/
public function setContentType(string $contentType): IAttachment {
Expand All @@ -74,8 +61,6 @@ public function setContentType(string $contentType): IAttachment {
}

/**
* @param string $body
* @return $this
* @since 13.0.0
*/
public function setBody(string $body): IAttachment {
Expand Down
135 changes: 59 additions & 76 deletions lib/private/Mail/EMailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,33 +52,19 @@
* @package OC\Mail
*/
class EMailTemplate implements IEMailTemplate {
/** @var Defaults */
protected $themingDefaults;
/** @var IURLGenerator */
protected $urlGenerator;
/** @var IFactory */
protected $l10nFactory;
/** @var string */
protected $emailId;
/** @var array */
protected $data;

/** @var string */
protected $subject = '';
/** @var string */
protected $htmlBody = '';
/** @var string */
protected $plainBody = '';
/** @var bool indicated if the footer is added */
protected $headerAdded = false;
/** @var bool indicated if the body is already opened */
protected $bodyOpened = false;
/** @var bool indicated if there is a list open in the body */
protected $bodyListOpened = false;
/** @var bool indicated if the footer is added */
protected $footerAdded = false;

protected $head = <<<EOF
protected string $subject = '';
protected string $htmlBody = '';
protected string $plainBody = '';
/** indicated if the header is added */
protected bool $headerAdded = false;
/** indicated if the body is already opened */
protected bool $bodyOpened = false;
/** indicated if there is a list open in the body */
protected bool $bodyListOpened = false;
/** indicated if the footer is added */
protected bool $footerAdded = false;

protected string $head = <<<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" style="-webkit-font-smoothing:antialiased;background:#fff!important">
<head>
Expand All @@ -96,7 +82,7 @@ class EMailTemplate implements IEMailTemplate {
<center data-parsed="" style="min-width:580px;width:100%">
EOF;

protected $tail = <<<EOF
protected string $tail = <<<EOF
</center>
</td>
</tr>
Expand All @@ -108,7 +94,7 @@ class EMailTemplate implements IEMailTemplate {

EOF;

protected $header = <<<EOF
protected string $header = <<<EOF
<table align="center" class="wrapper header float-center" style="Margin:0 auto;background:#fff;border-collapse:collapse;border-spacing:0;float:none;margin:0 auto;padding:0;text-align:center;vertical-align:top;width:100%%">
<tr style="padding:0;text-align:left;vertical-align:top">
<td class="wrapper-inner" style="-moz-hyphens:auto;-webkit-hyphens:auto;Margin:0;border-collapse:collapse!important;color:#0a0a0a;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Oxygen-Sans,Ubuntu,Cantarell,'Helvetica Neue',Arial,sans-serif;font-size:16px;font-weight:400;hyphens:auto;line-height:1.3;margin:0;padding:20px;text-align:left;vertical-align:top;word-wrap:break-word">
Expand Down Expand Up @@ -141,7 +127,7 @@ class EMailTemplate implements IEMailTemplate {
</table>
EOF;

protected $heading = <<<EOF
protected string $heading = <<<EOF
<table align="center" class="container main-heading float-center" style="Margin:0 auto;background:0 0!important;border-collapse:collapse;border-spacing:0;float:none;margin:0 auto;padding:0;text-align:center;vertical-align:top;width:580px">
<tbody>
<tr style="padding:0;text-align:left;vertical-align:top">
Expand All @@ -160,7 +146,7 @@ class EMailTemplate implements IEMailTemplate {
</table>
EOF;

protected $bodyBegin = <<<EOF
protected string $bodyBegin = <<<EOF
<table align="center" class="wrapper content float-center" style="Margin:0 auto;border-collapse:collapse;border-spacing:0;float:none;margin:0 auto;padding:0;text-align:center;vertical-align:top;width:100%">
<tr style="padding:0;text-align:left;vertical-align:top">
<td class="wrapper-inner" style="-moz-hyphens:auto;-webkit-hyphens:auto;Margin:0;border-collapse:collapse!important;color:#0a0a0a;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Oxygen-Sans,Ubuntu,Cantarell,'Helvetica Neue',Arial,sans-serif;font-size:16px;font-weight:400;hyphens:auto;line-height:1.3;margin:0;padding:0;text-align:left;vertical-align:top;word-wrap:break-word">
Expand All @@ -170,7 +156,7 @@ class EMailTemplate implements IEMailTemplate {
<td style="-moz-hyphens:auto;-webkit-hyphens:auto;Margin:0;border-collapse:collapse!important;color:#0a0a0a;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Oxygen-Sans,Ubuntu,Cantarell,'Helvetica Neue',Arial,sans-serif;font-size:16px;font-weight:400;hyphens:auto;line-height:1.3;margin:0;padding:0;text-align:left;vertical-align:top;word-wrap:break-word">
EOF;

protected $bodyText = <<<EOF
protected string $bodyText = <<<EOF
<table class="row description" style="border-collapse:collapse;border-spacing:0;display:table;padding:0;position:relative;text-align:left;vertical-align:top;width:100%%">
<tbody>
<tr style="padding:0;text-align:left;vertical-align:top">
Expand All @@ -190,15 +176,15 @@ class EMailTemplate implements IEMailTemplate {
EOF;

// note: listBegin (like bodyBegin) is not processed through sprintf, so "%" is not escaped as "%%". (bug #12151)
protected $listBegin = <<<EOF
protected string $listBegin = <<<EOF
<table class="row description" style="border-collapse:collapse;border-spacing:0;display:table;padding:0;position:relative;text-align:left;vertical-align:top;width:100%">
<tbody>
<tr style="padding:0;text-align:left;vertical-align:top">
<th class="small-12 large-12 columns first last" style="Margin:0 auto;color:#0a0a0a;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Oxygen-Sans,Ubuntu,Cantarell,'Helvetica Neue',Arial,sans-serif;font-size:16px;font-weight:400;line-height:1.3;margin:0 auto;padding:0;padding-bottom:30px;padding-left:30px;padding-right:30px;text-align:left;width:550px">
<table style="border-collapse:collapse;border-spacing:0;padding:0;text-align:left;vertical-align:top;width:100%">
EOF;

protected $listItem = <<<EOF
protected string $listItem = <<<EOF
<tr style="padding:0;text-align:left;vertical-align:top">
<td style="Margin:0;color:#0a0a0a;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Oxygen-Sans,Ubuntu,Cantarell,'Helvetica Neue',Arial,sans-serif;font-size:16px;font-weight:400;line-height:1.3;margin:0;padding:0;text-align:left;width:15px;">
<p class="text-left" style="Margin:0;Margin-bottom:10px;color:#777;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Oxygen-Sans,Ubuntu,Cantarell,'Helvetica Neue',Arial,sans-serif;font-size:16px;font-weight:400;line-height:1.3;margin:0;margin-bottom:10px;padding:0;padding-left:10px;text-align:left">%s</p>
Expand All @@ -210,15 +196,15 @@ class EMailTemplate implements IEMailTemplate {
</tr>
EOF;

protected $listEnd = <<<EOF
protected string $listEnd = <<<EOF
</table>
</th>
</tr>
</tbody>
</table>
EOF;

protected $buttonGroup = <<<EOF
protected string $buttonGroup = <<<EOF
<table class="spacer" style="border-collapse:collapse;border-spacing:0;padding:0;text-align:left;vertical-align:top;width:100%%">
<tbody>
<tr style="padding:0;text-align:left;vertical-align:top">
Expand Down Expand Up @@ -271,7 +257,7 @@ class EMailTemplate implements IEMailTemplate {
</table>
EOF;

protected $button = <<<EOF
protected string $button = <<<EOF
<table class="spacer" style="border-collapse:collapse;border-spacing:0;padding:0;text-align:left;vertical-align:top;width:100%%">
<tbody>
<tr style="padding:0;text-align:left;vertical-align:top">
Expand Down Expand Up @@ -311,7 +297,7 @@ class EMailTemplate implements IEMailTemplate {
</table>
EOF;

protected $bodyEnd = <<<EOF
protected string $bodyEnd = <<<EOF

</td>
</tr>
Expand All @@ -322,7 +308,7 @@ class EMailTemplate implements IEMailTemplate {
</table>
EOF;

protected $footer = <<<EOF
protected string $footer = <<<EOF
<table class="spacer float-center" style="Margin:0 auto;border-collapse:collapse;border-spacing:0;float:none;margin:0 auto;padding:0;text-align:center;vertical-align:top;width:100%%">
<tbody>
<tr style="padding:0;text-align:left;vertical-align:top">
Expand All @@ -348,32 +334,27 @@ class EMailTemplate implements IEMailTemplate {
</table>
EOF;

public function __construct(Defaults $themingDefaults,
IURLGenerator $urlGenerator,
IFactory $l10nFactory,
$emailId,
array $data) {
$this->themingDefaults = $themingDefaults;
$this->urlGenerator = $urlGenerator;
$this->l10nFactory = $l10nFactory;
public function __construct(
protected Defaults $themingDefaults,
protected IURLGenerator $urlGenerator,
protected IFactory $l10nFactory,
protected string $emailId,
protected array $data,
) {
$this->htmlBody .= $this->head;
$this->emailId = $emailId;
$this->data = $data;
}

/**
* Sets the subject of the email
*
* @param string $subject
*/
public function setSubject(string $subject) {
public function setSubject(string $subject): void {
$this->subject = $subject;
}

/**
* Adds a header to the email
*/
public function addHeader() {
public function addHeader(): void {
if ($this->headerAdded) {
return;
}
Expand All @@ -386,11 +367,10 @@ public function addHeader() {
/**
* Adds a heading to the email
*
* @param string $title
* @param string|bool $plainTitle Title that is used in the plain text email
* if empty the $title is used, if false none will be used
*/
public function addHeading(string $title, $plainTitle = '') {
public function addHeading(string $title, $plainTitle = ''): void {
if ($this->footerAdded) {
return;
}
Expand All @@ -407,7 +387,7 @@ public function addHeading(string $title, $plainTitle = '') {
/**
* Open the HTML body when it is not already
*/
protected function ensureBodyIsOpened() {
protected function ensureBodyIsOpened(): void {
if ($this->bodyOpened) {
return;
}
Expand All @@ -423,7 +403,7 @@ protected function ensureBodyIsOpened() {
* @param string|bool $plainText Text that is used in the plain text email
* if empty the $text is used, if false none will be used
*/
public function addBodyText(string $text, $plainText = '') {
public function addBodyText(string $text, $plainText = ''): void {
if ($this->footerAdded) {
return;
}
Expand Down Expand Up @@ -451,10 +431,17 @@ public function addBodyText(string $text, $plainText = '') {
* if empty or true the $text is used, if false none will be used
* @param string|bool $plainMetaInfo Meta info that is used in the plain text email
* if empty or true the $metaInfo is used, if false none will be used
* @param integer plainIndent If > 0, Indent plainText by this amount.
* @param integer $plainIndent plainIndent If > 0, Indent plainText by this amount.
* @since 12.0.0
*/
public function addBodyListItem(string $text, string $metaInfo = '', string $icon = '', $plainText = '', $plainMetaInfo = '', $plainIndent = 0) {
public function addBodyListItem(
string $text,
string $metaInfo = '',
string $icon = '',
$plainText = '',
$plainMetaInfo = '',
$plainIndent = 0,
): void {
$this->ensureBodyListOpened();

if ($plainText === '' || $plainText === true) {
Expand Down Expand Up @@ -504,7 +491,7 @@ public function addBodyListItem(string $text, string $metaInfo = '', string $ico
}
}

protected function ensureBodyListOpened() {
protected function ensureBodyListOpened(): void {
if ($this->bodyListOpened) {
return;
}
Expand All @@ -514,7 +501,7 @@ protected function ensureBodyListOpened() {
$this->htmlBody .= $this->listBegin;
}

protected function ensureBodyListClosed() {
protected function ensureBodyListClosed(): void {
if (!$this->bodyListOpened) {
return;
}
Expand All @@ -533,12 +520,14 @@ protected function ensureBodyListClosed() {
* @param string $plainTextLeft Text of left button that is used in the plain text version - if unset the $textLeft is used
* @param string $plainTextRight Text of right button that is used in the plain text version - if unset the $textRight is used
*/
public function addBodyButtonGroup(string $textLeft,
string $urlLeft,
string $textRight,
string $urlRight,
string $plainTextLeft = '',
string $plainTextRight = '') {
public function addBodyButtonGroup(
string $textLeft,
string $urlLeft,
string $textRight,
string $urlRight,
string $plainTextLeft = '',
string $plainTextRight = '',
): void {
if ($this->footerAdded) {
return;
}
Expand Down Expand Up @@ -573,7 +562,7 @@ public function addBodyButtonGroup(string $textLeft,
*
* @since 12.0.0
*/
public function addBodyButton(string $text, string $url, $plainText = '') {
public function addBodyButton(string $text, string $url, $plainText = ''): void {
if ($this->footerAdded) {
return;
}
Expand All @@ -600,7 +589,7 @@ public function addBodyButton(string $text, string $url, $plainText = '') {
/**
* Close the HTML body when it is open
*/
protected function ensureBodyIsClosed() {
protected function ensureBodyIsClosed(): void {
if (!$this->bodyOpened) {
return;
}
Expand All @@ -616,7 +605,7 @@ protected function ensureBodyIsClosed() {
*
* @param string $text If the text is empty the default "Name - Slogan<br>This is an automatically sent email" will be used
*/
public function addFooter(string $text = '', ?string $lang = null) {
public function addFooter(string $text = '', ?string $lang = null): void {
if ($text === '') {
$l10n = $this->l10nFactory->get('lib', $lang);
$slogan = $this->themingDefaults->getSlogan($lang);
Expand All @@ -641,17 +630,13 @@ public function addFooter(string $text = '', ?string $lang = null) {

/**
* Returns the rendered email subject as string
*
* @return string
*/
public function renderSubject(): string {
return $this->subject;
}

/**
* Returns the rendered HTML email as string
*
* @return string
*/
public function renderHtml(): string {
if (!$this->footerAdded) {
Expand All @@ -664,8 +649,6 @@ public function renderHtml(): string {

/**
* Returns the rendered plain text email as string
*
* @return string
*/
public function renderText(): string {
if (!$this->footerAdded) {
Expand Down