Completed
Push — master ( 798959...da39f8 )
by René
16s queued 10s
created

PollService::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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