Passed
Pull Request — master (#553)
by Morris
06:26
created

EmailController::sendEmailPublicLink()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 48
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 2.3053

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 48
ccs 19
cts 33
cp 0.5758
rs 9.125
cc 2
eloc 34
nc 2
nop 3
crap 2.3053
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 $recipient
82
	 * @param string $url
83
	 * @param string $calendarName
84
	 * @return JSONResponse
85
	 * @NoAdminRequired
86
	 */
87 2
	public function sendEmailPublicLink($recipient, $url, $calendarName) {
88 2
		$user = $this->userSession->getUser();
89 2
		$displayName = $user->getDisplayName();
90
91 2
		$subject = $this->l10n->t('%s has published the calendar »%s«', [$displayName, $calendarName]);
92
93 2
		$serverVersion = $this->config->getSystemValue('version');
94 2
		if (version_compare($serverVersion, '12', '>=')) {
95 2
			$emailTemplate = $this->mailer->createEMailTemplate();
96
97 2
			$emailTemplate->addHeader();
98 2
			$emailTemplate->addHeading($this->l10n->t('%s has published the calendar »%s«', [$displayName, $calendarName]));
99
100 2
			$emailTemplate->addBodyText($this->l10n->t('Hello,'));
101 2
			$emailTemplate->addBodyText($this->l10n->t('We wanted to inform you that %s has published the calendar »%s«.', [$displayName, $calendarName]));
102
103 2
			$emailTemplate->addBodyButton($this->l10n->t('Open »%s«', [$calendarName]), $url);
104
105
			// TRANSLATORS term at the end of a mail
106 2
			$emailTemplate->addBodyText($this->l10n->t('Cheers!'));
107
108 2
			$emailTemplate->addFooter();
109
110 2
			$bodyHTML = $emailTemplate->renderHtml();
111 2
			$textBody = $emailTemplate->renderText();
112 2
		} else {
113
			$emailTemplateHTML = new TemplateResponse('calendar', 'mail.publication.html', [
114
				'subject' => $subject,
115
				'username' => $displayName,
116
				'calendarname' => $calendarName,
117
				'calendarurl' => $url,
118
				'defaults' => $this->defaults
119
			], 'public');
120
			$bodyHTML = $emailTemplateHTML->render();
121
122
			$emailTemplateText = new TemplateResponse('calendar', 'mail.publication.text', [
123
				'subject' => $subject,
124
				'username' => $displayName,
125
				'calendarname' => $calendarName,
126
				'calendarurl' => $url
127
			], 'blank');
128
			$textBody = $emailTemplateText->render();
129
		}
130
131 2
		$status = $this->sendEmail($recipient, $subject, $bodyHTML, $textBody);
132
133 2
		return new JSONResponse([], $status);
134
	}
135
136
	/**
137
	 * @param string $recipient
138
	 * @param string $subject
139
	 * @param string $body
140
	 * @param string $textBody
141
	 * @return int
142
	 */
143 2
	private function sendEmail($recipient, $subject, $body, $textBody) {
144 2
		if (!$this->mailer->validateMailAddress($recipient)) {
145 1
			return Http::STATUS_BAD_REQUEST;
146
		}
147
148 1
		$sendFromDomain = $this->config->getSystemValue('mail_domain', 'domain.org');
149 1
		$sendFromAddress = $this->config->getSystemValue('mail_from_address', 'nextcloud');
150 1
		$sendFrom = $sendFromAddress . '@' . $sendFromDomain;
151
152 1
		$message = $this->mailer->createMessage();
153 1
		$message->setSubject($subject);
154 1
		$message->setFrom([$sendFrom => $this->defaults->getName()]);
155 1
		$message->setTo([$recipient => $this->l10n->t('Recipient')]);
156 1
		$message->setPlainBody($textBody);
157 1
		$message->setHtmlBody($body);
158 1
		$this->mailer->send($message);
159
160 1
		return Http::STATUS_OK;
161
	}
162
}
163