Completed
Push — master ( 4c6036...8838ed )
by Morris
11:46
created

NewUserMailHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 131
Duplicated Lines 14.5 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
dl 19
loc 131
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 19 19 1
A setL10N() 0 3 1
B generateTemplate() 0 47 5
A sendMail() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Lukas Reschke <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
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
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OC\Settings\Mailer;
23
24
use OC\Mail\EMailTemplate;
25
use OC\Mail\IEMailTemplate;
26
use OCA\Theming\ThemingDefaults;
27
use OCP\AppFramework\Utility\ITimeFactory;
28
use OCP\IConfig;
29
use OCP\IL10N;
30
use OCP\IURLGenerator;
31
use OCP\IUser;
32
use OCP\Mail\IMailer;
33
use OCP\Security\ICrypto;
34
use OCP\Security\ISecureRandom;
35
36
class NewUserMailHelper {
37
	/** @var ThemingDefaults */
38
	private $themingDefaults;
39
	/** @var IURLGenerator */
40
	private $urlGenerator;
41
	/** @var IL10N */
42
	private $l10n;
43
	/** @var IMailer */
44
	private $mailer;
45
	/** @var ISecureRandom */
46
	private $secureRandom;
47
	/** @var ITimeFactory */
48
	private $timeFactory;
49
	/** @var IConfig */
50
	private $config;
51
	/** @var ICrypto */
52
	private $crypto;
53
	/** @var string */
54
	private $fromAddress;
55
56
	/**
57
	 * @param ThemingDefaults $themingDefaults
58
	 * @param IURLGenerator $urlGenerator
59
	 * @param IL10N $l10n
60
	 * @param IMailer $mailer
61
	 * @param ISecureRandom $secureRandom
62
	 * @param ITimeFactory $timeFactory
63
	 * @param IConfig $config
64
	 * @param ICrypto $crypto
65
	 * @param string $fromAddress
66
	 */
67 View Code Duplication
	public function __construct(ThemingDefaults $themingDefaults,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
								IURLGenerator $urlGenerator,
69
								IL10N $l10n,
70
								IMailer $mailer,
71
								ISecureRandom $secureRandom,
72
								ITimeFactory $timeFactory,
73
								IConfig $config,
74
								ICrypto $crypto,
75
								$fromAddress) {
76
		$this->themingDefaults = $themingDefaults;
77
		$this->urlGenerator = $urlGenerator;
78
		$this->l10n = $l10n;
79
		$this->mailer = $mailer;
80
		$this->secureRandom = $secureRandom;
81
		$this->timeFactory = $timeFactory;
82
		$this->config = $config;
83
		$this->crypto = $crypto;
84
		$this->fromAddress = $fromAddress;
85
	}
86
87
	/**
88
	 * Set the IL10N object
89
	 *
90
	 * @param IL10N $l10n
91
	 */
92
	public function setL10N(IL10N $l10n) {
93
		$this->l10n = $l10n;
94
	}
95
96
	/**
97
	 * @param IUser $user
98
	 * @param bool $generatePasswordResetToken
99
	 * @return EMailTemplate
100
	 */
101
	public function generateTemplate(IUser $user, $generatePasswordResetToken = false) {
102
		if ($generatePasswordResetToken) {
103
			$token = $this->secureRandom->generate(
104
				21,
105
				ISecureRandom::CHAR_DIGITS .
106
				ISecureRandom::CHAR_LOWER .
107
				ISecureRandom::CHAR_UPPER
108
			);
109
			$tokenValue = $this->timeFactory->getTime() . ':' . $token;
110
			$mailAddress = (null !== $user->getEMailAddress()) ? $user->getEMailAddress() : '';
111
			$encryptedValue = $this->crypto->encrypt($tokenValue, $mailAddress . $this->config->getSystemValue('secret'));
112
			$this->config->setUserValue($user->getUID(), 'core', 'lostpassword', $encryptedValue);
113
			$link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', ['userId' => $user->getUID(), 'token' => $token]);
114
		} else {
115
			$link = $this->urlGenerator->getAbsoluteURL('/');
116
		}
117
118
		$emailTemplate = new EMailTemplate(
119
			$this->themingDefaults,
120
			$this->urlGenerator,
121
			$this->l10n
122
		);
123
		$emailTemplate->addHeader();
124
		$displayName = $user->getDisplayName();
125
		$userName = $user->getUID();
126
		if ($displayName === $userName) {
127
			$emailTemplate->addHeading($this->l10n->t('Welcome aboard'));
128
		} else {
129
			$emailTemplate->addHeading($this->l10n->t('Welcome aboard %s', [$displayName]));
130
		}
131
		$emailTemplate->addBodyText($this->l10n->t('You have now an %s account, you can add, protect, and share your data.', [$this->themingDefaults->getName()]));
132
		$emailTemplate->addBodyText($this->l10n->t('Your username is: %s', [$userName]));
133
		if ($generatePasswordResetToken) {
134
			$leftButtonText = $this->l10n->t('Set your password');
135
		} else {
136
			$leftButtonText = $this->l10n->t('Go to %s', [$this->themingDefaults->getName()]);
137
		}
138
		$emailTemplate->addBodyButtonGroup(
139
			$leftButtonText,
140
			$link,
141
			$this->l10n->t('Install Client'),
142
			'https://nextcloud.com/install/#install-clients'
143
		);
144
		$emailTemplate->addFooter();
145
146
		return $emailTemplate;
147
	}
148
149
	/**
150
	 * Sends a welcome mail to $user
151
	 *
152
	 * @param IUser $user
153
	 * @param IEmailTemplate $emailTemplate
154
	 * @throws \Exception If mail could not be sent
155
	 */
156
	public function sendMail(IUser $user,
157
							 IEMailTemplate $emailTemplate) {
158
		$message = $this->mailer->createMessage();
159
		$message->setTo([$user->getEMailAddress() => $user->getDisplayName()]);
160
		$message->setSubject($this->l10n->t('Your %s account was created', [$this->themingDefaults->getName()]));
161
		$message->setHtmlBody($emailTemplate->renderHTML());
162
		$message->setPlainBody($emailTemplate->renderText());
163
		$message->setFrom([$this->fromAddress => $this->themingDefaults->getName()]);
164
		$this->mailer->send($message);
165
	}
166
}
167