Completed
Pull Request — master (#1030)
by René
04:05
created

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