Completed
Pull Request — master (#743)
by Georg
20:11
created

SettingsController::setFirstRun()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 0
crap 2
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
			case 'timezone':
81 1
				return $this->getTimezone();
82
			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
			case 'firstRun':
105 1
				return $this->setFirstRun();
106
			case 'timezone':
107
				return $this->setTimezone($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 4
			$this->config->setUserValue(
127
				$this->userId,
128 1
				$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 2
			$view = $this->config->getUserValue(
148
				$this->userId,
149 1
				$this->appName,
150 1
				'currentView',
151
				'month'
152
			);
153 1
		} catch(\Exception $e) {
154 1
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
155
		}
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
	 */
168
	private function isViewAllowed($view) {
169
		$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 3
			$this->config->setUserValue(
191
				$this->userId,
192 1
				$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 2
			$view = $this->config->getUserValue(
211
				$this->userId,
212 1
				$this->appName,
213 1
				'skipPopover',
214
				'no'
215
			);
216 1
		} catch(\Exception $e) {
217 1
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
218
		}
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
	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
	 * check if value for showWeekNr is allowed
289
	 *
290
	 * @param $value
291
	 * @return bool
292
	 */
293
	private function isShowWeekNrValueAllowed($value) {
294
		$allowedValues = [
295
			'yes',
296
			'no'
297
		];
298
299
		return in_array($value, $allowedValues);
300
	}
301
302
	/**
303 2
	 * remember that first run routines executed
304
	 *
305 2
	 * @return JSONResponse
306 2
	 */
307 2 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...
308 2
		try {
309 2
			$this->config->setUserValue(
310
				$this->userId,
311 1
				$this->appName,
312 1
				'firstRun',
313
				'no'
314
			);
315 1
		} catch(\Exception $e) {
316
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
317
		}
318
319
		return new JSONResponse();
320
	}
321
322
	/**
323 2
	 * get stored value for first run
324
	 *
325 2
	 * @return JSONResponse
326 2
	 */
327 2 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...
328 2
		try {
329 2
			$value = $this->config->getUserValue(
330
				$this->userId,
331 1
				$this->appName,
332 1
				'firstRun',
333
				'yes'
334
			);
335 1
		} catch(\Exception $e) {
336 1
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
337
		}
338
339
		return new JSONResponse([
340
			'value' => $value,
341
		]);
342
	}
343
344
	/**
345
	 * sets display timezone for user
346
	 *
347
	 * @param string $value
348
	 * @return JSONResponse
349
	 */
350 View Code Duplication
	private function setTimezone($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...
351
		try {
352
			$this->config->setUserValue(
353
				$this->userId,
354
				$this->appName,
355
				'timezone',
356
				$value
357
			);
358
		} catch(\Exception $e) {
359
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
360
		}
361
362
		return new JSONResponse();
363
	}
364
365
	/**
366
	 * gets display timezone for user
367
	 *
368
	 * @return JSONResponse
369
	 */
370 View Code Duplication
	private function getTimezone() {
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
				'timezone',
376
				'automatic'
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