Passed
Pull Request — master (#1281)
by René
04:06
created

PollService::listForAdmin()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 11
ccs 0
cts 3
cp 0
rs 10
cc 3
nc 3
nop 0
crap 12
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\Service;
25
26
use OCP\AppFramework\Db\DoesNotExistException;
27
use OCA\Polls\Exceptions\EmptyTitleException;
28
use OCA\Polls\Exceptions\InvalidAccessException;
29
use OCA\Polls\Exceptions\InvalidShowResultsException;
30
use OCA\Polls\Exceptions\InvalidPollTypeException;
31
use OCA\Polls\Exceptions\NotAuthorizedException;
32
33
34
use OCA\Polls\Db\Log;
35
use OCA\Polls\Db\PollMapper;
36
use OCA\Polls\Db\Poll;
37
use OCA\Polls\Db\VoteMapper;
38
use OCA\Polls\Db\Vote;
39
use OCA\Polls\Model\Acl;
40
41
class PollService {
42
43
	/** @var string */
44
	private $userId;
0 ignored issues
show
introduced by
The private property $userId is not used, and could be removed.
Loading history...
45
46
	/** @var PollMapper */
47
	private $pollMapper;
48
49
	/** @var Poll */
50
	private $poll;
51
52
	/** @var VoteMapper */
53
	private $voteMapper;
54
55
	/** @var Vote */
56
	private $vote;
57
58
	/** @var LogService */
59
	private $logService;
60
61
	/** @var MailService */
62
	private $mailService;
63
64
	/** @var Acl */
65
	private $acl;
66
67
	public function __construct(
68
		string $UserId,
69
		PollMapper $pollMapper,
70
		Poll $poll,
71
		VoteMapper $voteMapper,
72
		Vote $vote,
73
		LogService $logService,
74
		MailService $mailService,
75
		Acl $acl
76
	) {
77
		$this->pollMapper = $pollMapper;
78
		$this->poll = $poll;
79
		$this->voteMapper = $voteMapper;
80
		$this->vote = $vote;
81
		$this->logService = $logService;
82
		$this->mailService = $mailService;
83
		$this->acl = $acl;
84
	}
85
86
	/**
87
	 * Get list of polls
88
	 */
89
	public function list(): array {
90
		$pollList = [];
91
		try {
92
			$polls = $this->pollMapper->findForMe(\OC::$server->getUserSession()->getUser()->getUID());
93
94
			foreach ($polls as $poll) {
95
				try {
96
					$this->acl->setPoll($poll)->requestView();
97
					// TODO: Not the elegant way. Improvement neccessary
98
					$pollList[] = (object) array_merge(
99
						(array) json_decode(json_encode($poll)),
100
						(array) json_decode(json_encode($this->acl))
101
						);
102
				} catch (NotAuthorizedException $e) {
103
					continue;
104
				}
105
			}
106
		} catch (DoesNotExistException $e) {
107
			// silent catch
108
		}
109
		return $pollList;
110
	}
111
112
	/**
113
	 *   * Get list of polls
114
	 *
115
	 * @return Poll[]
116
	 */
117
	public function listForAdmin(): array {
118
		$pollList = [];
119
		$userId = \OC::$server->getUserSession()->getUser()->getUID();
120
		if (\OC::$server->getGroupManager()->isAdmin($userId)) {
121
			try {
122
				$pollList = $this->pollMapper->findForAdmin($userId);
123
			} catch (DoesNotExistException $e) {
124
				// silent catch
125
			}
126
		}
127
		return $pollList;
128
	}
129
130
	/**
131
	 * 	 * Update poll configuration
132
	 *
133
	 * @return Poll
134
	 */
135
	public function takeover(int $pollId): Poll {
136
		$this->poll = $this->pollMapper->find($pollId);
137
138
		$this->poll->setOwner(\OC::$server->getUserSession()->getUser()->getUID());
139
140
		$this->pollMapper->update($this->poll);
141
		$this->logService->setLog($this->poll->getId(), Log::MSG_ID_UPDATEPOLL);
142
143
		return $this->poll;
144
	}
145
146
147
148
	/**
149
	 * 	 * get poll configuration
150
	 *
151
	 * @return Poll
152
	 */
153
	public function get(int $pollId): Poll {
154
		$this->poll = $this->pollMapper->find($pollId);
155
		$this->acl->setPoll($this->poll)->requestView();
156
		return $this->poll;
157
	}
158
159
	/**
160
	 * Add poll
161
	 */
162
	public function add(string $type, string $title) {
163
		if (!\OC::$server->getUserSession()->isLoggedIn()) {
164
			throw new NotAuthorizedException;
165
		}
166
167
		// Validate valuess
168
		if (!in_array($type, $this->getValidPollType())) {
169
			throw new InvalidPollTypeException('Invalid poll type');
170
		}
171
172
		if (!$title) {
173
			throw new EmptyTitleException('Title must not be empty');
174
		}
175
176
		$this->poll = new Poll();
177
		$this->poll->setType($type);
178
		$this->poll->setCreated(time());
179
		$this->poll->setOwner(\OC::$server->getUserSession()->getUser()->getUID());
180
		$this->poll->setTitle($title);
181
		$this->poll->setDescription('');
182
		$this->poll->setAccess(Poll::ACCESS_HIDDEN);
183
		$this->poll->setExpire(0);
184
		$this->poll->setAnonymous(0);
185
		$this->poll->setFullAnonymous(0);
186
		$this->poll->setAllowMaybe(0);
187
		$this->poll->setVoteLimit(0);
188
		$this->poll->setSettings('');
189
		$this->poll->setOptions('');
190
		$this->poll->setShowResults(Poll::SHOW_RESULTS_ALWAYS);
191
		$this->poll->setDeleted(0);
192
		$this->poll->setAdminAccess(0);
193
		$this->poll->setImportant(0);
194
		$this->poll = $this->pollMapper->insert($this->poll);
195
196
		$this->logService->setLog($this->poll->getId(), Log::MSG_ID_ADDPOLL);
197
198
		return $this->poll;
199
	}
200
201
	/**
202
	 * 	 * Update poll configuration
203
	 *
204
	 * @return Poll
205
	 */
206
	public function update(int $pollId, array $poll): Poll {
207
		$this->poll = $this->pollMapper->find($pollId);
208
		$this->acl->setPoll($this->poll)->requestEdit();
209
210
		// Validate valuess
211
		if (isset($poll['showResults']) && !in_array($poll['showResults'], $this->getValidShowResults())) {
212
			throw new InvalidShowResultsException('Invalid value for prop showResults');
213
		}
214
		if (isset($poll['access']) && !in_array($poll['access'], $this->getValidAccess())) {
215
			throw new InvalidAccessException('Invalid value for prop access ' . $poll['access']);
216
		}
217
218
		if (isset($poll['title']) && !$poll['title']) {
219
			throw new EmptyTitleException('Title must not be empty');
220
		}
221
		$this->poll->deserializeArray($poll);
222
223
		$this->pollMapper->update($this->poll);
224
		$this->logService->setLog($this->poll->getId(), Log::MSG_ID_UPDATEPOLL);
225
226
		return $this->poll;
227
	}
228
229
230
	/**
231
	 * 	 * Switch deleted status (move to deleted polls)
232
	 *
233
	 * @return Poll
234
	 */
235
	public function switchDeleted(int $pollId): Poll {
236
		$this->poll = $this->pollMapper->find($pollId);
237
		$this->acl->setPoll($this->poll)->requestDelete();
238
239
		if ($this->poll->getDeleted()) {
240
			$this->poll->setDeleted(0);
241
		} else {
242
			$this->poll->setDeleted(time());
243
		}
244
245
		$this->poll = $this->pollMapper->update($this->poll);
246
		$this->logService->setLog($this->poll->getId(), Log::MSG_ID_DELETEPOLL);
247
248
		return $this->poll;
249
	}
250
251
	/**
252
	 * 	 * Delete poll
253
	 *
254
	 * @return Poll
255
	 */
256
	public function delete(int $pollId): Poll {
257
		$this->poll = $this->pollMapper->find($pollId);
258
		$this->acl->setPoll($this->poll)->requestDelete();
259
260
		return $this->pollMapper->delete($this->poll);
261
	}
262
263
	/**
264
	 * 	 * Clone poll
265
	 *
266
	 * @return Poll
267
	 */
268
	public function clone(int $pollId): Poll {
269
		$origin = $this->pollMapper->find($pollId);
270
		$this->acl->setPoll($origin)->requestView();
271
272
		$this->poll = new Poll();
273
		$this->poll->setCreated(time());
274
		$this->poll->setOwner(\OC::$server->getUserSession()->getUser()->getUID());
275
		$this->poll->setTitle('Clone of ' . $origin->getTitle());
276
		$this->poll->setDeleted(0);
277
		$this->poll->setAccess(Poll::ACCESS_HIDDEN);
278
279
		$this->poll->setType($origin->getType());
280
		$this->poll->setDescription($origin->getDescription());
281
		$this->poll->setExpire($origin->getExpire());
282
		$this->poll->setAnonymous($origin->getAnonymous());
283
		$this->poll->setFullAnonymous($origin->getFullAnonymous());
284
		$this->poll->setAllowMaybe($origin->getAllowMaybe());
285
		$this->poll->setVoteLimit($origin->getVoteLimit());
286
		$this->poll->setSettings($origin->getSettings());
287
		$this->poll->setOptions($origin->getOptions());
288
		$this->poll->setShowResults($origin->getShowResults());
289
		$this->poll->setAdminAccess($origin->getAdminAccess());
290
		$this->poll->setImportant($origin->getImportant());
291
292
		return $this->pollMapper->insert($this->poll);
293
	}
294
295
	/**
296
	 * 	 * Collect email addresses from particitipants
297
	 *
298
	 * @return string[]
299
	 *
300
	 * @psalm-return array<int, string>
301
	 */
302
	public function getParticipantsEmailAddresses(int $pollId): array {
303
		$this->poll = $this->pollMapper->find($pollId);
304
		$this->acl->setPoll($this->poll)->requestEdit();
305
306
		$votes = $this->voteMapper->findParticipantsByPoll($pollId);
307
		$list = [];
308
		foreach ($votes as $vote) {
309
			$list[] = $vote->getDisplayName() . ' <' . $this->mailService->resolveEmailAddress($pollId, $vote->getUserId()) . '>';
310
		}
311
		return array_unique($list);
312
	}
313
314
	/**
315
	 * 	 * Get valid values for configuration options
316
	 *
317
	 * @return array
318
	 *
319
	 * @psalm-return array{pollType: mixed, access: mixed, showResults: mixed}
320
	 */
321
	public function getValidEnum(): array {
322
		return [
323
			'pollType' => $this->getValidPollType(),
324
			'access' => $this->getValidAccess(),
325
			'showResults' => $this->getValidShowResults()
326
		];
327
	}
328
329
	/**
330
	 * 	 * Get valid values for pollType
331
	 *
332
	 * @return string[]
333
	 *
334
	 * @psalm-return array{0: string, 1: string}
335
	 */
336
	private function getValidPollType(): array {
337
		return [Poll::TYPE_DATE, Poll::TYPE_TEXT];
338
	}
339
340
	/**
341
	 * 	 * Get valid values for access
342
	 *
343
	 * @return string[]
344
	 *
345
	 * @psalm-return array{0: string, 1: string}
346
	 */
347
	private function getValidAccess(): array {
348
		return [Poll::ACCESS_HIDDEN, Poll::ACCESS_PUBLIC];
349
	}
350
351
	/**
352
	 * 	 * Get valid values for showResult
353
	 *
354
	 * @return string[]
355
	 *
356
	 * @psalm-return array{0: string, 1: string, 2: string}
357
	 */
358
	private function getValidShowResults(): array {
359
		return [Poll::SHOW_RESULTS_ALWAYS, Poll::SHOW_RESULTS_CLOSED, Poll::SHOW_RESULTS_NEVER];
360
	}
361
}
362