Completed
Pull Request — master (#129)
by Jan-Christoph
52:33 queued 50:39
created

UserSettings   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 58.2%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
lcom 1
cbo 0
dl 0
loc 149
ccs 39
cts 67
cp 0.582
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserSetting() 0 18 2
C filterUsersBySetting() 0 40 11
A __construct() 0 4 1
B getDefaultSetting() 0 19 7
A getNotificationTypes() 0 12 3
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
	public function getUserSetting($user, $method, $type) {
67 6
		$defaultSetting = $this->getDefaultSetting($method, $type);
68 6
		if (is_bool($defaultSetting)) {
69 6
			return (bool) $this->config->getUserValue(
70 6
				$user,
71 6
				'activity',
72 6
				'notify_' . $method . '_' . $type,
73
				$defaultSetting
74 6
			);
75
		} else {
76 1
			return (int) $this->config->getUserValue(
77 1
				$user,
78 1
				'activity',
79 1
				'notify_' . $method . '_' . $type,
80
				$defaultSetting
81 1
			);
82
		}
83
	}
84
85
	/**
86
	 * Get a good default setting for a preference
87
	 *
88
	 * @param string $method Should be one of 'stream', 'email' or 'setting'
89
	 * @param string $type One of the activity types, 'batchtime', 'self' or 'selfemail'
90
	 * @return bool|int
91
	 */
92 12
	public function getDefaultSetting($method, $type) {
93 12
		if ($method === 'setting') {
94 10
			if ($type === 'batchtime') {
95 2
				return 3600;
96 9
			} else if ($type === 'self') {
97 7
				return true;
98 4
			} else if ($type === 'selfemail') {
99 3
				return false;
100
			}
101 1
			return false;
102
		}
103
104
		try {
105 8
			$setting = $this->manager->getSettingById($type);
106 8
			return ($method === 'stream') ? $setting->isDefaultEnabledStream() : $setting->isDefaultEnabledMail();
107 5
		} catch (\InvalidArgumentException $e) {
108 5
			return false;
109
		}
110
	}
111
112
	/**
113
	 * Get a list with enabled notification types for a user
114
	 *
115
	 * @param string	$user	Name of the user
116
	 * @param string	$method	Should be one of 'stream' or 'email'
117
	 * @return array
118
	 */
119 5
	public function getNotificationTypes($user, $method) {
120 5
		$notificationTypes = array();
121
122 5
		$settings = $this->manager->getSettings();
123 5
		foreach ($settings as $setting) {
124 5
			if ($this->getUserSetting($user, $method, $setting->getIdentifier())) {
125 5
				$notificationTypes[] = $setting->getIdentifier();
126 5
			}
127 5
		}
128
129 5
		return $notificationTypes;
130
	}
131
132
	/**
133
	 * Filters the given user array by their notification setting
134
	 *
135
	 * @param array $users
136
	 * @param string $method
137
	 * @param string $type
138
	 * @return array Returns a "username => b:true" Map for method = stream
139
	 *               Returns a "username => i:batchtime" Map for method = email
140
	 */
141
	public function filterUsersBySetting($users, $method, $type) {
142
		if (empty($users) || !is_array($users)) {
143
			return array();
144
		}
145
146
		$filteredUsers = array();
147
		$potentialUsers = $this->config->getUserValueForUsers('activity', 'notify_' . $method . '_' . $type, $users);
148
		foreach ($potentialUsers as $user => $value) {
149
			if ($value) {
150
				$filteredUsers[$user] = true;
151
			}
152
			unset($users[array_search($user, $users)]);
153
		}
154
155
		// Get the batch time setting from the database
156
		if ($method === 'email') {
157
			$potentialUsers = $this->config->getUserValueForUsers('activity', 'notify_setting_batchtime', array_keys($filteredUsers));
158
			foreach ($potentialUsers as $user => $value) {
159
				$filteredUsers[$user] = $value;
160
			}
161
		}
162
163
		if (empty($users)) {
164
			return $filteredUsers;
165
		}
166
167
		// If the setting is enabled by default,
168
		// we add all users that didn't set the preference yet.
169
		if ($this->getDefaultSetting($method, $type)) {
170
			foreach ($users as $user) {
171
				if ($method === 'stream') {
172
					$filteredUsers[$user] = true;
173
				} else {
174
					$filteredUsers[$user] = $this->getDefaultSetting('setting', 'batchtime');
175
				}
176
			}
177
		}
178
179
		return $filteredUsers;
180
	}
181
}
182