From: Faraz Samapoor Date: Sun, 25 Jun 2023 09:48:09 +0000 (+0330) Subject: Refactors lib/private/Mail. X-Git-Tag: v28.0.0beta1~830^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ba4f998b53ee7be87f037a435ffc00a6ec1a0872;p=nextcloud-server.git Refactors lib/private/Mail. Mainly using PHP8's constructor property promotion. Signed-off-by: Faraz Samapoor --- diff --git a/lib/private/Mail/Attachment.php b/lib/private/Mail/Attachment.php index 5bfe0dd0522..7500d0b6c98 100644 --- a/lib/private/Mail/Attachment.php +++ b/lib/private/Mail/Attachment.php @@ -36,26 +36,15 @@ use Symfony\Component\Mime\Email; * @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 { @@ -64,8 +53,6 @@ class Attachment implements IAttachment { } /** - * @param string $contentType - * @return $this * @since 13.0.0 */ public function setContentType(string $contentType): IAttachment { @@ -74,8 +61,6 @@ class Attachment implements IAttachment { } /** - * @param string $body - * @return $this * @since 13.0.0 */ public function setBody(string $body): IAttachment { diff --git a/lib/private/Mail/EMailTemplate.php b/lib/private/Mail/EMailTemplate.php index 40738623c19..bb60f761450 100644 --- a/lib/private/Mail/EMailTemplate.php +++ b/lib/private/Mail/EMailTemplate.php @@ -52,33 +52,19 @@ use OCP\Mail\IEMailTemplate; * @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 = << @@ -96,7 +82,7 @@ class EMailTemplate implements IEMailTemplate {
EOF; - protected $tail = << @@ -108,7 +94,7 @@ EOF; EOF; - protected $header = << @@ -141,7 +127,7 @@ EOF; EOF; - protected $heading = << @@ -160,7 +146,7 @@ EOF; EOF; - protected $bodyBegin = << @@ -170,7 +156,7 @@ EOF; EOF; - protected $bodyText = << @@ -190,7 +176,7 @@ EOF; EOF; // note: listBegin (like bodyBegin) is not processed through sprintf, so "%" is not escaped as "%%". (bug #12151) - protected $listBegin = << @@ -198,7 +184,7 @@ EOF; EOF; - protected $listItem = << EOF; - protected $listEnd = << @@ -218,7 +204,7 @@ EOF;

%s

@@ -210,7 +196,7 @@ EOF;
EOF; - protected $buttonGroup = << @@ -271,7 +257,7 @@ EOF; EOF; - protected $button = << @@ -311,7 +297,7 @@ EOF; EOF; - protected $bodyEnd = << @@ -322,7 +308,7 @@ EOF; EOF; - protected $footer = << @@ -348,32 +334,27 @@ EOF; 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; } @@ -386,11 +367,10 @@ EOF; /** * 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; } @@ -407,7 +387,7 @@ EOF; /** * Open the HTML body when it is not already */ - protected function ensureBodyIsOpened() { + protected function ensureBodyIsOpened(): void { if ($this->bodyOpened) { return; } @@ -423,7 +403,7 @@ EOF; * @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; } @@ -451,10 +431,17 @@ EOF; * 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) { @@ -504,7 +491,7 @@ EOF; } } - protected function ensureBodyListOpened() { + protected function ensureBodyListOpened(): void { if ($this->bodyListOpened) { return; } @@ -514,7 +501,7 @@ EOF; $this->htmlBody .= $this->listBegin; } - protected function ensureBodyListClosed() { + protected function ensureBodyListClosed(): void { if (!$this->bodyListOpened) { return; } @@ -533,12 +520,14 @@ EOF; * @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; } @@ -573,7 +562,7 @@ EOF; * * @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; } @@ -600,7 +589,7 @@ EOF; /** * Close the HTML body when it is open */ - protected function ensureBodyIsClosed() { + protected function ensureBodyIsClosed(): void { if (!$this->bodyOpened) { return; } @@ -616,7 +605,7 @@ EOF; * * @param string $text If the text is empty the default "Name - Slogan
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); @@ -641,8 +630,6 @@ EOF; /** * Returns the rendered email subject as string - * - * @return string */ public function renderSubject(): string { return $this->subject; @@ -650,8 +637,6 @@ EOF; /** * Returns the rendered HTML email as string - * - * @return string */ public function renderHtml(): string { if (!$this->footerAdded) { @@ -664,8 +649,6 @@ EOF; /** * Returns the rendered plain text email as string - * - * @return string */ public function renderText(): string { if (!$this->footerAdded) { diff --git a/lib/private/Mail/Mailer.php b/lib/private/Mail/Mailer.php index 5d838b2cdf1..9c38794a5c7 100644 --- a/lib/private/Mail/Mailer.php +++ b/lib/private/Mail/Mailer.php @@ -79,34 +79,20 @@ use Symfony\Component\Mime\Exception\RfcComplianceException; */ class Mailer implements IMailer { private ?MailerInterface $instance = null; - private IConfig $config; - private LoggerInterface $logger; - private Defaults $defaults; - private IURLGenerator $urlGenerator; - private IL10N $l10n; - private IEventDispatcher $dispatcher; - private IFactory $l10nFactory; - - public function __construct(IConfig $config, - LoggerInterface $logger, - Defaults $defaults, - IURLGenerator $urlGenerator, - IL10N $l10n, - IEventDispatcher $dispatcher, - IFactory $l10nFactory) { - $this->config = $config; - $this->logger = $logger; - $this->defaults = $defaults; - $this->urlGenerator = $urlGenerator; - $this->l10n = $l10n; - $this->dispatcher = $dispatcher; - $this->l10nFactory = $l10nFactory; + + public function __construct( + private IConfig $config, + private LoggerInterface $logger, + private Defaults $defaults, + private IURLGenerator $urlGenerator, + private IL10N $l10n, + private IEventDispatcher $dispatcher, + private IFactory $l10nFactory, + ) { } /** * Creates a new message object that can be passed to send() - * - * @return Message */ public function createMessage(): Message { $plainTextOnly = $this->config->getSystemValueBool('mail_send_plaintext_only', false); @@ -117,7 +103,6 @@ class Mailer implements IMailer { * @param string|null $data * @param string|null $filename * @param string|null $contentType - * @return IAttachment * @since 13.0.0 */ public function createAttachment($data = null, $filename = null, $contentType = null): IAttachment { @@ -125,9 +110,7 @@ class Mailer implements IMailer { } /** - * @param string $path * @param string|null $contentType - * @return IAttachment * @since 13.0.0 */ public function createAttachmentFromPath(string $path, $contentType = null): IAttachment { @@ -137,9 +120,6 @@ class Mailer implements IMailer { /** * Creates a new email template object * - * @param string $emailId - * @param array $data - * @return IEMailTemplate * @since 12.0.0 */ public function createEMailTemplate(string $emailId, array $data = []): IEMailTemplate { @@ -350,14 +330,10 @@ class Mailer implements IMailer { break; } - switch ($this->config->getSystemValueString('mail_sendmailmode', 'smtp')) { - case 'pipe': - $binaryParam = ' -t'; - break; - default: - $binaryParam = ' -bs'; - break; - } + $binaryParam = match ($this->config->getSystemValueString('mail_sendmailmode', 'smtp')) { + 'pipe' => ' -t', + default => ' -bs', + }; return new SendmailTransport($binaryPath . $binaryParam, null, $this->logger); } diff --git a/lib/private/Mail/Message.php b/lib/private/Mail/Message.php index 016340c9692..b0634a4754d 100644 --- a/lib/private/Mail/Message.php +++ b/lib/private/Mail/Message.php @@ -46,27 +46,19 @@ use Symfony\Component\Mime\Exception\RfcComplianceException; * @package OC\Mail */ class Message implements IMessage { - private Email $symfonyEmail; - private bool $plainTextOnly; - - private array $to; - private array $from; - private array $replyTo; - private array $cc; - private array $bcc; - - public function __construct(Email $symfonyEmail, bool $plainTextOnly) { - $this->symfonyEmail = $symfonyEmail; - $this->plainTextOnly = $plainTextOnly; - $this->to = []; - $this->from = []; - $this->replyTo = []; - $this->cc = []; - $this->bcc = []; + private array $to = []; + private array $from = []; + private array $replyTo = []; + private array $cc = []; + private array $bcc = []; + + public function __construct( + private Email $symfonyEmail, + private bool $plainTextOnly, + ) { } /** - * @return $this * @since 13.0.0 */ public function attach(IAttachment $attachment): IMessage { @@ -122,7 +114,6 @@ class Message implements IMessage { * If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php * * @param array $addresses Example: array('sender@domain.org', 'other@domain.org' => 'A name') - * @return $this */ public function setFrom(array $addresses): IMessage { $this->from = $addresses; @@ -138,8 +129,6 @@ class Message implements IMessage { /** * Set the Reply-To address of this message - * - * @return $this */ public function setReplyTo(array $addresses): IMessage { $this->replyTo = $addresses; @@ -157,7 +146,6 @@ class Message implements IMessage { * Set the to addresses of this message. * * @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name') - * @return $this */ public function setTo(array $recipients): IMessage { $this->to = $recipients; @@ -175,7 +163,6 @@ class Message implements IMessage { * Set the CC recipients of this message. * * @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name') - * @return $this */ public function setCc(array $recipients): IMessage { $this->cc = $recipients; @@ -193,7 +180,6 @@ class Message implements IMessage { * Set the BCC recipients of this message. * * @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name') - * @return $this */ public function setBcc(array $recipients): IMessage { $this->bcc = $recipients; @@ -241,7 +227,7 @@ class Message implements IMessage { } /** - * Set the underlying Email intance + * Set the underlying Email instance */ public function setSymfonyEmail(Email $symfonyEmail): void { $this->symfonyEmail = $symfonyEmail; @@ -254,9 +240,6 @@ class Message implements IMessage { return $this->symfonyEmail; } - /** - * @return $this - */ public function setBody(string $body, string $contentType): IMessage { if (!$this->plainTextOnly || $contentType !== 'text/html') { if ($contentType === 'text/html') { @@ -284,10 +267,9 @@ class Message implements IMessage { * we wrap the calls here. We then have the validation errors all in one place and can * throw shortly before \OC\Mail\Mailer::send * - * @return void * @throws InvalidArgumentException|RfcComplianceException */ - public function setRecipients() { + public function setRecipients(): void { $this->symfonyEmail->to(...$this->convertAddresses($this->getTo())); $this->symfonyEmail->from(...$this->convertAddresses($this->getFrom())); $this->symfonyEmail->replyTo(...$this->convertAddresses($this->getReplyTo())); @@ -295,9 +277,6 @@ class Message implements IMessage { $this->symfonyEmail->bcc(...$this->convertAddresses($this->getBcc())); } - /** - * @return $this - */ public function useTemplate(IEMailTemplate $emailTemplate): IMessage { $this->setSubject($emailTemplate->renderSubject()); $this->setPlainBody($emailTemplate->renderText()); @@ -335,8 +314,6 @@ class Message implements IMessage { /** * Get the current value of the Auto-Submitted header. Defaults to "no" * which is equivalent to the header not existing at all - * - * @return string */ public function getAutoSubmitted(): string { $headers = $this->symfonyEmail->getHeaders();