Completed
Push — master ( 7cae75...0a2e2f )
by Lukas
16:41 queued 04:14
created

MailNotifications::sendInternalShareMail()   B

Complexity

Conditions 7
Paths 50

Size

Total Lines 60
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 40
nc 50
nop 3
dl 0
loc 60
rs 7.4661
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B MailNotifications::sendLinkShareMail() 0 29 3
A MailNotifications::createMailBody() 0 19 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Björn Schießle <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Robin McCorkell <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 * @author scolebrook <[email protected]>
12
 * @author Thomas Müller <[email protected]>
13
 * @author Vincent Petry <[email protected]>
14
 *
15
 * @license AGPL-3.0
16
 *
17
 * This code is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License, version 3,
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License, version 3,
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
28
 *
29
 */
30
31
namespace OC\Share;
32
33
use DateTime;
34
use OCP\IL10N;
35
use OCP\IURLGenerator;
36
use OCP\IUser;
37
use OCP\Mail\IMailer;
38
use OCP\ILogger;
39
use OCP\Defaults;
40
use OCP\Util;
41
42
/**
43
 * Class MailNotifications
44
 *
45
 * @package OC\Share
46
 */
47
class MailNotifications {
48
49
	/** @var IUser sender userId */
50
	private $user;
51
	/** @var string sender email address */
52
	private $replyTo;
53
	/** @var string */
54
	private $senderDisplayName;
55
	/** @var IL10N */
56
	private $l;
57
	/** @var IMailer */
58
	private $mailer;
59
	/** @var Defaults */
60
	private $defaults;
61
	/** @var ILogger */
62
	private $logger;
63
	/** @var IURLGenerator */
64
	private $urlGenerator;
65
66
	/**
67
	 * @param IUser $user
68
	 * @param IL10N $l10n
69
	 * @param IMailer $mailer
70
	 * @param ILogger $logger
71
	 * @param Defaults $defaults
72
	 * @param IURLGenerator $urlGenerator
73
	 */
74
	public function __construct(IUser $user,
75
								IL10N $l10n,
76
								IMailer $mailer,
77
								ILogger $logger,
78
								Defaults $defaults,
79
								IURLGenerator $urlGenerator) {
80
		$this->l = $l10n;
81
		$this->user = $user;
82
		$this->mailer = $mailer;
83
		$this->logger = $logger;
84
		$this->defaults = $defaults;
85
		$this->urlGenerator = $urlGenerator;
86
87
		$this->replyTo = $this->user->getEMailAddress();
88
		$this->senderDisplayName = $this->user->getDisplayName();
89
	}
90
91
	/**
92
	 * inform recipient about public link share
93
	 *
94
	 * @param string $recipient recipient email address
95
	 * @param string $filename the shared file
96
	 * @param string $link the public link
97
	 * @param int $expiration expiration date (timestamp)
98
	 * @return string[] $result of failed recipients
99
	 */
100
	public function sendLinkShareMail($recipient, $filename, $link, $expiration) {
101
		$subject = (string)$this->l->t('%s shared »%s« with you', [$this->senderDisplayName, $filename]);
102
		list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration);
103
104
		$recipient = str_replace([', ', '; ', ',', ';', ' '], ',', $recipient);
105
		$recipients = explode(',', $recipient);
106
		try {
107
			$message = $this->mailer->createMessage();
108
			$message->setSubject($subject);
109
			$message->setTo($recipients);
110
			$message->setHtmlBody($htmlBody);
0 ignored issues
show
Bug introduced by
It seems like $htmlBody defined by $this->createMailBody($f...me, $link, $expiration) on line 102 can also be of type boolean; however, OC\Mail\Message::setHtmlBody() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
111
			$message->setPlainBody($textBody);
0 ignored issues
show
Bug introduced by
It seems like $textBody defined by $this->createMailBody($f...me, $link, $expiration) on line 102 can also be of type boolean; however, OC\Mail\Message::setPlainBody() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
112
			$message->setFrom([
113
				Util::getDefaultEmailAddress('sharing-noreply') =>
114
					(string)$this->l->t('%s via %s', [
115
						$this->senderDisplayName,
116
						$this->defaults->getName()
117
					]),
118
			]);
119
			if(!is_null($this->replyTo)) {
120
				$message->setReplyTo([$this->replyTo]);
121
			}
122
123
			return $this->mailer->send($message);
124
		} catch (\Exception $e) {
125
			$this->logger->error("Can't send mail with public link to $recipient: ".$e->getMessage(), ['app' => 'sharing']);
126
			return [$recipient];
127
		}
128
	}
129
130
	/**
131
	 * create mail body for plain text and html mail
132
	 *
133
	 * @param string $filename the shared file
134
	 * @param string $link link to the shared file
135
	 * @param int $expiration expiration date (timestamp)
136
	 * @param string $prefix prefix of mail template files
137
	 * @return array an array of the html mail body and the plain text mail body
138
	 */
139
	private function createMailBody($filename, $link, $expiration, $prefix = '') {
140
		$formattedDate = $expiration ? $this->l->l('date', $expiration) : null;
141
142
		$html = new \OC_Template('core', $prefix . 'mail', '');
143
		$html->assign ('link', $link);
144
		$html->assign ('user_displayname', $this->senderDisplayName);
145
		$html->assign ('filename', $filename);
146
		$html->assign('expiration',  $formattedDate);
147
		$htmlMail = $html->fetchPage();
148
149
		$plainText = new \OC_Template('core', $prefix . 'altmail', '');
150
		$plainText->assign ('link', $link);
151
		$plainText->assign ('user_displayname', $this->senderDisplayName);
152
		$plainText->assign ('filename', $filename);
153
		$plainText->assign('expiration', $formattedDate);
154
		$plainTextMail = $plainText->fetchPage();
155
156
		return [$htmlMail, $plainTextMail];
157
	}
158
}
159