Completed
Push — master ( aa79a7...51f1cd )
by Thomas
05:09
created

SettingsController::getSkipPopover()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 0
dl 16
loc 16
ccs 11
cts 11
cp 1
crap 2
rs 9.4285
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 15
	public function __construct($appName, IRequest $request, IUserSession $userSession,
55
								IConfig $config) {
56 15
		parent::__construct($appName, $request);
57 15
		$this->config = $config;
58 15
		$this->userSession = $userSession;
59 15
		$this->userId = $userSession->getUser()->getUID();
60 15
	}
61
62
	/**
63
	 * get a configuration item
64
	 *
65
	 * @NoAdminRequired
66
	 *
67
	 * @param string $key
68
	 * @return JSONResponse
69
	 */
70 5
	public function getConfig($key) {
71
		switch ($key) {
72 5
			case 'view':
73 2
				return $this->getView();
74 3
			case 'skipPopover':
75 2
				return $this->getSkipPopover();
76 1
			case 'showWeekNr':
77
				return $this->getShowWeekNr();
78 1
			default:
79 1
				return new JSONResponse([], Http::STATUS_BAD_REQUEST);
80 1
		}
81
	}
82
83
	/**
84
	 * set a configuration item
85
	 *
86
	 * @NoAdminRequired
87
	 *
88
	 * @param string $key
89
	 * @param mixed $value
90
	 * @return JSONResponse
91
	 */
92 10
	public function setConfig($key, $value) {
93
		switch ($key) {
94 10
			case 'view':
95 5
				return $this->setView($value);
96 5
			case 'skipPopover':
97 4
				return $this->setSkipPopover($value);
98 1
			case 'showWeekNr':
99
				return $this->setShowWeekNr($value);
100 1
			default:
101 1
				return new JSONResponse([], Http::STATUS_BAD_REQUEST);
102 1
		}
103
	}
104
105
106
	/**
107
	 * set a new view
108
	 *
109
	 * @param string $view
110
	 * @return JSONResponse
111
	 */
112 5 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...
113 5
		if (!$this->isViewAllowed($view)) {
114 1
			return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
115
		}
116
117
		try {
118 4
			$this->config->setUserValue(
119 4
				$this->userId,
120 4
				$this->appName,
121 4
				'currentView',
122
				$view
123 4
			);
124 4
		} catch(\Exception $e) {
125 1
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
126
		}
127
128 3
		return new JSONResponse();
129
	}
130
131
132
	/**
133
	 * get a config value
134
	 *
135
	 * @return JSONResponse
136
	 */
137 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...
138
		try {
139 2
			$view = $this->config->getUserValue(
140 2
				$this->userId,
141 2
				$this->appName,
142 2
				'currentView',
143
				'month'
144 2
			);
145 2
		} catch(\Exception $e) {
146 1
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
147
		}
148
149 1
		return new JSONResponse([
150 1
			'value' => $view,
151 1
		]);
152
	}
153
154
	/**
155
	 * check if view is allowed
156
	 *
157
	 * @param $view
158
	 * @return bool
159
	 */
160 5
	private function isViewAllowed($view) {
161
		$allowedViews = [
162 5
			'agendaDay',
163 5
			'agendaWeek',
164 5
			'month',
165 5
		];
166
167 5
		return in_array($view, $allowedViews);
168
	}
169
170
	/**
171
	 * set if popover shall be skipped
172
	 *
173
	 * @param $value
174
	 * @return JSONResponse
175
	 */
176 4 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...
177 4
		if (!$this->isSkipPopoverValueAllowed($value)) {
178 1
			return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
179
		}
180
181
		try {
182 3
			$this->config->setUserValue(
183 3
				$this->userId,
184 3
				$this->appName,
185 3
				'skipPopover',
186
				$value
187 3
			);
188 3
		} catch(\Exception $e) {
189 1
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
190
		}
191
192 2
		return new JSONResponse();
193
	}
194
195
	/**
196
	 * get if popover shall be skipped
197
	 *
198
	 * @return JSONResponse
199
	 */
200 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...
201
		try {
202 2
			$view = $this->config->getUserValue(
203 2
				$this->userId,
204 2
				$this->appName,
205 2
				'skipPopover',
206
				'no'
207 2
			);
208 2
		} catch(\Exception $e) {
209 1
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
210
		}
211
212 1
		return new JSONResponse([
213 1
			'value' => $view,
214 1
		]);
215
	}
216
217
	/**
218
	 * check if value for skipPopover is allowed
219
	 *
220
	 * @param $value
221
	 * @return bool
222
	 */
223 4
	private function isSkipPopoverValueAllowed($value) {
224
		$allowedValues = [
225 4
			'yes',
226
			'no'
227 4
		];
228
229 4
		return in_array($value, $allowedValues);
230
	}
231
232
	/**
233
	 * set config value for showing week numbers
234
	 *
235
	 * @param $value
236
	 * @return JSONResponse
237
	 */
238
	private function setShowWeekNr($value) {
239
		if (!$this->isShowWeekNrValueAllowed($value)) {
240
			return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
241
		}
242
243
		try {
244
			$this->config->setUserValue(
245
				$this->userId,
246
				$this->appName,
247
				'showWeekNr',
248
				$value
249
			);
250
		} catch(\Exception $e) {
251
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
252
		}
253
254
		return new JSONResponse();
255
	}
256
257
	/**
258
	 * get config value for showing week numbers
259
	 *
260
	 * @return JSONResponse
261
	 */
262 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...
263
		try {
264
			$value = $this->config->getUserValue(
265
				$this->userId,
266
				$this->appName,
267
				'showWeekNr',
268
				'no'
269
			);
270
		} catch(\Exception $e) {
271
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
272
		}
273
274
		return new JSONResponse([
275
			'value' => $value,
276
		]);
277
	}
278
279
	/**
280
	 * check if value for showWeekNr is allowed
281
	 *
282
	 * @param $value
283
	 * @return bool
284
	 */
285
	private function isShowWeekNrValueAllowed($value) {
286
		$allowedValues = [
287
			'yes',
288
			'no'
289
		];
290
291
		return in_array($value, $allowedValues);
292
	}
293
}
294