Completed
Pull Request — master (#153)
by Maxence
02:45
created

UserSettings::getUserSetting()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 2.0145

Importance

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