Completed
Pull Request — master (#929)
by René
04:36
created

OptionController::update()   B

Complexity

Conditions 6
Paths 42

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 6
eloc 21
nc 42
nop 1
dl 0
loc 38
ccs 0
cts 26
cp 0
crap 42
rs 8.9617
c 2
b 1
f 0
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 Exception;
27
use OCP\AppFramework\Db\DoesNotExistException;
28
29
use OCP\IRequest;
30
use OCP\ILogger;
31
use OCP\AppFramework\Controller;
32
use OCP\AppFramework\Http;
33
use OCP\AppFramework\Http\DataResponse;
34
35
use OCP\IGroupManager;
36
use OCP\Security\ISecureRandom;
37
38
use OCA\Polls\Db\Poll;
39
use OCA\Polls\Db\PollMapper;
40
use OCA\Polls\Db\Option;
41
use OCA\Polls\Db\OptionMapper;
42
use OCA\Polls\Service\LogService;
43
use OCA\Polls\Model\Acl;
44
45
class OptionController extends Controller {
46
47
	private $userId;
48
	private $optionMapper;
49
50
	private $groupManager;
51
	private $pollMapper;
52
	private $logger;
53
	private $logService;
54
	private $acl;
55
56
	/**
57
	 * OptionController constructor.
58
	 * @param string $appName
59
	 * @param $UserId
60
	 * @param IRequest $request
61
	 * @param ILogger $logger
62
	 * @param OptionMapper $optionMapper
63
	 * @param IGroupManager $groupManager
64
	 * @param PollMapper $pollMapper
65
	 * @param LogService $logService
66
	 * @param Acl $acl
67
	 */
68
69
	public function __construct(
70
		string $appName,
71
		$UserId,
72
		IRequest $request,
73
		OptionMapper $optionMapper,
74
		IGroupManager $groupManager,
75
		PollMapper $pollMapper,
76
		ILogger $logger,
77
		LogService $logService,
78
		Acl $acl
79
	) {
80
		parent::__construct($appName, $request);
81
		$this->userId = $UserId;
82
		$this->optionMapper = $optionMapper;
83
		$this->groupManager = $groupManager;
84
		$this->pollMapper = $pollMapper;
85
		$this->logger = $logger;
86
		$this->logService = $logService;
87
		$this->acl = $acl;
88
	}
89
90
91
	/**
92
	 * Get all options of given poll
93
	 * @NoAdminRequired
94
	 * @param integer $pollId
95
	 * @return array Array of Option objects
96
	 */
97
	public function get($pollId) {
98
99
		try {
100
101
			if (!$this->acl->getFoundByToken()) {
102
				$this->acl->setPollId($pollId);
103
			}
104
105
			return new DataResponse($this->optionMapper->findByPoll($pollId), Http::STATUS_OK);
106
107
		} catch (DoesNotExistException $e) {
108
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
109
		}
110
	}
111
112
113
	/**
114
	 * getByToken
115
	 * Read all options of a poll based on a share token and return list as array
116
	 * @NoAdminRequired
117
	 * @PublicPage
118
	 * @NoCSRFRequired
119
	 * @param string $token
120
	 * @return DataResponse
121
	 */
122
	public function getByToken($token) {
123
124
		try {
125
			$this->acl->setToken($token);
126
			return $this->get($this->acl->getPollId());
127
128
		} catch (DoesNotExistException $e) {
129
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
130
		}
131
	}
132
133
	/**
134
	 * Add a new Option to poll
135
	 * @NoAdminRequired
136
	 * @param Option $option
137
	 * @return DataResponse
138
	 */
139
	public function add($option) {
140
141
		try {
142
143
			if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) {
144
				return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
145
			}
146
147
			$NewOption = new Option();
148
149
			$NewOption->setPollId($option['pollId']);
150
			$NewOption->setPollOptionText(trim(htmlspecialchars($option['pollOptionText'])));
151
			$NewOption->setTimestamp($option['timestamp']);
152
			$NewOption->setOrder($option['timestamp'], $option['order']);
0 ignored issues
show
Unused Code introduced by
The call to OCA\Polls\Db\Option::setOrder() has too many arguments starting with $option['order']. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

152
			$NewOption->/** @scrutinizer ignore-call */ 
153
               setOrder($option['timestamp'], $option['order']);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
153
154
			$this->optionMapper->insert($NewOption);
155
			$this->logService->setLog($option['pollId'], 'addOption');
156
			return new DataResponse($NewOption, Http::STATUS_OK);
157
158
		} catch (Exception $e) {
159
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
160
		}
161
162
	}
163
164
	/**
165
	 * Update poll option
166
	 * @NoAdminRequired
167
	 * @param Option $option
168
	 * @return DataResponse
169
	 */
170
	public function update($option) {
171
172
		try {
173
			$this->logger->alert(json_encode($option));
174
			$updateOption = $this->optionMapper->find($option['id']);
175
176
			if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) {
177
				return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
178
			}
179
180
			$updateOption->setPollOptionText(trim(htmlspecialchars($option['pollOptionText'])));
181
			$updateOption->setTimestamp($option['timestamp']);
182
183
			if ($option['timestamp']) {
184
				$updateOption->setOrder($option['timestamp']);
185
			} else {
186
				$updateOption->setOrder($option['order']);
187
			}
188
189
190
191
			if ($option['confirmed']) {
192
				// do not update confirmation date, if option is already confirmed
193
				if (!$updateOption->getConfirmed()){
194
					$updateOption->setConfirmed(time());
195
				}
196
197
			} else {
198
				$updateOption->setConfirmed(0);
199
			}
200
201
			$this->optionMapper->update($updateOption);
202
			$this->logService->setLog($option['pollId'], 'updateOption');
203
204
			return new DataResponse($updateOption, Http::STATUS_OK);
205
206
		} catch (Exception $e) {
207
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
208
		}
209
	}
210
211
	/**
212
	 * Remove a single option
213
	 * @NoAdminRequired
214
	 * @param Option $option
215
	 * @return DataResponse
216
	 */
217
	public function remove($option) {
218
		try {
219
220
			if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) {
221
				return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
222
			}
223
224
			$this->optionMapper->remove($option['id']);
225
			$this->logService->setLog($option['pollId'], 'deleteOption');
226
227
			return new DataResponse(array(
228
				'action' => 'deleted',
229
				'optionId' => $option['id']
230
			), Http::STATUS_OK);
231
232
		} catch (Exception $e) {
233
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
234
		}
235
236
	}
237
238
}
239