EmailController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 74.58%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 134
ccs 44
cts 59
cp 0.7458
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A sendEmailPublicLink() 0 53 2
A sendEmail() 0 19 2
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('calendar.PublicShareNotification', [
96 2
				'recipient' => $recipient,
97 2
				'displayname' => $displayName,
98 2
				'calendar_name' => $calendarName,
99 2
				'calendar_url' => $url,
100
			]);
101
102 2
			$emailTemplate->addHeader();
103 2
			$emailTemplate->addHeading($this->l10n->t('%s has published the calendar »%s«', [$displayName, $calendarName]));
104
105 2
			$emailTemplate->addBodyText($this->l10n->t('Hello,'));
106 2
			$emailTemplate->addBodyText($this->l10n->t('We wanted to inform you that %s has published the calendar »%s«.', [$displayName, $calendarName]));
107
108 2
			$emailTemplate->addBodyButton($this->l10n->t('Open »%s«', [$calendarName]), $url);
109
110
			// TRANSLATORS term at the end of a mail
111 2
			$emailTemplate->addBodyText($this->l10n->t('Cheers!'));
112
113 2
			$emailTemplate->addFooter();
114
115 2
			$bodyHTML = $emailTemplate->renderHtml();
116 2
			$textBody = $emailTemplate->renderText();
117
		} else {
118
			$emailTemplateHTML = new TemplateResponse('calendar', 'mail.publication.html', [
119
				'subject' => $subject,
120
				'username' => $displayName,
121
				'calendarname' => $calendarName,
122
				'calendarurl' => $url,
123
				'defaults' => $this->defaults
124
			], 'public');
125
			$bodyHTML = $emailTemplateHTML->render();
126
127
			$emailTemplateText = new TemplateResponse('calendar', 'mail.publication.text', [
128
				'subject' => $subject,
129
				'username' => $displayName,
130
				'calendarname' => $calendarName,
131
				'calendarurl' => $url
132
			], 'blank');
133
			$textBody = $emailTemplateText->render();
134
		}
135
136 2
		$status = $this->sendEmail($recipient, $subject, $bodyHTML, $textBody);
137
138 2
		return new JSONResponse([], $status);
139
	}
140
141
	/**
142
	 * @param string $recipient
143
	 * @param string $subject
144
	 * @param string $body
145
	 * @param string $textBody
146
	 * @return int
147
	 */
148 2
	private function sendEmail($recipient, $subject, $body, $textBody) {
149 2
		if (!$this->mailer->validateMailAddress($recipient)) {
150 1
			return Http::STATUS_BAD_REQUEST;
151
		}
152
153 1
		$sendFromDomain = $this->config->getSystemValue('mail_domain', 'domain.org');
154 1
		$sendFromAddress = $this->config->getSystemValue('mail_from_address', 'nextcloud');
155 1
		$sendFrom = $sendFromAddress . '@' . $sendFromDomain;
156
157 1
		$message = $this->mailer->createMessage();
158 1
		$message->setSubject($subject);
159 1
		$message->setFrom([$sendFrom => $this->defaults->getName()]);
160 1
		$message->setTo([$recipient => $this->l10n->t('Recipient')]);
161 1
		$message->setPlainBody($textBody);
162 1
		$message->setHtmlBody($body);
163 1
		$this->mailer->send($message);
164
165 1
		return Http::STATUS_OK;
166
	}
167
}
168