Settings::personal()   B
last analyzed

Complexity

Conditions 10
Paths 15

Size

Total Lines 53

Duplication

Lines 14
Ratio 26.42 %

Code Coverage

Tests 32
CRAP Score 10

Importance

Changes 0
Metric Value
dl 14
loc 53
ccs 32
cts 32
cp 1
rs 7.1587
c 0
b 0
f 0
cc 10
nc 15
nop 3
crap 10

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\IUser;
36
use OCP\Security\ISecureRandom;
37
38
class Settings extends Controller {
39
	/** @var \OCP\IConfig */
40
	protected $config;
41
42
	/** @var \OCP\Security\ISecureRandom */
43
	protected $random;
44
45
	/** @var \OCP\IURLGenerator */
46
	protected $urlGenerator;
47
48
	/** @var \OCA\Activity\Data */
49
	protected $data;
50
51
	/** @var \OCA\Activity\UserSettings */
52
	protected $userSettings;
53
54
	/** @var \OCP\IL10N */
55
	protected $l10n;
56
57
	/** @var IUser */
58
	protected $user;
59
60
	/**
61
	 * constructor of the controller
62
	 *
63
	 * @param string $appName
64
	 * @param IRequest $request
65
	 * @param IConfig $config
66
	 * @param ISecureRandom $random
67
	 * @param IURLGenerator $urlGenerator
68
	 * @param Data $data
69
	 * @param UserSettings $userSettings
70
	 * @param IL10N $l10n
71
	 * @param IUser $user
72
	 */
73 14 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...
74
								IRequest $request,
75
								IConfig $config,
76
								ISecureRandom $random,
77
								IURLGenerator $urlGenerator,
78
								Data $data,
79
								UserSettings $userSettings,
80
								IL10N $l10n,
81
								IUser $user) {
82 14
		parent::__construct($appName, $request);
83 14
		$this->config = $config;
84 14
		$this->random = $random;
85 14
		$this->urlGenerator = $urlGenerator;
86 14
		$this->data = $data;
87 14
		$this->userSettings = $userSettings;
88 14
		$this->l10n = $l10n;
89 14
		$this->user = $user;
90 14
	}
91
92
	/**
93
	 * @NoAdminRequired
94
	 *
95
	 * @param int $notify_setting_batchtime
96
	 * @param bool $notify_setting_self
97
	 * @param bool $notify_setting_selfemail
98
	 * @return DataResponse
99
	 */
100 3
	public function personal(
101
			$notify_setting_batchtime = UserSettings::EMAIL_SEND_HOURLY,
102
			$notify_setting_self = false,
103
			$notify_setting_selfemail = false) {
104 3
		$types = $this->data->getNotificationTypes($this->l10n);
105
106 3
		foreach ($types as $type => $data) {
107 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...
108 3
				$this->config->setUserValue(
109 3
					$this->user->getUID(), 'activity',
110 3
					'notify_email_' . $type,
111 3
					(int) $this->request->getParam($type . '_email', false)
112
				);
113
			}
114
115 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...
116 3
				$this->config->setUserValue(
117 3
					$this->user->getUID(), 'activity',
118 3
					'notify_stream_' . $type,
119 3
					(int) $this->request->getParam($type . '_stream', false)
120
				);
121
			}
122
		}
123
124 3
		$email_batch_time = 3600;
125 3
		if ($notify_setting_batchtime === UserSettings::EMAIL_SEND_DAILY) {
126 1
			$email_batch_time = 3600 * 24;
127 2
		} elseif ($notify_setting_batchtime === UserSettings::EMAIL_SEND_WEEKLY) {
128 1
			$email_batch_time = 3600 * 24 * 7;
129
		}
130
131 3
		$this->config->setUserValue(
132 3
			$this->user->getUID(), 'activity',
133 3
			'notify_setting_batchtime',
134 3
			$email_batch_time
135
		);
136 3
		$this->config->setUserValue(
137 3
			$this->user->getUID(), 'activity',
138 3
			'notify_setting_self',
139 3
			(int) $notify_setting_self
140
		);
141 3
		$this->config->setUserValue(
142 3
			$this->user->getUID(), 'activity',
143 3
			'notify_setting_selfemail',
144 3
			(int) $notify_setting_selfemail
145
		);
146
147 3
		return new DataResponse([
148
			'data'		=> [
149 3
				'message'	=> (string) $this->l10n->t('Your settings have been updated.'),
150
			],
151
		]);
152
	}
153
154
	/**
155
	 * @NoAdminRequired
156
	 * @NoCSRFRequired
157
	 *
158
	 * @return TemplateResponse
159
	 */
160 8
	public function displayPanel() {
161 8
		$types = $this->data->getNotificationTypes($this->l10n);
162
163 8
		$activities = [];
164 8
		foreach ($types as $type => $desc) {
165 1
			if (\is_array($desc)) {
166 1
				$methods = isset($desc['methods']) ? $desc['methods'] : [IExtension::METHOD_STREAM, IExtension::METHOD_MAIL];
167 1
				$desc = isset($desc['desc']) ? $desc['desc'] : '';
168
			} else {
169 1
				$methods = [IExtension::METHOD_STREAM, IExtension::METHOD_MAIL];
170
			}
171
172 1
			$activities[$type] = [
173 1
				'desc'		=> $desc,
174 1
				IExtension::METHOD_MAIL		=> $this->userSettings->getUserSetting($this->user->getUID(), 'email', $type),
175 1
				IExtension::METHOD_STREAM	=> $this->userSettings->getUserSetting($this->user->getUID(), 'stream', $type),
176 1
				'methods'	=> $methods,
177
			];
178
		}
179
180 8
		$settingBatchTime = UserSettings::EMAIL_SEND_HOURLY;
181 8
		$currentSetting = (int) $this->userSettings->getUserSetting($this->user->getUID(), 'setting', 'batchtime');
182 8
		if ($currentSetting === 3600 * 24 * 7) {
183 1
			$settingBatchTime = UserSettings::EMAIL_SEND_WEEKLY;
184 7
		} elseif ($currentSetting === 3600 * 24) {
185 1
			$settingBatchTime = UserSettings::EMAIL_SEND_DAILY;
186
		}
187
188 8
		return new TemplateResponse('activity', 'personal', [
189 8
			'activities'		=> $activities,
190 8
			'activity_email'	=> $this->user->getEMailAddress(),
191
192 8
			'setting_batchtime'	=> $settingBatchTime,
193
194 8
			'notify_self'		=> $this->userSettings->getUserSetting($this->user->getUID(), 'setting', 'self'),
195 8
			'notify_selfemail'	=> $this->userSettings->getUserSetting($this->user->getUID(), 'setting', 'selfemail'),
196
197
			'methods'			=> [
198 8
				IExtension::METHOD_MAIL => $this->l10n->t('Mail'),
199 8
				IExtension::METHOD_STREAM => $this->l10n->t('Stream'),
200
			],
201 8
		], '');
202
	}
203
204
	/**
205
	 * @NoAdminRequired
206
	 *
207
	 * @param string $enable	'true' if the feed is enabled
208
	 * @return DataResponse
209
	 */
210 2
	public function feed($enable) {
211 2
		$token = $tokenUrl = '';
212
213 2
		if ($enable === 'true') {
214 1
			$conflicts = true;
215
216
			// Check for collisions
217 1
			while (!empty($conflicts)) {
218 1
				$token = $this->random->generate(30);
219 1
				$conflicts = $this->config->getUsersForUserValue('activity', 'rsstoken', $token);
220
			}
221
222 1
			$tokenUrl = $this->urlGenerator->linkToRouteAbsolute('activity.Feed.show', ['token' => $token]);
223
		}
224
225 2
		$this->config->setUserValue($this->user->getUID(), 'activity', 'rsstoken', $token);
226
227 2
		return new DataResponse([
228
			'data'		=> [
229 2
				'message'	=> (string) $this->l10n->t('Your settings have been updated.'),
230 2
				'rsslink'	=> $tokenUrl,
231
			],
232
		]);
233
	}
234
}
235