Passed
Pull Request — master (#1038)
by René
04:16
created

PreferencesController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 6
eloc 24
dl 0
loc 63
ccs 0
cts 34
cp 0
rs 10
c 4
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 0 19 3
A get() 0 5 2
A __construct() 0 9 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author René Gieling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Polls\Controller;
25
26
use OCP\AppFramework\Db\DoesNotExistException;
27
28
use OCP\IRequest;
29
use OCP\AppFramework\Controller;
30
use OCP\AppFramework\Http;
31
use OCP\AppFramework\Http\DataResponse;
32
use OCA\Polls\Db\Preferences;
33
use OCA\Polls\Db\PreferencesMapper;
34
35
class PreferencesController extends Controller {
36
	private $userId;
37
	private $preferencesMapper;
38
39
	/**
40
	 * PreferencesController constructor.
41
	 * @param string $appName
42
	 * @param $UserId
43
	 * @param PreferencesMapper $preferencesMapper
44
	 */
45
46
	public function __construct(
47
		string $appName,
48
		$userId,
49
		IRequest $request,
50
		PreferencesMapper $preferencesMapper
51
	) {
52
		parent::__construct($appName, $request);
53
		$this->userId = $userId;
54
		$this->preferencesMapper = $preferencesMapper;
55
	}
56
57
	/**
58
	 * get
59
	 * Read all preferences
60
	 * @NoAdminRequired
61
	 * @NoCSRFRequired
62
	 * @return DataResponse
63
	 */
64
	public function get() {
65
		try {
66
			return new DataResponse($this->preferencesMapper->find($this->userId), Http::STATUS_OK);
67
		} catch (DoesNotExistException $e) {
68
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
69
		}
70
	}
71
72
	/**
73
	 * write
74
	 * Write wreferences
75
	 * @NoAdminRequired
76
	 * @param int $settings
77
	 * @return DataResponse
78
	 */
79
	public function write($settings) {
80
		if (!\OC::$server->getUserSession()->isLoggedIn()) {
81
			return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
82
		}
83
84
		try {
85
			$preferences = $this->preferencesMapper->find($this->userId);
86
			$preferences->setPreferences(json_encode($settings));
87
			$preferences->setTimestamp(time());
88
			$preferences = $this->preferencesMapper->update($preferences);
89
		} catch (\Exception $e) {
90
			$preferences = new Preferences();
91
			$preferences->setUserId($this->userId);
92
			$preferences->setPreferences(json_encode($settings));
93
			$preferences->setTimestamp(time());
94
			$preferences = $this->preferencesMapper->insert($preferences);
95
		}
96
97
		return new DataResponse($preferences, Http::STATUS_OK);
98
	}
99
}
100