UserSettings::filterUsersBySetting()   B
last analyzed

Complexity

Conditions 11
Paths 19

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 11

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 21
cts 21
cp 1
rs 7.3166
c 0
b 0
f 0
cc 11
nc 19
nop 3
crap 11

How to fix   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
 *
5
 * @copyright Copyright (c) 2016, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Activity;
23
24
use OCP\Activity\IManager;
25
use OCP\IConfig;
26
use OCP\Util;
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
	 * @param Data $data
51
	 */
52 26
	public function __construct(IManager $manager, IConfig $config, Data $data) {
53 26
		$this->manager = $manager;
54 26
		$this->config = $config;
55 26
		$this->data = $data;
56 26
	}
57
58
	/**
59
	 * Get a setting for a user
60
	 *
61
	 * Falls back to some good default values if the user does not have a preference
62
	 *
63
	 * @param string $user
64
	 * @param string $method Should be one of 'stream', 'email' or 'setting'
65
	 * @param string $type One of the activity types, 'batchtime' or 'self'
66
	 * @return bool|int
67
	 */
68 6
	public function getUserSetting($user, $method, $type) {
69 6
		$defaultSetting = $this->getDefaultSetting($method, $type);
70 6
		if (\is_bool($defaultSetting)) {
71 6
			return (bool) $this->config->getUserValue(
72 6
				$user,
73 6
				'activity',
74 6
				'notify_' . $method . '_' . $type,
75 6
				$defaultSetting
76
			);
77
		} else {
78
			return (int) $this->config->getUserValue(
79
				$user,
80
				'activity',
81
				'notify_' . $method . '_' . $type,
82
				$defaultSetting
83
			);
84
		}
85
	}
86
87
	/**
88
	 * Get a good default setting for a preference
89
	 *
90
	 * @param string $method Should be one of 'stream', 'email' or 'setting'
91
	 * @param string $type One of the activity types, 'batchtime', 'self' or 'selfemail'
92
	 * @return bool|int
93
	 */
94 18
	public function getDefaultSetting($method, $type) {
95 18
		if ($method === 'setting') {
96 10
			if ($type === 'batchtime') {
97 2
				return 3600;
98 8
			} elseif ($type === 'self') {
99 6
				return true;
100 2
			} elseif ($type === 'selfemail') {
101 1
				return false;
102
			}
103
		}
104
105 15
		$settings = $this->manager->getDefaultTypes($method);
106 15
		return \in_array($type, $settings);
107
	}
108
109
	/**
110
	 * Get a list with enabled notification types for a user
111
	 *
112
	 * @param string	$user	Name of the user
113
	 * @param string	$method	Should be one of 'stream' or 'email'
114
	 * @return array
115
	 */
116 6
	public function getNotificationTypes($user, $method) {
117 6
		$l = Util::getL10N('activity');
118 6
		$types = $this->data->getNotificationTypes($l);
119
120 6
		$notificationTypes = [];
121 6
		foreach ($types as $type => $desc) {
122 6
			if ($this->getUserSetting($user, $method, $type)) {
123 6
				$notificationTypes[] = $type;
124
			}
125
		}
126
127 6
		return $notificationTypes;
128
	}
129
130
	/**
131
	 * Filters the given user array by their notification setting
132
	 *
133
	 * @param array $users
134
	 * @param string $method
135
	 * @param string $type
136
	 * @return array Returns a "username => b:true" Map for method = stream
137
	 *               Returns a "username => i:batchtime" Map for method = email
138
	 */
139 8
	public function filterUsersBySetting($users, $method, $type) {
140 8
		if (empty($users) || !\is_array($users)) {
141 1
			return [];
142
		}
143
144 7
		$filteredUsers = [];
145 7
		$potentialUsers = $this->config->getUserValueForUsers('activity', 'notify_' . $method . '_' . $type, $users);
146 7
		foreach ($potentialUsers as $user => $value) {
147 7
			if ($value) {
148 7
				$filteredUsers[$user] = true;
149
			}
150 7
			unset($users[\array_search($user, $users)]);
151
		}
152
153
		// Get the batch time setting from the database
154 7
		if ($method === 'email') {
155 4
			$potentialUsers = $this->config->getUserValueForUsers('activity', 'notify_setting_batchtime', \array_keys($filteredUsers));
156 4
			foreach ($potentialUsers as $user => $value) {
157 4
				$filteredUsers[$user] = $value;
158
			}
159
		}
160
161 7
		if (empty($users)) {
162 1
			return $filteredUsers;
163
		}
164
165
		// If the setting is enabled by default,
166
		// we add all users that didn't set the preference yet.
167 6
		if ($this->getDefaultSetting($method, $type)) {
168 2
			foreach ($users as $user) {
169 2
				if ($method === 'stream') {
170 1
					$filteredUsers[$user] = true;
171
				} else {
172 2
					$filteredUsers[$user] = $this->getDefaultSetting('setting', 'batchtime');
173
				}
174
			}
175
		}
176
177 6
		return $filteredUsers;
178
	}
179
}
180