Passed
Pull Request — master (#503)
by Andreas
05:20
created

EmailController::sendEmailPublicLink()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 56
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 2.3312

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 56
ccs 22
cts 39
cp 0.5641
rs 9.7251
cc 2
eloc 39
nc 2
nop 3
crap 2.3312

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
 * @author Thomas Citharel <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016 Thomas Citharel <[email protected]>
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
namespace OCA\Calendar\Controller;
22
23
use OCP\AppFramework\Controller;
24
use OCP\AppFramework\Http;
25
use OCP\AppFramework\Http\JSONResponse;
26
use OCP\AppFramework\Http\TemplateResponse;
27
use OCP\Defaults;
28
use OCP\IConfig;
29
use OCP\IL10N;
30
use OCP\IRequest;
31
use OCP\IUserSession;
32
use OCP\Mail\IMailer;
33
34
class EmailController extends Controller {
35
36
	/**
37
	 * @var IConfig
38
	 */
39
	private $config;
40
41
	/**
42
	 * @var Defaults
43
	 */
44
	private $defaults;
45
46
	/**
47
	 * @var IL10N
48
	 */
49
	private $l10n;
50
51
	/**
52
	 * @var IMailer
53
	 */
54
	private $mailer;
55
56
	/**
57
	 * @var IUserSession
58
	 */
59
	private $userSession;
60
61
	/**
62
	 * @param string $appName
63
	 * @param IRequest $request an instance of the request
64
	 * @param IUserSession $userSession
65
	 * @param IConfig $config
66
	 * @param IMailer $mailer
67
	 * @param IL10N $l10N
68
	 * @param Defaults $defaults
69
	 */
70 2
	public function __construct($appName, IRequest $request, IUserSession $userSession,
71
								IConfig $config, IMailer $mailer, IL10N $l10N, Defaults $defaults) {
72 2
		parent::__construct($appName, $request);
73 2
		$this->config = $config;
74 2
		$this->userSession = $userSession;
75 2
		$this->mailer = $mailer;
76 2
		$this->l10n = $l10N;
77 2
		$this->defaults = $defaults;
78 2
	}
79
80
	/**
81
	 * @param string $to
82
	 * @param string $url
83
	 * @param string $name
84
	 * @return JSONResponse
85
	 * @NoAdminRequired
86
	 */
87 2
	public function sendEmailPublicLink($to, $url, $name) {
88 2
		$user = $this->userSession->getUser();
89 2
		$username = $user->getDisplayName();
90
91 2
		$subject = $this->l10n->t('%s has published the calendar "%s"', [$username, $name]);
92
93 2
		$serverVersion = $this->config->getSystemValue('version');
94 2
		if (version_compare($serverVersion, '12', '>=')) {
95
			$emailTemplate = $this->mailer->createEMailTemplate();
96
97
			$emailTemplate->addHeader();
98
			$emailTemplate->addHeading($this->l10n->t('%s has published the calendar %s', [$username, $name]));
99
100
			$emailTemplate->addBodyText($this->l10n->t('Hello,'));
101
102
			$htmlText = str_replace(
103
				['{boldstart}', '{boldend}'],
104
				['<b>', '</b>'],
105
				$this->l10n->t('We wanted to inform you that %s has published the calendar {boldstart}%s{boldend}.', [$username, $name])
106
			);
107
108
			$plainText = $this->l10n->t('We wanted to inform you that %s has published the calendar %s.', [$username, $name]);
109
			$emailTemplate->addBodyText($htmlText, $plainText);
110
111
			$emailTemplate->addBodyButton($this->l10n->t('Click here to access it'), $url);
112
113
			// TRANSLATORS term at the end of a mail
114
			$emailTemplate->addBodyText($this->l10n->t('Cheers!'));
115
116
			$emailTemplate->addFooter();
117
118
			$bodyHTML = $emailTemplate->renderHtml();
119
			$textBody = $emailTemplate->renderText();
120
		} else {
121 2
			$emailTemplateHTML = new TemplateResponse('calendar', 'mail.publication.html', [
122 2
				'subject' => $subject,
123 2
				'username' => $username,
124 2
				'calendarname' => $name,
125 2
				'calendarurl' => $url,
126 2
				'defaults' => $this->defaults
127 2
			], 'public');
128 2
			$bodyHTML = $emailTemplateHTML->render();
129
130 2
			$emailTemplateText = new TemplateResponse('calendar', 'mail.publication.text', [
131 2
				'subject' => $subject,
132 2
				'username' => $username,
133 2
				'calendarname' => $name,
134
				'calendarurl' => $url
135 2
			], 'blank');
136 2
			$textBody = $emailTemplateText->render();
137
		}
138
139 2
		$status = $this->sendEmail($to, $subject, $bodyHTML, $textBody);
140
141 2
		return new JSONResponse([], $status);
142
	}
143
144
	/**
145
	 * @param string $target
146
	 * @param string $subject
147
	 * @param string $body
148
	 * @param string $textBody
149
	 * @return int
150
	 */
151 2
	private function sendEmail($target, $subject, $body, $textBody) {
152 2
		if (!$this->mailer->validateMailAddress($target)) {
153 2
			return Http::STATUS_BAD_REQUEST;
154
		}
155
156
		$sendFromDomain = $this->config->getSystemValue('mail_domain', 'domain.org');
157
		$sendFromAddress = $this->config->getSystemValue('mail_from_address', 'nextcloud');
158
		$sendFrom = $sendFromAddress . '@' . $sendFromDomain;
159
160
		$message = $this->mailer->createMessage();
161
		$message->setSubject($subject);
162
		$message->setFrom([$sendFrom => $this->defaults->getName()]);
163
		$message->setTo([$target => 'Recipient']);
164
		$message->setPlainBody($textBody);
165
		$message->setHtmlBody($body);
166
		$this->mailer->send($message);
167
168
		return Http::STATUS_OK;
169
	}
170
}
171