Passed
Pull Request — master (#1272)
by René
03:46
created

PollService::deletePermanently()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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