Passed
Pull Request — master (#1254)
by René
03:58
created

PollController::getByToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
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\Controller;
25
26
use OCP\AppFramework\Db\DoesNotExistException;
27
use OCA\Polls\Exceptions\Exception;
28
29
use OCP\IRequest;
30
use OCP\AppFramework\Controller;
31
use OCP\AppFramework\Http;
32
use OCP\AppFramework\Http\DataResponse;
33
34
use OCA\Polls\DB\Share;
35
use OCA\Polls\Service\PollService;
36
use OCA\Polls\Service\CommentService;
37
use OCA\Polls\Service\OptionService;
38
use OCA\Polls\Service\ShareService;
39
use OCA\Polls\Service\VoteService;
40
use OCA\Polls\Model\Acl;
41
42
class PollController extends Controller {
43
44
	/** @var PollService */
45
	private $pollService;
46
47
	/** @var CommentService */
48
	private $commentService;
49
50
	/** @var OptionService */
51
	private $optionService;
52
53
	/** @var ShareService */
54
	private $shareService;
55
56
	/** @var Share */
57
	private $share;
58
59
	/** @var VoteService */
60
	private $voteService;
61
62
	/** @var Acl */
63
	private $acl;
64
65
	use ResponseHandle;
66
67
	/**
68
	 * PollController constructor.
69
	 * @param string $appName
70
	 * @param IRequest $request
71
	 * @param PollService $pollService
72
	 * @param CommentService $commentService
73
	 * @param OptionService $optionService
74
	 * @param ShareService $shareService
75
	 * @param VoteService $voteService
76
	 * @param Share $share
77
	 * @param Acl $acl
78
	 */
79
80
	public function __construct(
81
		string $appName,
82
		IRequest $request,
83
		PollService $pollService,
84
		CommentService $commentService,
85
		OptionService $optionService,
86
		ShareService $shareService,
87
		VoteService $voteService,
88
		Share $share,
89
		Acl $acl
90
	) {
91
		parent::__construct($appName, $request);
92
		$this->pollService = $pollService;
93
		$this->commentService = $commentService;
94
		$this->optionService = $optionService;
95
		$this->shareService = $shareService;
96
		$this->voteService = $voteService;
97
		$this->share = $share;
98
		$this->acl = $acl;
99
	}
100
101
	/**
102
	 * Get list of polls
103
	 * @NoAdminRequired
104
	 * @return DataResponse
105
	 */
106
107
	public function list() {
108
		return $this->response(function () {
109
			return $this->pollService->list();
110
		});
111
	}
112
113
114
	/**
115
	 * get complete poll
116
	 * @NoAdminRequired
117
	 * @PublicPage
118
	 * @deprecated use getByToken/getById
119
	 * @param int $pollId
120
	 * @param string $token
121
	 * @return DataResponse
122
	 */
123
	public function get(int $pollId = 0, string $token = '') {
124
		if ($token) {
125
			return $this->getByToken($token);
126
		} else {
127
			return $this->getByPollId($pollId);
128
		}
129
	}
130
131
	/**
132
	 * get complete poll via token
133
	 * @NoAdminRequired
134
	 * @PublicPage
135
	 * @param string $token
136
	 * @return DataResponse
137
	 */
138
	public function getByToken(string $token) {
139
		return $this->response(function () use ($token) {
140
			$this->share = $this->shareService->get($token);
141
			$this->acl->setShare($this->share);
142
			$this->poll = $this->pollService->get($this->share->getPollId());
0 ignored issues
show
Bug Best Practice introduced by
The property poll does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
143
			return $this->build();
144
		});
145
	}
146
147
	/**
148
	 * get complete poll via pollId
149
	 * @NoAdminRequired
150
	 * @param int $pollId
151
	 * @return DataResponse
152
	 */
153
	public function getByPollId(int $pollId) {
154
		return $this->response(function () use ($pollId) {
155
			$this->share = null;
156
			$this->poll = $this->pollService->get($pollId);
0 ignored issues
show
Bug Best Practice introduced by
The property poll does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
157
			$this->acl->setPoll($this->poll);
158
			return $this->build();
159
		});
160
	}
161
162
	/**
163
	 * get complete poll
164
	 * @NoAdminRequired
165
	 * @PublicPage
166
	 * @param int $pollId
167
	 * @param string $token
168
	 * @return Array
169
	 */
170
	private function build() {
171
		try {
172
			$comments = $this->commentService->list($this->poll->getId());
173
		} catch (Exception $e) {
174
			$comments = [];
175
		}
176
177
		try {
178
			$options = $this->optionService->list($this->poll->getId());
179
		} catch (Exception $e) {
180
			$options = [];
181
		}
182
183
		try {
184
			$votes = $this->voteService->list($this->poll->getId());
185
		} catch (Exception $e) {
186
			$votes = [];
187
		}
188
189
		try {
190
			$shares = $this->shareService->list($this->poll->getId());
191
		} catch (Exception $e) {
192
			$shares = [];
193
		}
194
195
		return [
196
			'acl' => $this->acl,
197
			'poll' => $this->poll,
198
			'comments' => $comments,
199
			'options' => $options,
200
			'share' => $this->share,
201
			'shares' => $shares,
202
			'votes' => $votes,
203
		];
204
	}
205
206
	/**
207
	 * Add poll
208
	 * @NoAdminRequired
209
	 * @param string $type
210
	 * @param string $title
211
	 * @return DataResponse
212
	 */
213
214
	public function add($type, $title) {
215
		return $this->responseCreate(function () use ($type, $title) {
216
			return $this->pollService->add($type, $title);
217
		});
218
	}
219
220
	/**
221
	 * Update poll configuration
222
	 * @NoAdminRequired
223
	 * @param int $pollId
224
	 * @param array $poll
225
	 * @return DataResponse
226
	 */
227
228
	public function update($pollId, $poll) {
229
		return $this->response(function () use ($pollId, $poll) {
230
			return $this->pollService->update($pollId, $poll);
231
		});
232
	}
233
234
	/**
235
	 * Switch deleted status (move to deleted polls)
236
	 * @NoAdminRequired
237
	 * @param int $pollId
238
	 * @return DataResponse
239
	 */
240
241
	public function delete($pollId) {
242
		return $this->response(function () use ($pollId) {
243
			return $this->pollService->update($pollId);
0 ignored issues
show
Bug introduced by
The call to OCA\Polls\Service\PollService::update() has too few arguments starting with poll. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

243
			return $this->pollService->/** @scrutinizer ignore-call */ update($pollId);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
244
		});
245
	}
246
247
	/**
248
	 * Delete poll
249
	 * @NoAdminRequired
250
	 * @param Array $poll
251
	 * @return DataResponse
252
	 */
253
254
	public function deletePermanently($pollId) {
255
		return $this->responseDeleteTolerant(function () use ($pollId) {
256
			return $this->pollService->deletePermanently($pollId);
257
		});
258
	}
259
260
	/**
261
	 * Clone poll
262
	 * @NoAdminRequired
263
	 * @param int $pollId
264
	 * @return DataResponse
265
	 */
266
	public function clone($pollId) {
267
		return $this->response(function () use ($pollId) {
268
			$poll = $this->pollService->clone($pollId);
269
			$this->optionService->clone($pollId, $poll->getId());
270
271
			return $poll;
272
		});
273
	}
274
275
	/**
276
	 * Collect email addresses from particitipants
277
	 * @NoAdminRequired
278
	 * @param Array $poll
279
	 * @return DataResponse
280
	 */
281
282
	public function getParticipantsEmailAddresses($pollId) {
283
		return $this->response(function () use ($pollId) {
284
			return $this->pollService->getParticipantsEmailAddresses($pollId);
285
		});
286
		try {
0 ignored issues
show
Unused Code introduced by
TryCatchNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
287
			return new DataResponse($this->pollService->getParticipantsEmailAddresses($pollId), Http::STATUS_OK);
288
		} catch (DoesNotExistException $e) {
289
			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
290
		} catch (Exception $e) {
291
			return new DataResponse(['message' => $e->getMessage()], $e->getStatus());
292
		}
293
	}
294
}
295