Completed
Push — some-scrutinizing ( 26da3a...6d8498 )
by Maxence
02:28
created

ConfigService   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 155
rs 10
c 3
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isCircleAllowed() 0 7 2
A getAppValue() 0 9 2
A setAppValue() 0 3 1
A deleteAppValue() 0 3 1
A getUserValue() 0 3 1
A setUserValue() 0 3 1
A getValueForUser() 0 3 1
A setValueForUser() 0 3 1
A getCloudVersion() 0 9 2
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
39
	private $defaults = [
40
		self::CIRCLES_ALLOW_CIRCLES => Circle::CIRCLES_ALL,
41
		self::CIRCLES_SWAP_TO_TEAMS => '0'
42
	];
43
44
	/** @var string */
45
	private $appName;
46
47
	/** @var IConfig */
48
	private $config;
49
50
	/** @var string */
51
	private $userId;
52
53
	/** @var MiscService */
54
	private $miscService;
55
56
	/** @var int */
57
	private $allowedCircle = -1;
58
59
	public function __construct($appName, IConfig $config, $userId, MiscService $miscService) {
60
		$this->appName = $appName;
61
		$this->config = $config;
62
		$this->userId = $userId;
63
		$this->miscService = $miscService;
64
	}
65
66
67
	/**
68
	 * returns if this type of circle is allowed by the current configuration.
69
	 *
70
	 * @param $type
71
	 *
72
	 * @return int
73
	 */
74
	public function isCircleAllowed($type) {
75
		if ($this->allowedCircle === -1) {
76
			$this->allowedCircle = (int) $this->getAppValue(self::CIRCLES_ALLOW_CIRCLES);
77
		}
78
79
		return ((int)$type & (int)$this->allowedCircle);
80
	}
81
82
	/**
83
	 * Get a value by key
84
	 *
85
	 * @param string $key
86
	 *
87
	 * @return string
88
	 */
89
	public function getAppValue($key) {
90
		$defaultValue = null;
91
92
		if (array_key_exists($key, $this->defaults)) {
93
			$defaultValue = $this->defaults[$key];
94
		}
95
96
		return $this->config->getAppValue($this->appName, $key, $defaultValue);
97
	}
98
99
	/**
100
	 * Set a value by key
101
	 *
102
	 * @param string $key
103
	 * @param string $value
104
	 *
105
	 * @return void
106
	 */
107
	public function setAppValue($key, $value) {
108
		$this->config->setAppValue($this->appName, $key, $value);
109
	}
110
111
	/**
112
	 * remove a key
113
	 *
114
	 * @param string $key
115
	 *
116
	 * @return string
117
	 */
118
	public function deleteAppValue($key) {
119
		return $this->config->deleteAppValue($this->appName, $key);
120
	}
121
122
	/**
123
	 * Get a user value by key
124
	 *
125
	 * @param string $key
126
	 *
127
	 * @return string
128
	 */
129
	public function getUserValue($key) {
130
		return $this->config->getUserValue($this->userId, $this->appName, $key);
131
	}
132
133
	/**
134
	 * Set a user value by key
135
	 *
136
	 * @param string $key
137
	 * @param string $value
138
	 *
139
	 * @return string
140
	 */
141
	public function setUserValue($key, $value) {
142
		return $this->config->setUserValue($this->userId, $this->appName, $key, $value);
143
	}
144
145
	/**
146
	 * Get a user value by key and user
147
	 *
148
	 * @param string $userId
149
	 * @param string $key
150
	 *
151
	 * @return string
152
	 */
153
	public function getValueForUser($userId, $key) {
154
		return $this->config->getUserValue($userId, $this->appName, $key);
155
	}
156
157
	/**
158
	 * Set a user value by key
159
	 *
160
	 * @param string $userId
161
	 * @param string $key
162
	 * @param string $value
163
	 *
164
	 * @return string
165
	 */
166
	public function setValueForUser($userId, $key, $value) {
167
		return $this->config->setUserValue($userId, $this->appName, $key, $value);
168
	}
169
170
	/**
171
	 * return the cloud version.
172
	 * if $complete is true, return a string x.y.z
173
	 *
174
	 * @param boolean $complete
175
	 *
176
	 * @return string|integer
177
	 */
178
	public function getCloudVersion($complete = false) {
179
		$ver = Util::getVersion();
180
181
		if ($complete) {
182
			return implode('.', $ver);
183
		}
184
185
		return $ver[0];
186
	}
187
}
188