Completed
Pull Request — master (#505)
by Joas
03:22
created

Settings   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 197
Duplicated Lines 16.24 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 21
c 2
b 0
f 0
lcom 1
cbo 2
dl 32
loc 197
ccs 98
cts 98
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 18 18 1
C personal() 14 53 10
C displayPanel() 0 43 7
B feed() 0 24 3

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
 * @author Joas Schilling <[email protected]>
4
 * @author Lukas Reschke <[email protected]>
5
 *
6
 * @copyright Copyright (c) 2016, ownCloud, Inc.
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\Activity\Controller;
24
25
use OCA\Activity\Data;
26
use OCA\Activity\UserSettings;
27
use OCP\Activity\IExtension;
28
use OCP\AppFramework\Controller;
29
use OCP\AppFramework\Http\DataResponse;
30
use OCP\AppFramework\Http\TemplateResponse;
31
use OCP\IConfig;
32
use OCP\IL10N;
33
use OCP\IRequest;
34
use OCP\IURLGenerator;
35
use OCP\Security\ISecureRandom;
36
37
class Settings extends Controller {
38
	/** @var \OCP\IConfig */
39
	protected $config;
40
41
	/** @var \OCP\Security\ISecureRandom */
42
	protected $random;
43
44
	/** @var \OCP\IURLGenerator */
45
	protected $urlGenerator;
46
47
	/** @var \OCA\Activity\Data */
48
	protected $data;
49
50
	/** @var \OCA\Activity\UserSettings */
51
	protected $userSettings;
52
53
	/** @var \OCP\IL10N */
54
	protected $l10n;
55
56
	/** @var string */
57
	protected $user;
58
59
	/**
60
	 * constructor of the controller
61
	 *
62
	 * @param string $appName
63
	 * @param IRequest $request
64
	 * @param IConfig $config
65
	 * @param ISecureRandom $random
66
	 * @param IURLGenerator $urlGenerator
67
	 * @param Data $data
68
	 * @param UserSettings $userSettings
69
	 * @param IL10N $l10n
70
	 * @param string $user
71
	 */
72 15 View Code Duplication
	public function __construct($appName,
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...
73
								IRequest $request,
74
								IConfig $config,
75
								ISecureRandom $random,
76
								IURLGenerator $urlGenerator,
77
								Data $data,
78
								UserSettings $userSettings,
79
								IL10N $l10n,
80
								$user) {
81 15
		parent::__construct($appName, $request);
82 15
		$this->config = $config;
83 15
		$this->random = $random;
84 15
		$this->urlGenerator = $urlGenerator;
85 15
		$this->data = $data;
86 15
		$this->userSettings = $userSettings;
87 15
		$this->l10n = $l10n;
88 15
		$this->user = $user;
89 15
	}
90
91
	/**
92
	 * @NoAdminRequired
93
	 *
94
	 * @param int $notify_setting_batchtime
95
	 * @param bool $notify_setting_self
96
	 * @param bool $notify_setting_selfemail
97
	 * @return DataResponse
98
	 */
99 3
	public function personal(
100
			$notify_setting_batchtime = UserSettings::EMAIL_SEND_HOURLY,
101
			$notify_setting_self = false,
102
			$notify_setting_selfemail = false) {
103 3
		$types = $this->data->getNotificationTypes($this->l10n);
104
105 3
		foreach ($types as $type => $data) {
106 3 View Code Duplication
			if (!is_array($data) || (isset($data['methods']) && in_array(IExtension::METHOD_MAIL, $data['methods']))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
107 3
				$this->config->setUserValue(
108 3
					$this->user, 'activity',
109 3
					'notify_email_' . $type,
110 3
					(int) $this->request->getParam($type . '_email', false)
111 3
				);
112 3
			}
113
114 3 View Code Duplication
			if (!is_array($data) || (isset($data['methods']) && in_array(IExtension::METHOD_STREAM, $data['methods']))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
115 3
				$this->config->setUserValue(
116 3
					$this->user, 'activity',
117 3
					'notify_stream_' . $type,
118 3
					(int) $this->request->getParam($type . '_stream', false)
119 3
				);
120 3
			}
121 3
		}
122
123 3
		$email_batch_time = 3600;
124 3
		if ($notify_setting_batchtime === UserSettings::EMAIL_SEND_DAILY) {
125 1
			$email_batch_time = 3600 * 24;
126 3
		} else if ($notify_setting_batchtime === UserSettings::EMAIL_SEND_WEEKLY) {
127 1
			$email_batch_time = 3600 * 24 * 7;
128 1
		}
129
130 3
		$this->config->setUserValue(
131 3
			$this->user, 'activity',
132 3
			'notify_setting_batchtime',
133
			$email_batch_time
134 3
		);
135 3
		$this->config->setUserValue(
136 3
			$this->user, 'activity',
137 3
			'notify_setting_self',
138
			(int) $notify_setting_self
139 3
		);
140 3
		$this->config->setUserValue(
141 3
			$this->user, 'activity',
142 3
			'notify_setting_selfemail',
143
			(int) $notify_setting_selfemail
144 3
		);
145
146 3
		return new DataResponse(array(
147
			'data'		=> array(
148 3
				'message'	=> (string) $this->l10n->t('Your settings have been updated.'),
149 3
			),
150 3
		));
151
	}
152
153
	/**
154
	 * @NoAdminRequired
155
	 * @NoCSRFRequired
156
	 *
157
	 * @return TemplateResponse
158
	 */
159 9
	public function displayPanel() {
160 9
		$types = $this->data->getNotificationTypes($this->l10n);
161
162 9
		$activities = array();
163 9
		foreach ($types as $type => $desc) {
164 2
			if (is_array($desc)) {
165 2
				$methods = isset($desc['methods']) ? $desc['methods'] : [IExtension::METHOD_STREAM, IExtension::METHOD_MAIL];
166 2
				$desc = isset($desc['desc']) ? $desc['desc'] : '';
167 2
			} else {
168 2
				$methods = [IExtension::METHOD_STREAM, IExtension::METHOD_MAIL];
169
			}
170
171 2
			$activities[$type] = array(
172 2
				'desc'		=> $desc,
173 2
				IExtension::METHOD_MAIL		=> $this->userSettings->getUserSetting($this->user, 'email', $type),
174 2
				IExtension::METHOD_STREAM	=> $this->userSettings->getUserSetting($this->user, 'stream', $type),
175 2
				'methods'	=> $methods,
176
			);
177 9
		}
178
179 9
		$settingBatchTime = UserSettings::EMAIL_SEND_HOURLY;
180 9
		$currentSetting = (int) $this->userSettings->getUserSetting($this->user, 'setting', 'batchtime');
181 9
		if ($currentSetting === 3600 * 24 * 7) {
182 1
			$settingBatchTime = UserSettings::EMAIL_SEND_WEEKLY;
183 9
		} else if ($currentSetting === 3600 * 24) {
184 1
			$settingBatchTime = UserSettings::EMAIL_SEND_DAILY;
185 1
		}
186
187 9
		return new TemplateResponse('activity', 'personal', [
188 9
			'activities'		=> $activities,
189 9
			'activity_email'	=> $this->config->getUserValue($this->user, 'settings', 'email', ''),
190
191 9
			'setting_batchtime'	=> $settingBatchTime,
192
193 9
			'notify_self'		=> $this->userSettings->getUserSetting($this->user, 'setting', 'self'),
194 9
			'notify_selfemail'	=> $this->userSettings->getUserSetting($this->user, 'setting', 'selfemail'),
195
196
			'methods'			=> [
197 9
				IExtension::METHOD_MAIL => $this->l10n->t('Mail'),
198 9
				IExtension::METHOD_STREAM => $this->l10n->t('Stream'),
199 9
			],
200 9
		], '');
201
	}
202
203
	/**
204
	 * @NoAdminRequired
205
	 *
206
	 * @param string $enable	'true' if the feed is enabled
207
	 * @return DataResponse
208
	 */
209 2
	public function feed($enable) {
210 2
		$token = $tokenUrl = '';
211
212 2
		if ($enable === 'true') {
213 1
			$conflicts = true;
214
215
			// Check for collisions
216 1
			while (!empty($conflicts)) {
217 1
				$token = $this->random->generate(30);
218 1
				$conflicts = $this->config->getUsersForUserValue('activity', 'rsstoken', $token);
219 1
			}
220
221 1
			$tokenUrl = $this->urlGenerator->linkToRouteAbsolute('activity.Feed.show', ['token' => $token]);
222 1
		}
223
224 2
		$this->config->setUserValue($this->user, 'activity', 'rsstoken', $token);
225
226 2
		return new DataResponse(array(
227
			'data'		=> array(
228 2
				'message'	=> (string) $this->l10n->t('Your settings have been updated.'),
229 2
				'rsslink'	=> $tokenUrl,
230 2
			),
231 2
		));
232
	}
233
}
234