Completed
Push — federated-circles ( 915d64...66c054 )
by Maxence
03:31
created

ConfigService::deleteAppValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Service;
28
29
use OCA\Circles\Model\Circle;
30
use OCP\IConfig;
31
use OCP\Util;
32
33
class ConfigService {
34
35
36
	const CIRCLES_ALLOW_CIRCLES = 'allow_circles';
37
	const CIRCLES_SWAP_TO_TEAMS = 'swap_to_teams';
38
	const CIRCLES_ALLOW_FEDERATED = 'allow_federated';
39
40
	private $defaults = [
41
		self::CIRCLES_ALLOW_CIRCLES   => Circle::CIRCLES_ALL,
42
		self::CIRCLES_SWAP_TO_TEAMS   => '0',
43
		self::CIRCLES_ALLOW_FEDERATED => '1'
44
	];
45
46
	/** @var string */
47
	private $appName;
48
49
	/** @var IConfig */
50
	private $config;
51
52
	/** @var string */
53
	private $userId;
54
55
	/** @var MiscService */
56
	private $miscService;
57
58
	/** @var int */
59
	private $allowedCircle = -1;
60
61
	/** @var int */
62
	private $allowedFederated = -1;
63
64
	public function __construct($appName, IConfig $config, $userId, MiscService $miscService) {
65
		$this->appName = $appName;
66
		$this->config = $config;
67
		$this->userId = $userId;
68
		$this->miscService = $miscService;
69
	}
70
71
72
	/**
73
	 * returns if this type of circle is allowed by the current configuration.
74
	 *
75
	 * @param $type
76
	 *
77
	 * @return int
78
	 */
79
	public function isCircleAllowed($type) {
80
		if ($this->allowedCircle === -1) {
81
			$this->allowedCircle = (int)$this->getAppValue(self::CIRCLES_ALLOW_CIRCLES);
82
		}
83
84
		return ((int)$type & (int)$this->allowedCircle);
85
	}
86
87
88
	public function isFederatedAllowed() {
89
		if ($this->allowedFederated === -1) {
90
			$this->allowedFederated = (int)$this->getAppValue(self::CIRCLES_ALLOW_FEDERATED);
91
		}
92
93
		return ($this->allowedFederated === 1);
94
	}
95
96
	/**
97
	 * Get a value by key
98
	 *
99
	 * @param string $key
100
	 *
101
	 * @return string
102
	 */
103
	public function getAppValue($key) {
104
		$defaultValue = null;
105
106
		if (array_key_exists($key, $this->defaults)) {
107
			$defaultValue = $this->defaults[$key];
108
		}
109
110
		return $this->config->getAppValue($this->appName, $key, $defaultValue);
111
	}
112
113
	/**
114
	 * Set a value by key
115
	 *
116
	 * @param string $key
117
	 * @param string $value
118
	 *
119
	 * @return void
120
	 */
121
	public function setAppValue($key, $value) {
122
		$this->config->setAppValue($this->appName, $key, $value);
123
	}
124
125
	/**
126
	 * remove a key
127
	 *
128
	 * @param string $key
129
	 *
130
	 * @return string
131
	 */
132
	public function deleteAppValue($key) {
133
		return $this->config->deleteAppValue($this->appName, $key);
134
	}
135
136
	/**
137
	 * Get a user value by key
138
	 *
139
	 * @param string $key
140
	 *
141
	 * @return string
142
	 */
143
	public function getUserValue($key) {
144
		return $this->config->getUserValue($this->userId, $this->appName, $key);
145
	}
146
147
	/**
148
	 * Set a user value by key
149
	 *
150
	 * @param string $key
151
	 * @param string $value
152
	 *
153
	 * @return string
154
	 */
155
	public function setUserValue($key, $value) {
156
		return $this->config->setUserValue($this->userId, $this->appName, $key, $value);
157
	}
158
159
	/**
160
	 * Get a user value by key and user
161
	 *
162
	 * @param string $userId
163
	 * @param string $key
164
	 *
165
	 * @return string
166
	 */
167
	public function getValueForUser($userId, $key) {
168
		return $this->config->getUserValue($userId, $this->appName, $key);
169
	}
170
171
	/**
172
	 * Set a user value by key
173
	 *
174
	 * @param string $userId
175
	 * @param string $key
176
	 * @param string $value
177
	 *
178
	 * @return string
179
	 */
180
	public function setValueForUser($userId, $key, $value) {
181
		return $this->config->setUserValue($userId, $this->appName, $key, $value);
182
	}
183
184
	/**
185
	 * return the cloud version.
186
	 * if $complete is true, return a string x.y.z
187
	 *
188
	 * @param boolean $complete
189
	 *
190
	 * @return string|integer
191
	 */
192
	public function getCloudVersion($complete = false) {
193
		$ver = Util::getVersion();
194
195
		if ($complete) {
196
			return implode('.', $ver);
197
		}
198
199
		return $ver[0];
200
	}
201
}
202