Completed
Pull Request — master (#966)
by René
04:37
created

PollService::list()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
c 2
b 0
f 0
dl 0
loc 18
ccs 0
cts 14
cp 0
rs 9.9
cc 4
nc 4
nop 0
crap 20
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
 use OCP\ILogger;
35
36
 use OCA\Polls\Db\PollMapper;
37
 use OCA\Polls\Db\Poll;
38
 use OCA\Polls\Service\LogService;
39
 use OCA\Polls\Model\Acl;
40
41
 class PollService {
42
43
	private $logger;
44
	private $pollMapper;
45
 	private $poll;
46
 	private $logService;
47
 	private $acl;
48
49
 	/**
50
 	 * PollController constructor.
51
 	 * @param ILogger $logger
52
 	 * @param PollMapper $pollMapper
53
 	 * @param Poll $poll
54
 	 * @param LogService $logService
55
 	 * @param Acl $acl
56
 	 */
57
58
 	public function __construct(
59
		ILogger $logger,
60
 		PollMapper $pollMapper,
61
 		Poll $poll,
62
 		LogService $logService,
63
 		Acl $acl
64
 	) {
65
		$this->logger = $logger;
66
 		$this->pollMapper = $pollMapper;
67
 		$this->poll = $poll;
68
 		$this->logService = $logService;
69
 		$this->acl = $acl;
70
 	}
71
72
73
	/**
74
	 * list
75
	 * @NoAdminRequired
76
	 * @return array
77
	 */
78
79
	public function list() {
80
		if (!\OC::$server->getUserSession()->isLoggedIn()) {
81
			throw new NotAuthorizedException;
82
		}
83
84
		$pollList = [];
85
86
		$polls = $this->pollMapper->findAll();
87
		// TODO: Not the elegant way. Improvement neccessary
88
		foreach ($polls as $poll) {
89
			$combinedPoll = (object) array_merge(
90
				(array) json_decode(json_encode($poll)), (array) json_decode(json_encode($this->acl->setPollId($poll->getId()))));
91
			if ($combinedPoll->allowView) {
92
				$pollList[] = $combinedPoll;
93
			}
94
		}
95
96
		return $pollList;
97
	}
98
99
	/**
100
	 * get
101
	 * @NoAdminRequired
102
	 * @param integer $pollId
103
	 * @return array
104
	 */
105
 	public function get($pollId) {
106
107
		if (!$this->acl->setPollId($pollId)->getAllowView()) {
108
			throw new NotAuthorizedException;
109
		}
110
111
		return $this->pollMapper->find($pollId);
112
113
 	}
114
115
	/**
116
	 * get
117
	 * @NoAdminRequired
118
	 * @param integer $pollId
119
	 * @return array
120
	 */
121
 	public function getByToken($token) {
122
123
		if (!$this->acl->setToken($token)->getAllowView()) {
124
			throw new NotAuthorizedException;
125
		}
126
127
		return $this->pollMapper->find($this->acl->getPollId());
128
129
 	}
130
131
	/**
132
	 * delete
133
	 * @NoAdminRequired
134
	 * @NoCSRFRequired
135
	 * @param integer $pollId
136
	 * @return Poll
137
	 */
138
139
	public function delete($pollId) {
140
		$this->poll = $this->pollMapper->find($pollId);
141
142
		if (!$this->acl->setPollId($pollId)->getAllowEdit()) {
143
			throw new NotAuthorizedException;
144
		}
145
146
		if ($this->poll->getDeleted()) {
147
			$this->poll->setDeleted(0);
148
		} else {
149
			$this->poll->setDeleted(time());
150
		}
151
152
		$this->poll = $this->pollMapper->update($this->poll);
153
		$this->logService->setLog($this->poll->getId(), 'deletePoll');
154
155
		return $this->poll;
156
	}
157
158
	/**
159
	 * deletePermanently
160
	 * @NoAdminRequired
161
	 * @NoCSRFRequired
162
	 * @param integer $pollId
163
	 * @return Poll
164
	 */
165
166
	public function deletePermanently($pollId) {
167
		$this->poll = $this->pollMapper->find($pollId);
168
169
		if (!$this->acl->setPollId($pollId)->getAllowEdit() || !$this->poll->getDeleted()) {
170
			throw new NotAuthorizedException;
171
		}
172
173
		return $this->pollMapper->delete($this->poll);
174
	}
175
176
	/**
177
	 * write
178
	 * @NoAdminRequired
179
	 * @NoCSRFRequired
180
	 * @param string $type
181
	 * @param string $title
182
	 * @return Poll
183
	 */
184
185
	public function add($type, $title) {
186
		if (!\OC::$server->getUserSession()->isLoggedIn()) {
187
			throw new NotAuthorizedException;
188
		}
189
190
		// Validate valuess
191
		if (!in_array($type, $this->getValidPollType())) {
192
			throw new InvalidPollTypeException('Invalid poll type');
193
		}
194
195
		if (!$title) {
196
			throw new EmptyTitleException('Title must not be empty');
197
		}
198
199
		$this->poll = new Poll();
200
		$this->poll->setType($type);
201
		$this->poll->setCreated(time());
202
		$this->poll->setOwner(\OC::$server->getUserSession()->getUser()->getUID());
203
		$this->poll->setTitle($title);
204
		$this->poll->setDescription('');
205
		$this->poll->setAccess('hidden');
206
		$this->poll->setExpire(0);
207
		$this->poll->setAnonymous(0);
208
		$this->poll->setFullAnonymous(0);
209
		$this->poll->setAllowMaybe(0);
210
		$this->poll->setVoteLimit(0);
211
		$this->poll->setSettings('');
212
		$this->poll->setOptions('');
213
		$this->poll->setShowResults('always');
214
		$this->poll->setDeleted(0);
215
		$this->poll->setAdminAccess(0);
216
		$this->poll = $this->pollMapper->insert($this->poll);
217
218
		$this->logService->setLog($this->poll->getId(), 'addPoll');
219
220
		return $this->poll;
221
	}
222
223
	/**
224
	 * update
225
	 * @NoAdminRequired
226
	 * @NoCSRFRequired
227
	 * @param Array $poll
228
	 * @return Poll
229
	 */
230
231
	public function update($pollId, $poll) {
232
233
		$this->poll = $this->pollMapper->find($pollId);
234
235
		if (!$this->acl->setPollId($this->poll->getId())->getAllowEdit()) {
236
			throw new NotAuthorizedException;
237
		}
238
239
		// Validate valuess
240
		if (isset($poll['showResults']) && !in_array($poll['showResults'], $this->getValidShowResults())) {
241
			throw new InvalidShowResultsException('Invalid value for prop showResults');
242
		}
243
244
		if (isset($poll['access']) && !in_array($poll['access'], $this->getValidAccess())) {
245
			throw new InvalidAccessException('Invalid value for prop access '. $poll['access']);
246
		}
247
248
		if (isset($poll['title']) && !$poll['title']) {
249
			throw new EmptyTitleException('Title must not be empty');
250
		}
251
		$this->poll->deserializeArray($poll);
252
253
		$this->pollMapper->update($this->poll);
254
		$this->logService->setLog($this->poll->getId(), 'updatePoll');
255
256
		return $this->poll;
257
	}
258
259
	/**
260
	 * clone
261
	 * @NoAdminRequired
262
	 * @NoCSRFRequired
263
	 * @param integer $pollId
264
	 * @return Poll
265
	 */
266
	public function clone($pollId) {
267
268
		if (!$this->acl->setPollId($this->poll->getId())->getAllowView()) {
269
			throw new NotAuthorizedException;
270
		}
271
272
		$this->poll = $this->pollMapper->find($pollId);
273
274
		$this->poll->setCreated(time());
275
		$this->poll->setOwner(\OC::$server->getUserSession()->getUser()->getUID());
276
		$this->poll->setTitle('Clone of ' . $this->poll->getTitle());
277
		$this->poll->setDeleted(0);
278
		$this->poll->setId(0);
279
280
		$this->poll = $this->pollMapper->insert($this->poll);
281
		$this->logService->setLog($this->poll->getId(), 'addPoll');
282
283
		$this->optionService->clone($pollId, $this->poll->getId());
0 ignored issues
show
Bug Best Practice introduced by
The property optionService does not exist on OCA\Polls\Service\PollService. Did you maybe forget to declare it?
Loading history...
284
285
		return $this->poll;
286
287
	}
288
289
	public function getValidEnum() {
290
		return [
291
			'pollType' => $this->getValidPollType(),
292
			'access' => $this->getValidAccess(),
293
			'showResults' => $this->getValidShowResults()
294
		];
295
	}
296
297
	private function getValidPollType() {
298
		return ['datePoll', 'textPoll'];
299
	}
300
301
	private function getValidAccess() {
302
		return ['hidden', 'public'];
303
	}
304
305
	private function getValidShowResults() {
306
		return ['always', 'expired', 'never'];
307
	}
308
}
309