Completed
Push — master ( f366d9...dee547 )
by Morris
12s
created

UserSettings   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 177
Duplicated Lines 19.21 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 0
dl 34
loc 177
ccs 39
cts 78
cp 0.5
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C getDefaultFromSetting() 0 24 7
A getUserSetting() 18 18 2
A getConfigSetting() 16 16 2
A getNotificationTypes() 0 12 3
C filterUsersBySetting() 0 40 11

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) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 *
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;
24
25
use OCP\Activity\IManager;
26
use OCP\IConfig;
27
28
/**
29
 * Class UserSettings
30
 *
31
 * @package OCA\Activity
32
 */
33
class UserSettings {
34
	/** @var IManager */
35
	protected $manager;
36
37
	/** @var IConfig */
38
	protected $config;
39
40
	/** @var Data */
41
	protected $data;
42
43
	const EMAIL_SEND_HOURLY = 0;
44
	const EMAIL_SEND_DAILY = 1;
45
	const EMAIL_SEND_WEEKLY = 2;
46
	const EMAIL_SEND_ASAP = 3;
47
48
	/**
49
	 * @param IManager $manager
50
	 * @param IConfig $config
51
	 */
52 26
	public function __construct(IManager $manager, IConfig $config) {
53 26
		$this->manager = $manager;
54 26
		$this->config = $config;
55 26
	}
56
57
	/**
58
	 * Get a setting for a user
59
	 *
60
	 * Falls back to some good default values if the user does not have a preference
61
	 *
62
	 * @param string $user
63
	 * @param string $method Should be one of 'stream', 'email' or 'setting'
64
	 * @param string $type One of the activity types, 'batchtime' or 'self'
65
	 * @return bool|int
66
	 */
67 6 View Code Duplication
	public function getUserSetting($user, $method, $type) {
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 6
		$defaultSetting = $this->getDefaultFromSetting($method, $type);
69 6
		if (is_bool($defaultSetting)) {
70 6
			return (bool) $this->config->getUserValue(
71 6
				$user,
72 6
				'activity',
73 6
				'notify_' . $method . '_' . $type,
74
				$defaultSetting
75 6
			);
76
		}
77
78 1
		return (int) $this->config->getUserValue(
79 1
			$user,
80 1
			'activity',
81 1
			'notify_' . $method . '_' . $type,
82
			$defaultSetting
83 1
		);
84
	}
85
86
	/**
87
	 * @param string $method
88
	 * @param string $type
89
	 * @return bool|int
90
	 */
91 View Code Duplication
	public function getConfigSetting($method, $type) {
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...
92
		$defaultSetting = $this->getDefaultFromSetting($method, $type);
93
		if (is_bool($defaultSetting)) {
94
			return (bool) $this->config->getAppValue(
95
				'activity',
96
				'notify_' . $method . '_' . $type,
97
				$defaultSetting
98
			);
99
		}
100
101
		return (int) $this->config->getAppValue(
102
			'activity',
103
			'notify_' . $method . '_' . $type,
104
			$defaultSetting
105
		);
106
	}
107
108
	/**
109
	 * Get a good default setting for a preference
110
	 *
111
	 * @param string $method Should be one of 'stream', 'email' or 'setting'
112
	 * @param string $type One of the activity types, 'batchtime', 'self' or 'selfemail'
113
	 * @return bool|int
114
	 */
115 12
	protected function getDefaultFromSetting($method, $type) {
116 12
		if ($method === 'setting') {
117 10
			if ($type === 'batchtime') {
118 2
				return 3600;
119
			}
120
121 9
			if ($type === 'self') {
122 7
				return true;
123
			}
124
125 4
			if ($type === 'selfemail') {
126 3
				return false;
127
			}
128
129 1
			return false;
130
		}
131
132
		try {
133 8
			$setting = $this->manager->getSettingById($type);
134 8
			return ($method === 'stream') ? $setting->isDefaultEnabledStream() : $setting->isDefaultEnabledMail();
135 5
		} catch (\InvalidArgumentException $e) {
136 5
			return false;
137
		}
138
	}
139
140
	/**
141
	 * Get a list with enabled notification types for a user
142
	 *
143
	 * @param string	$user	Name of the user
144
	 * @param string	$method	Should be one of 'stream' or 'email'
145
	 * @return array
146
	 */
147 5
	public function getNotificationTypes($user, $method) {
148 5
		$notificationTypes = array();
149
150 5
		$settings = $this->manager->getSettings();
151 5
		foreach ($settings as $setting) {
152 5
			if ($this->getUserSetting($user, $method, $setting->getIdentifier())) {
153 5
				$notificationTypes[] = $setting->getIdentifier();
154 5
			}
155 5
		}
156
157 5
		return $notificationTypes;
158
	}
159
160
	/**
161
	 * Filters the given user array by their notification setting
162
	 *
163
	 * @param array $users
164
	 * @param string $method
165
	 * @param string $type
166
	 * @return array Returns a "username => b:true" Map for method = stream
167
	 *               Returns a "username => i:batchtime" Map for method = email
168
	 */
169
	public function filterUsersBySetting($users, $method, $type) {
170
		if (empty($users) || !is_array($users)) {
171
			return array();
172
		}
173
174
		$filteredUsers = array();
175
		$potentialUsers = $this->config->getUserValueForUsers('activity', 'notify_' . $method . '_' . $type, $users);
176
		foreach ($potentialUsers as $user => $value) {
177
			if ($value) {
178
				$filteredUsers[$user] = true;
179
			}
180
			unset($users[array_search($user, $users, true)]);
181
		}
182
183
		// Get the batch time setting from the database
184
		if ($method === 'email') {
185
			$potentialUsers = $this->config->getUserValueForUsers('activity', 'notify_setting_batchtime', array_keys($filteredUsers));
186
			foreach ($potentialUsers as $user => $value) {
187
				$filteredUsers[$user] = $value;
188
			}
189
		}
190
191
		if (empty($users)) {
192
			return $filteredUsers;
193
		}
194
195
		// If the setting is enabled by default,
196
		// we add all users that didn't set the preference yet.
197
		if ($this->getDefaultFromSetting($method, $type)) {
198
			foreach ($users as $user) {
199
				if ($method === 'stream') {
200
					$filteredUsers[$user] = true;
201
				} else {
202
					$filteredUsers[$user] = $this->getDefaultFromSetting('setting', 'batchtime');
203
				}
204
			}
205
		}
206
207
		return $filteredUsers;
208
	}
209
}
210