Completed
Push — master ( 432e3d...cf972b )
by René
07:24 queued 03:01
created

PollController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 13
dl 0
loc 27
ccs 0
cts 27
cp 0
crap 2
rs 9.8666
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\IL10N;
32
use OCP\AppFramework\Controller;
33
use OCP\AppFramework\Http;
34
use OCP\AppFramework\Http\DataResponse;
35
36
use OCP\IGroupManager;
37
use OCP\IUser;
38
use OCP\IUserManager;
39
use OCP\Security\ISecureRandom;
40
41
use OCA\Polls\Db\Poll;
42
use OCA\Polls\Db\PollMapper;
43
use OCA\Polls\Db\Option;
44
use OCA\Polls\Db\OptionMapper;
45
use OCA\Polls\Service\LogService;
46
use OCA\Polls\Service\MailService;
47
use OCA\Polls\Model\Acl;
48
49
class PollController extends Controller {
50
51
	private $userId;
52
	private $pollMapper;
53
	private $trans;
54
	private $logger;
55
	private $groupManager;
56
	private $userManager;
57
	private $poll;
58
	private $logService;
59
	private $mailService;
60
	private $acl;
61
62
	/**
63
	 * CommentController constructor.
64
	 * @param string $appName
65
	 * @param $userId
66
	 * @param IRequest $request
67
	 * @param ILogger $logger
68
	 * @param IL10N $trans
69
	 * @param PollMapper $pollMapper
70
	 * @param OptionsMapper $optionMapper
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Controller\OptionsMapper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
71
	 * @param IGroupManager $groupManager
72
	 * @param IUserManager $userManager
73
	 * @param LogService $logService
74
	 * @param MailService $mailService
75
	 * @param Acl $acl
76
	 */
77
78
	public function __construct(
79
		string $appName,
80
		$userId,
81
		IRequest $request,
82
		ILogger $logger,
83
		IL10N $trans,
84
		PollMapper $pollMapper,
85
		OptionMapper $optionMapper,
86
		Poll $poll,
87
		IGroupManager $groupManager,
88
		IUserManager $userManager,
89
		LogService $logService,
90
		MailService $mailService,
91
		Acl $acl
92
	) {
93
		parent::__construct($appName, $request);
94
		$this->userId = $userId;
95
		$this->trans = $trans;
96
		$this->pollMapper = $pollMapper;
97
		$this->optionMapper = $optionMapper;
0 ignored issues
show
Bug Best Practice introduced by
The property optionMapper does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
98
		$this->logger = $logger;
99
		$this->groupManager = $groupManager;
100
		$this->userManager = $userManager;
101
		$this->poll = $poll;
102
		$this->logService = $logService;
103
		$this->mailService = $mailService;
104
		$this->acl = $acl;
105
	}
106
107
	/**
108
	 * list
109
	 * @NoAdminRequired
110
	 * @NoCSRFRequired
111
	 * @return DataResponse
112
	 */
113
114
	public function list() {
115
		if (\OC::$server->getUserSession()->isLoggedIn()) {
116
			try {
117
				$polls = array_values(array_filter($this->pollMapper->findAll(), function($item) {
118
					return $this->acl->setPollId($item->getId())->getAllowView();
119
				}));
120
				return new DataResponse($polls, Http::STATUS_OK);
121
			} catch (DoesNotExistException $e) {
122
				return new DataResponse($e, Http::STATUS_NOT_FOUND);
123
			}
124
		}
125
	}
126
127
	/**
128
	 * get
129
	 * @NoAdminRequired
130
	 * @NoCSRFRequired
131
	 * @param integer $pollId
132
	 * @return array
133
	 */
134
 	public function get($pollId) {
135
136
 		try {
137
			if (!$this->acl->getFoundByToken()) {
138
				$this->acl->setPollId($pollId);
139
			}
140
			$this->poll = $this->pollMapper->find($pollId);
141
142
			return new DataResponse([
143
				'poll' => $this->poll,
144
				'acl' => $this->acl
145
			], Http::STATUS_OK);
146
147
		} catch (DoesNotExistException $e) {
148
			$this->logger->info('Poll ' . $pollId . ' not found!', ['app' => 'polls']);
149
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
150
 		}
151
 	}
152
153
	/**
154
	 * getByToken
155
	 * Read all options of a poll based on a share token and return list as array
156
	 * @NoAdminRequired
157
	 * @PublicPage
158
	 * @NoCSRFRequired
159
	 * @param string $token
160
	 * @return DataResponse
161
	 */
162
	public function getByToken($token) {
163
164
		try {
165
			return $this->get($this->acl->setToken($token)->getPollId());
166
		} catch (DoesNotExistException $e) {
167
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
168
		}
169
170
	}
171
172
	/**
173
	 * delete
174
	 * @NoAdminRequired
175
	 * @param Array $poll
176
	 * @return DataResponse
177
	 */
178
179
	public function delete($pollId) {
180
181
		try {
182
			// Find existing poll
183
			$this->poll = $this->pollMapper->find($pollId);
184
			$this->acl->setPollId($this->poll->getId());
185
186
			if (!$this->acl->getAllowEdit()) {
187
				$this->logger->alert('Unauthorized delete attempt from user ' . $this->userId);
188
				return new DataResponse(['message' => 'Unauthorized write attempt.'], Http::STATUS_UNAUTHORIZED);
189
			}
190
191
			if ($this->poll->getDeleted()) {
192
				$this->poll->setDeleted(0);
193
			} else {
194
				$this->poll->setDeleted(time());
195
			}
196
197
			$this->pollMapper->update($this->poll);
198
			$this->logService->setLog($this->poll->getId(), 'deletePoll');
199
			return new DataResponse([
200
				'deleted' => $pollId
201
			], Http::STATUS_OK);
202
203
		} catch (Exception $e) {
204
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
205
		}
206
	}
207
208
	/**
209
	 * write
210
	 * @NoAdminRequired
211
	 * @param Array $poll
212
	 * @return DataResponse
213
	 */
214
215
	public function write($poll) {
216
217
		try {
218
			// Find existing poll
219
			$this->poll = $this->pollMapper->find($poll['id']);
220
			$this->acl->setPollId($this->poll->getId());
221
			if (!$this->acl->getAllowEdit()) {
222
				$this->logger->alert('Unauthorized write attempt from user ' . $this->userId);
223
				return new DataResponse(['message' => 'Unauthorized write attempt.'], Http::STATUS_UNAUTHORIZED);
224
			}
225
226
		} catch (Exception $e) {
227
			$this->poll = new Poll();
228
229
			$this->poll->setType($poll['type']);
230
			$this->poll->setOwner($this->userId);
231
			$this->poll->setCreated(time());
232
		} finally {
233
			$this->poll->setTitle($poll['title']);
234
			$this->poll->setDescription($poll['description']);
235
			$this->poll->setAccess($poll['access']);
236
			$this->poll->setExpire($poll['expire']);
237
			$this->poll->setAnonymous(intval($poll['anonymous']));
238
			$this->poll->setFullAnonymous(intval($poll['fullAnonymous']) * $this->poll->getAnonymous());
239
			$this->poll->setAllowMaybe(intval($poll['allowMaybe']));
240
			$this->poll->setVoteLimit(intval($poll['voteLimit']));
241
			$this->poll->setSettings('');
242
			$this->poll->setOptions('');
243
			$this->poll->setShowResults($poll['showResults']);
244
			$this->poll->setDeleted($poll['deleted']);
245
			$this->poll->setAdminAccess($poll['adminAccess']);
246
247
			if ($this->poll->getId() > 0) {
248
				$this->pollMapper->update($this->poll);
249
				$this->logService->setLog($this->poll->getId(), 'updatePoll');
250
			} else {
251
				$this->pollMapper->insert($this->poll);
252
				$this->logService->setLog($this->poll->getId(), 'addPoll');
253
			}
254
			$this->acl->setPollId($this->poll->getId());
255
			return new DataResponse([
256
				'poll' => $this->poll,
257
				'acl' => $this->acl
258
			], Http::STATUS_OK);
259
		}
260
	}
261
262
	/**
263
	 * clone
264
	 * @NoAdminRequired
265
	 * @param integer $pollId
266
	 * @return DataResponse
267
	 */
268
	public function clone($pollId) {
269
		$this->poll = $this->pollMapper->find($pollId);
270
271
		$clonePoll = new Poll();
272
		$clonePoll->setOwner($this->userId);
273
		$clonePoll->setCreated(time());
274
		$clonePoll->setTitle('Clone of ' . $this->poll->getTitle());
275
		$clonePoll->setDeleted(0);
276
277
		$clonePoll->setType($this->poll->getType());
278
		$clonePoll->setDescription($this->poll->getDescription());
279
		$clonePoll->setAccess($this->poll->getAccess());
280
		$clonePoll->setExpire($this->poll->getExpire());
0 ignored issues
show
Bug introduced by
$this->poll->getExpire() of type string is incompatible with the type integer expected by parameter $value of OCA\Polls\Db\Poll::setExpire(). ( Ignorable by Annotation )

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

280
		$clonePoll->setExpire(/** @scrutinizer ignore-type */ $this->poll->getExpire());
Loading history...
281
		$clonePoll->setAnonymous(intval($this->poll->getAnonymous()));
282
		$clonePoll->setFullAnonymous(intval($this->poll->getFullAnonymous())  * $clonePoll->getAnonymous());
283
		$clonePoll->setAllowMaybe(intval($this->poll->getAllowMaybe()));
284
		$clonePoll->setVoteLimit(intval($this->poll->getVoteLimit()));
285
		$clonePoll->setSettings('');
286
		$clonePoll->setOptions('');
287
		$clonePoll->setShowResults($this->poll->getShowResults());
288
		$clonePoll->setAdminAccess($this->poll->getAdminAccess());
289
290
		$this->pollMapper->insert($clonePoll);
291
		$this->logService->setLog($clonePoll->getId(), 'addPoll');
292
293
		foreach ($this->optionMapper->findByPoll($pollId) as $option) {
294
			$newOption = new Option();
295
			$newOption->setPollId($clonePoll->getId());
296
			$newOption->setPollOptionText($option->getPollOptionText());
297
			$newOption->setTimestamp($option->getTimestamp());
298
299
			$this->optionMapper->insert($newOption);
300
		}
301
		return new DataResponse([
302
			'pollId' => $clonePoll->getId()
303
		], Http::STATUS_OK);
304
305
	}
306
307
}
308