Completed
Pull Request — master (#1038)
by René
06:20
created

PreferencesController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 3
c 4
b 0
f 0
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 10
cc 1
nc 1
nop 4
crap 2
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
29
use OCP\IRequest;
30
use OCP\AppFramework\Controller;
31
use OCP\AppFramework\Http;
32
use OCP\AppFramework\Http\DataResponse;
33
34
35
use OCA\Polls\Db\Preferences;
36
use OCA\Polls\Db\PreferencesMapper;
37
38
class PreferencesController extends Controller {
39
	private $userId;
40
	private $preferencesMapper;
41
42
	private $groupManager;
0 ignored issues
show
introduced by
The private property $groupManager is not used, and could be removed.
Loading history...
43
	private $pollMapper;
0 ignored issues
show
introduced by
The private property $pollMapper is not used, and could be removed.
Loading history...
44
	private $anonymizer;
0 ignored issues
show
introduced by
The private property $anonymizer is not used, and could be removed.
Loading history...
45
	private $acl;
0 ignored issues
show
introduced by
The private property $acl is not used, and could be removed.
Loading history...
46
47
	/**
48
	 * PreferencesController constructor.
49
	 * @param string $appName
50
	 * @param $UserId
51
	 * @param PreferencesMapper $preferencesMapper
52
	 */
53
54
	public function __construct(
55
		string $appName,
56
		$userId,
57
		IRequest $request,
58
		PreferencesMapper $preferencesMapper
59
	) {
60
		parent::__construct($appName, $request);
61
		$this->userId = $userId;
62
		$this->preferencesMapper = $preferencesMapper;
63
	}
64
65
66
	/**
67
	 * get
68
	 * Read all preferences
69
	 * @NoAdminRequired
70
	 * @NoCSRFRequired
71
	 * @param integer $pollId
72
	 * @return DataResponse
73
	 */
74
	public function get() {
75
		try {
76
			return new DataResponse($this->preferencesMapper->find($this->userId), Http::STATUS_OK);
77
		} catch (DoesNotExistException $e) {
78
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
79
		}
80
	}
81
82
	/**
83
	 * write
84
	 * Write a new comment to the db and returns the new comment as array
85
	 * @NoAdminRequired
86
	 * @NoCSRFRequired
87
	 * @param int $pollId
88
	 * @param string $userId
89
	 * @param string $message
90
	 * @return DataResponse
91
	 */
92
	public function write($settings) {
93
		if (!\OC::$server->getUserSession()->isLoggedIn()) {
94
			return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
95
		}
96
97
		try {
98
			$preferences = $this->preferencesMapper->find($this->userId);
99
			$preferences->setPreferences(json_encode($settings));
100
			$preferences->setTimestamp(time());
101
			$preferences = $this->preferencesMapper->update($preferences);
102
		} catch (\Exception $e) {
103
			$preferences = new Preferences();
104
			$preferences->setUserId($this->userId);
105
			$preferences->setPreferences(json_encode($settings));
106
			$preferences->setTimestamp(time());
107
			$preferences = $this->preferencesMapper->insert($preferences);
108
		}
109
110
		return new DataResponse($preferences, Http::STATUS_OK);
111
	}
112
113
	// /**
114
	//  * delete
115
	//  * Delete Preferences
116
	//  * @NoAdminRequired
117
	//  * @param int $pollId
118
	//  * @param string $message
119
	//  * @return DataResponse
120
	//  */
121
	// public function delete($userId) {
122
	// 	if (!\OC::$server->getUserSession()->isLoggedIn()) {
123
	// 		return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
124
	// 	}
125
	//
126
	// 	try {
127
	// 		$this->preferencesMapper->delete($userId);
128
	// 	} catch (\Exception $e) {
129
	// 		return new DataResponse($e, Http::STATUS_CONFLICT);
130
	// 	}
131
	//
132
	// 	return new DataResponse(['deleted' => $userId], Http::STATUS_OK);
133
	//
134
	// }
135
}
136