Completed
Push — move-config-route ( d690df )
by Thomas
35:41
created

SettingsController::getConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 9.4285
1
<?php
2
/**
3
 * ownCloud - 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
	 * @param string $appName
45
	 * @param IRequest $request an instance of the request
46
	 * @param IUserSession $userSession
47
	 * @param IConfig $config
48
	 */
49
	public function __construct($appName, IRequest $request, IUserSession $userSession,
50
								IConfig $config) {
51
		parent::__construct($appName, $request);
52
		$this->config = $config;
53
		$this->userSession = $userSession;
54
	}
55
56
	/**
57
	 * get a configuration item
58
	 *
59
	 * @param string $key
60
	 * @return JSONResponse
61
	 */
62
	public function getConfig($key) {
63
		switch ($key) {
64
			case 'view':
65
				return $this->getView();
66
			default:
67
				return new JSONResponse([], Http::STATUS_BAD_REQUEST);
68
		}
69
	}
70
71
	/**
72
	 * set a configuration item
73
	 *
74
	 * @param string $key
75
	 * @param mixed $value
76
	 * @return JSONResponse
77
	 */
78
	public function setConfig($key, $value) {
79
		switch ($key) {
80
			case 'view':
81
				return $this->setView($value);
82
			default:
83
				return new JSONResponse([], Http::STATUS_BAD_REQUEST);
84
		}
85
	}
86
87
88
	/**
89
	 * set a new view
90
	 *
91
	 * @param string $view
92
	 * @return JSONResponse
93
	 *
94
	 * @NoAdminRequired
95
	 */
96
	private function setView($view) {
97
		if (!$this->isViewAllowed($view)) {
98
			return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
99
		}
100
101
		$userId = $this->userSession->getUser()->getUID();
102
		$app = $this->appName;
103
104
		try {
105
			$this->config->setUserValue(
106
				$userId,
107
				$app,
108
				'currentView',
109
				$view
110
			);
111
		} catch(\Exception $e) {
112
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
113
		}
114
115
		return new JSONResponse();
116
	}
117
118
119
	/**
120
	 * get a config value
121
	 *
122
	 * @return JSONResponse
123
	 *
124
	 * @NoAdminRequired
125
	 */
126
	private function getView() {
127
		$userId = $this->userSession->getUser()->getUID();
128
		$app = $this->appName;
129
130
		try {
131
			$view = $this->config->getUserValue(
132
				$userId,
133
				$app,
134
				'currentView',
135
				'month'
136
			);
137
		} catch(\Exception $e) {
138
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
139
		}
140
141
		return new JSONResponse([
142
			'value' => $view,
143
		]);
144
	}
145
146
	/**
147
	 * check if view is allowed
148
	 *
149
	 * @param $view
150
	 * @return bool
151
	 */
152
	private function isViewAllowed($view) {
153
		$allowedViews = [
154
			'agendaDay',
155
			'agendaWeek',
156
			'month',
157
		];
158
159
		return in_array($view, $allowedViews);
160
	}
161
}