Completed
Pull Request — master (#335)
by
unknown
21:24
created

SettingsController::getConfig()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.0208

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 6
nop 1
dl 0
loc 16
ccs 11
cts 12
cp 0.9167
crap 6.0208
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2016 Georg Ehrke <[email protected]>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or any later version.
12
 *
13
 * This library 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
19
 * License along with this library.  If not, see <http://www.gnu.org/g/>.
20
 *
21
 */
22
namespace OCA\Calendar\Controller;
23
24
use OCP\AppFramework\Controller;
25
use OCP\AppFramework\Http\JSONResponse;
26
use OCP\AppFramework\Http;
27
use OCP\IConfig;
28
use OCP\IRequest;
29
use OCP\IUserSession;
30
31
class SettingsController extends Controller {
32
33
	/**
34
	 * @var IConfig
35
	 */
36
	private $config;
37
38
	/**
39
	 * @var IUserSession
40
	 */
41
	private $userSession;
42
43
	/**
44
	 * @var string
45
	 */
46
	private $userId;
47
48
	/**
49
	 * @param string $appName
50
	 * @param IRequest $request an instance of the request
51
	 * @param IUserSession $userSession
52
	 * @param IConfig $config
53
	 */
54 19
	public function __construct($appName, IRequest $request, IUserSession $userSession,
55
								IConfig $config) {
56 19
		parent::__construct($appName, $request);
57 19
		$this->config = $config;
58 19
		$this->userSession = $userSession;
59 19
		$this->userId = $userSession->getUser()->getUID();
60 19
	}
61
62
	/**
63
	 * get a configuration item
64
	 *
65
	 * @NoAdminRequired
66
	 *
67
	 * @param string $key
68
	 * @return JSONResponse
69
	 */
70 7
	public function getConfig($key) {
71
		switch ($key) {
72 7
			case 'view':
73 2
				return $this->getView();
74 5
			case 'skipPopover':
75 2
				return $this->getSkipPopover();
76 3
			case 'showWeekNr':
77
				return $this->getShowWeekNr();
78 3
			case 'firstRun':
79 2
				return $this->getFirstRun();
80 1
			case 'startOfWeek':
81 1
				return $this->getStartOfWeek();
82 1
			default:
83
				return new JSONResponse([], Http::STATUS_BAD_REQUEST);
84
		}
85
	}
86
87
	/**
88
	 * set a configuration item
89
	 *
90
	 * @NoAdminRequired
91
	 *
92
	 * @param string $key
93
	 * @param mixed $value
94 12
	 * @return JSONResponse
95
	 */
96 12
	public function setConfig($key, $value) {
97 5
		switch ($key) {
98 7
			case 'view':
99 4
				return $this->setView($value);
100 3
			case 'skipPopover':
101
				return $this->setSkipPopover($value);
102 3
			case 'showWeekNr':
103 2
				return $this->setShowWeekNr($value);
104 1
			case 'firstRun':
105 1
				return $this->setFirstRun();
106 1
			case 'startOfWeek':
107
				return $this->setStartOfWeek($value);
108
			default:
109
				return new JSONResponse([], Http::STATUS_BAD_REQUEST);
110
		}
111
	}
112
113
114
	/**
115
	 * set a new view
116 5
	 *
117 5
	 * @param string $view
118 1
	 * @return JSONResponse
119
	 */
120 View Code Duplication
	private function setView($view) {
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...
121
		if (!$this->isViewAllowed($view)) {
122 4
			return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
123 4
		}
124 4
125 4
		try {
126
			$this->config->setUserValue(
127 4
				$this->userId,
128 4
				$this->appName,
129 1
				'currentView',
130
				$view
131
			);
132 3
		} catch(\Exception $e) {
133
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
134
		}
135
136
		return new JSONResponse();
137
	}
138
139
140
	/**
141 2
	 * get a config value
142
	 *
143 2
	 * @return JSONResponse
144 2
	 */
145 2 View Code Duplication
	private function getView() {
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...
146 2
		try {
147
			$view = $this->config->getUserValue(
148 2
				$this->userId,
149 2
				$this->appName,
150 1
				'currentView',
151
				'month'
152
			);
153 1
		} catch(\Exception $e) {
154 1
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
155 1
		}
156
157
		return new JSONResponse([
158
			'value' => $view,
159
		]);
160
	}
161
162
	/**
163
	 * check if view is allowed
164 5
	 *
165
	 * @param $view
166 5
	 * @return bool
167 5
	 */
168 5
	private function isViewAllowed($view) {
169 5
		$allowedViews = [
170
			'agendaDay',
171 5
			'agendaWeek',
172
			'month',
173
		];
174
175
		return in_array($view, $allowedViews);
176
	}
177
178
	/**
179
	 * set if popover shall be skipped
180 4
	 *
181 4
	 * @param $value
182 1
	 * @return JSONResponse
183
	 */
184 View Code Duplication
	private function setSkipPopover($value) {
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...
185
		if (!$this->isSkipPopoverValueAllowed($value)) {
186 3
			return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
187 3
		}
188 3
189 3
		try {
190
			$this->config->setUserValue(
191 3
				$this->userId,
192 3
				$this->appName,
193 1
				'skipPopover',
194
				$value
195
			);
196 2
		} catch(\Exception $e) {
197
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
198
		}
199
200
		return new JSONResponse();
201
	}
202
203
	/**
204 2
	 * get if popover shall be skipped
205
	 *
206 2
	 * @return JSONResponse
207 2
	 */
208 2 View Code Duplication
	private function getSkipPopover() {
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...
209 2
		try {
210
			$view = $this->config->getUserValue(
211 2
				$this->userId,
212 2
				$this->appName,
213 1
				'skipPopover',
214
				'no'
215
			);
216 1
		} catch(\Exception $e) {
217 1
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
218 1
		}
219
220
		return new JSONResponse([
221
			'value' => $view,
222
		]);
223
	}
224
225
	/**
226
	 * check if value for skipPopover is allowed
227 4
	 *
228
	 * @param $value
229 4
	 * @return bool
230
	 */
231 4
	private function isSkipPopoverValueAllowed($value) {
232
		$allowedValues = [
233 4
			'yes',
234
			'no'
235
		];
236
237
		return in_array($value, $allowedValues);
238
	}
239
240
	/**
241
	 * set config value for showing week numbers
242
	 *
243
	 * @param $value
244
	 * @return JSONResponse
245
	 */
246 View Code Duplication
	private function setShowWeekNr($value) {
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...
247
		if (!$this->isShowWeekNrValueAllowed($value)) {
248
			return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
249
		}
250
251
		try {
252
			$this->config->setUserValue(
253
				$this->userId,
254
				$this->appName,
255
				'showWeekNr',
256
				$value
257
			);
258
		} catch(\Exception $e) {
259
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
260
		}
261
262
		return new JSONResponse();
263
	}
264
265
	/**
266
	 * get config value for showing week numbers
267
	 *
268
	 * @return JSONResponse
269
	 */
270 View Code Duplication
	private function getShowWeekNr() {
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...
271
		try {
272
			$value = $this->config->getUserValue(
273
				$this->userId,
274
				$this->appName,
275
				'showWeekNr',
276
				'no'
277
			);
278
		} catch(\Exception $e) {
279
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
280
		}
281
282
		return new JSONResponse([
283
			'value' => $value,
284
		]);
285
	}
286
287
	/**
288
	 * set config value for start of week
289
	 *
290
	 * @param $value
291
	 * @return JSONResponse
292
	 */
293 View Code Duplication
	private function setStartOfWeek($value) {
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...
294
		try {
295
			$this->config->setUserValue(
296
				$this->userId,
297
				$this->appName,
298
				'startOfWeek',
299
				$value
300
			);
301
		} catch(\Exception $e) {
302
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
303 2
		}
304
305 2
		return new JSONResponse();
306 2
	}
307 2
308 2
	/**
309
	 * get config value for start of week
310 2
	 *
311 2
	 * @return JSONResponse
312 1
	 */
313 View Code Duplication
	private function getStartOfWeek() {
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...
314
		try {
315 1
			$value = $this->config->getUserValue(
316
				$this->userId,
317
				$this->appName,
318
				'startOfWeek',
319
				''
320
			);
321
		} catch(\Exception $e) {
322
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
323 2
		}
324
325 2
		return new JSONResponse([
326 2
			'value' => $value,
327 2
		]);
328 2
	}
329
330 2
	/**
331 2
	 * check if value for showWeekNr is allowed
332 1
	 *
333
	 * @param $value
334
	 * @return bool
335 1
	 */
336 1
	private function isShowWeekNrValueAllowed($value) {
337 1
		$allowedValues = [
338
			'yes',
339
			'no'
340
		];
341
342
		return in_array($value, $allowedValues);
343
	}
344
345
	/**
346
	 * remember that first run routines executed
347
	 *
348
	 * @return JSONResponse
349
	 */
350 View Code Duplication
	private function setFirstRun() {
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...
351
		try {
352
			$this->config->setUserValue(
353
				$this->userId,
354
				$this->appName,
355
				'firstRun',
356
				'no'
357
			);
358
		} catch(\Exception $e) {
359
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
360
		}
361
362
		return new JSONResponse();
363
	}
364
365
	/**
366
	 * get stored value for first run
367
	 *
368
	 * @return JSONResponse
369
	 */
370 View Code Duplication
	private function getFirstRun() {
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...
371
		try {
372
			$value = $this->config->getUserValue(
373
				$this->userId,
374
				$this->appName,
375
				'firstRun',
376
				'yes'
377
			);
378
		} catch(\Exception $e) {
379
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
380
		}
381
382
		return new JSONResponse([
383
			'value' => $value,
384
		]);
385
	}
386
}
387