Completed
Push — show_random_color_button ( a7f49b...47bc9a )
by Georg
19:12
created

SettingsController::isSkipPopoverValueAllowed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 9.4285
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
	public function __construct($appName, IRequest $request, IUserSession $userSession,
55
								IConfig $config) {
56
		parent::__construct($appName, $request);
57
		$this->config = $config;
58
		$this->userSession = $userSession;
59
		$this->userId = $userSession->getUser()->getUID();
60
	}
61
62
	/**
63
	 * get a configuration item
64
	 *
65
	 * @NoAdminRequired
66
	 *
67
	 * @param string $key
68
	 * @return JSONResponse
69
	 */
70
	public function getConfig($key) {
71
		switch ($key) {
72
			case 'view':
73
				return $this->getView();
74
			case 'skipPopover':
75
				return $this->getSkipPopover();
76
			default:
77
				return new JSONResponse([], Http::STATUS_BAD_REQUEST);
78
		}
79
	}
80
81
	/**
82
	 * set a configuration item
83
	 *
84
	 * @NoAdminRequired
85
	 *
86
	 * @param string $key
87
	 * @param mixed $value
88
	 * @return JSONResponse
89
	 */
90
	public function setConfig($key, $value) {
91
		switch ($key) {
92
			case 'view':
93
				return $this->setView($value);
94
			case 'skipPopover':
95
				return $this->setSkipPopover($value);
96
			default:
97
				return new JSONResponse([], Http::STATUS_BAD_REQUEST);
98
		}
99
	}
100
101
102
	/**
103
	 * set a new view
104
	 *
105
	 * @param string $view
106
	 * @return JSONResponse
107
	 */
108 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...
109
		if (!$this->isViewAllowed($view)) {
110
			return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
111
		}
112
113
		try {
114
			$this->config->setUserValue(
115
				$this->userId,
116
				$this->appName,
117
				'currentView',
118
				$view
119
			);
120
		} catch(\Exception $e) {
121
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
122
		}
123
124
		return new JSONResponse();
125
	}
126
127
128
	/**
129
	 * get a config value
130
	 *
131
	 * @return JSONResponse
132
	 */
133 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...
134
		try {
135
			$view = $this->config->getUserValue(
136
				$this->userId,
137
				$this->appName,
138
				'currentView',
139
				'month'
140
			);
141
		} catch(\Exception $e) {
142
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
143
		}
144
145
		return new JSONResponse([
146
			'value' => $view,
147
		]);
148
	}
149
150
	/**
151
	 * check if view is allowed
152
	 *
153
	 * @param $view
154
	 * @return bool
155
	 */
156
	private function isViewAllowed($view) {
157
		$allowedViews = [
158
			'agendaDay',
159
			'agendaWeek',
160
			'month',
161
		];
162
163
		return in_array($view, $allowedViews);
164
	}
165
166
	/**
167
	 * set if popover shall be skipped
168
	 *
169
	 * @param $value
170
	 * @return JSONResponse
171
	 */
172 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...
173
		if (!$this->isSkipPopoverValueAllowed($value)) {
174
			return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
175
		}
176
177
		try {
178
			$this->config->setUserValue(
179
				$this->userId,
180
				$this->appName,
181
				'skipPopover',
182
				$value
183
			);
184
		} catch(\Exception $e) {
185
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
186
		}
187
188
		return new JSONResponse();
189
	}
190
191
	/**
192
	 * get if popover shall be skipped
193
	 *
194
	 * @return JSONResponse
195
	 */
196 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...
197
		try {
198
			$view = $this->config->getUserValue(
199
				$this->userId,
200
				$this->appName,
201
				'skipPopover',
202
				'no'
203
			);
204
		} catch(\Exception $e) {
205
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
206
		}
207
208
		return new JSONResponse([
209
			'value' => $view,
210
		]);
211
	}
212
213
	/**
214
	 * check if value for skipPopover is allowed
215
	 *
216
	 * @param $value
217
	 * @return bool
218
	 */
219
	private function isSkipPopoverValueAllowed($value) {
220
		$allowedValues = [
221
			'yes',
222
			'no'
223
		];
224
225
		return in_array($value, $allowedValues);
226
	}
227
}
228