Completed
Pull Request — master (#652)
by Thomas
52:08
created

SettingsController::getConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

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
ccs 3
cts 3
cp 1
crap 2
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 7
	public function __construct($appName, IRequest $request, IUserSession $userSession,
50
								IConfig $config) {
51 7
		parent::__construct($appName, $request);
52 7
		$this->config = $config;
53 7
		$this->userSession = $userSession;
54 7
	}
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 5
				return $this->getView();
66 5
			default:
67 1
				return new JSONResponse([], Http::STATUS_BAD_REQUEST);
68
		}
69
	}
70 4
71 4
	/**
72
	 * set a configuration item
73
	 *
74 4
	 * @param string $key
75 4
	 * @param mixed $value
76 4
	 * @return JSONResponse
77 4
	 */
78
	public function setConfig($key, $value) {
79 4
		switch ($key) {
80 4
			case 'view':
81 1
				return $this->setView($value);
82
			default:
83
				return new JSONResponse([], Http::STATUS_BAD_REQUEST);
84 3
		}
85
	}
86
87
88
	/**
89
	 * set a new view
90
	 *
91
	 * @param string $view
92
	 * @return JSONResponse
93
	 *
94
	 * @NoAdminRequired
95 2
	 */
96 2
	private function setView($view) {
97 2
		if (!$this->isViewAllowed($view)) {
98
			return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
99
		}
100 2
101 2
		$userId = $this->userSession->getUser()->getUID();
102 2
		$app = $this->appName;
103 2
104
		try {
105 2
			$this->config->setUserValue(
106 2
				$userId,
107 1
				$app,
108
				'currentView',
109
				$view
110 1
			);
111 1
		} catch(\Exception $e) {
112 1
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
113
		}
114
115
		return new JSONResponse();
116
	}
117
118
119
	/**
120
	 * get a config value
121 5
	 *
122
	 * @return JSONResponse
123 5
	 *
124 5
	 * @NoAdminRequired
125 5
	 */
126 5
	private function getView() {
127
		$userId = $this->userSession->getUser()->getUID();
128 5
		$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
}