EmailController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 65.63%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 97
ccs 21
cts 32
cp 0.6563
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A sendEmailPublicLink() 0 16 1
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 $to
82
	 * @param string $url
83
	 * @param string $name
84
	 * @return JSONResponse
85
	 * @NoAdminRequired
86
	 */
87 2
	public function sendEmailPublicLink($to, $url, $name) {
88
89 2
		$user = $this->userSession->getUser();
90 2
		$username = $user->getDisplayName();
91
92 2
		$subject = $this->l10n->t('%s has published the calendar "%s"', [$username, $name]);
93
94 2
		$emailTemplateHTML = new TemplateResponse('calendar', 'mail.publication.html', ['subject' => $subject, 'username' => $username, 'calendarname' => $name, 'calendarurl' => $url, 'defaults' => $this->defaults], 'public');
95 2
		$bodyHTML = $emailTemplateHTML->render();
96 2
		$emailTemplateText = new TemplateResponse('calendar', 'mail.publication.text', ['subject' => $subject, 'username' => $username, 'calendarname' => $name, 'calendarurl' => $url], 'blank');
97 2
		$textBody = $emailTemplateText->render();
98
99 2
		$status = $this->sendEmail($to, $subject, $bodyHTML, $textBody);
100
101 2
		return new JSONResponse([], $status);
102
	}
103
104
	/**
105
	 * @param string $target
106
	 * @param string $subject
107
	 * @param string $body
108
	 * @param string $textBody
109
	 * @return int
110
	 */
111 2
	private function sendEmail($target, $subject, $body, $textBody) {
112 2
		if (!$this->mailer->validateMailAddress($target)) {
113 2
			return Http::STATUS_BAD_REQUEST;
114
		}
115
116
		$sendFromDomain = $this->config->getSystemValue('mail_domain', 'domain.org');
117
		$sendFromAddress = $this->config->getSystemValue('mail_from_address', 'owncloud');
118
		$sendFrom = $sendFromAddress . '@' . $sendFromDomain;
119
120
		$message = $this->mailer->createMessage();
121
		$message->setSubject($subject);
122
		$message->setFrom([$sendFrom => $this->defaults->getName()]);
123
		$message->setTo([$target => 'Recipient']);
124
		$message->setPlainBody($textBody);
125
		$message->setHtmlBody($body);
126
		$this->mailer->send($message);
127
128
		return Http::STATUS_OK;
129
	}
130
}
131