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

PublicController::commentDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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\IRequest;
27
use OCP\IURLGenerator;
28
use OCP\AppFramework\Controller;
29
use OCP\AppFramework\Http\DataResponse;
30
use OCP\AppFramework\Http\Template\PublicTemplateResponse;
31
32
use OCA\Polls\Exceptions\Exception;
33
34
35
use OCA\Polls\DB\Share;
36
use OCA\Polls\DB\Poll;
37
use OCA\Polls\Model\Acl;
38
use OCA\Polls\Service\CommentService;
39
use OCA\Polls\Service\OptionService;
40
use OCA\Polls\Service\PollService;
41
use OCA\Polls\Service\ShareService;
42
use OCA\Polls\Service\SubscriptionService;
43
use OCA\Polls\Service\VoteService;
44
45
class PublicController extends Controller {
46
47
	/** @var IURLGenerator */
48
	private $urlGenerator;
49
50
	/** @var CommentService */
51
	private $commentService;
52
53
	/** @var OptionService */
54
	private $optionService;
55
56
	/** @var PollService */
57
	private $pollService;
58
59
	/** @var Poll */
60
	private $poll;
61
62
	/** @var ShareService */
63
	private $shareService;
64
65
	/** @var SubscriptionService */
66
	private $subscriptionService;
67
68
	/** @var Share */
69
	private $share;
70
71
	/** @var VoteService */
72
	private $voteService;
73
74
	/** @var SystemService */
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Controller\SystemService 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...
75
	private $systemService;
76
77
	use ResponseHandle;
78
79
	/**
80
	 * PollController constructor.
81
	 * @param string $appName
82
	 * @param IRequest $request
83
	 * @param IURLGenerator $urlGenerator
84
	 * @param Acl $acl
85
	 * @param CommentService $commentService
86
	 * @param OptionService $optionService
87
	 * @param PollService $pollService
88
	 * @param Poll $poll
89
	 * @param ShareService $shareService
90
	 * @param Share $share
91
	 * @param SubscriptionService $subscriptionService
92
	 * @param VoteService $voteService
93
	 * @param SystemService $systemService
94
	 */
95
96
	public function __construct(
97
		string $appName,
98
		IRequest $request,
99
		IURLGenerator $urlGenerator,
100
		Acl $acl,
101
		CommentService $commentService,
102
		OptionService $optionService,
103
		PollService $pollService,
104
		Poll $poll,
105
		ShareService $shareService,
106
		Share $share,
107
		SubscriptionService $subscriptionService,
108
		VoteService $voteService,
109
		SystemService $systemService
110
	) {
111
		parent::__construct($appName, $request);
112
		$this->urlGenerator = $urlGenerator;
113
		$this->acl = $acl;
0 ignored issues
show
Bug Best Practice introduced by
The property acl does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
114
		$this->commentService = $commentService;
115
		$this->optionService = $optionService;
116
		$this->pollService = $pollService;
117
		$this->poll = $poll;
118
		$this->shareService = $shareService;
119
		$this->share = $share;
120
		$this->subscriptionService = $subscriptionService;
121
		$this->voteService = $voteService;
122
		$this->systemService = $systemService;
123
	}
124
125
	/**
126
	 * @PublicPage
127
	 * @NoAdminRequired
128
	 * @NoCSRFRequired
129
	 * @param string $token
130
	 * @return PublicTemplateResponse
131
	 */
132
	public function votePage() {
133
		if (\OC::$server->getUserSession()->isLoggedIn()) {
134
			return new TemplateResponse('polls', 'polls.tmpl', [
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Controller\TemplateResponse 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...
135
				'urlGenerator' => $this->urlGenerator]);
136
		} else {
137
			return new PublicTemplateResponse('polls', 'polls.tmpl', [
138
				'urlGenerator' => $this->urlGenerator]);
139
		}
140
	}
141
142
	/**
143
	 * get complete poll via token
144
	 * @NoAdminRequired
145
	 * @PublicPage
146
	 * @param string $token
147
	 * @return DataResponse
148
	 */
149
	public function poll(string $token) {
150
		return $this->response(function () use ($token) {
151
			$this->share = $this->shareService->get($token);
152
			$this->acl->setShare($this->share);
153
			$this->poll = $this->pollService->get($this->share->getPollId());
154
			return $this->buildPoll();
155
		});
156
	}
157
158
	/**
159
	 * Set vote with token
160
	 * @NoAdminRequired
161
	 * @PublicPage
162
	 * @param Array $option
163
	 * @param string $setTo
164
	 * @param string $token
165
	 * @return DataResponse
166
	 */
167
	public function vote($optionId, $setTo, $token) {
168
		return $this->response(function () use ($optionId, $setTo, $token) {
169
			return ['vote' =>$this->voteService->set($optionId, $setTo, $token)];
170
		});
171
	}
172
173
	/**
174
	 * Write a new comment to the db and returns the new comment as array
175
	 * @NoAdminRequired
176
	 * @PublicPage
177
	 * @param int $pollId
178
	 * @param string $message
179
	 * @param string $token
180
	 * @return DataResponse
181
	 */
182
	public function comment($token, $message) {
183
		return $this->response(function () use ($token, $message) {
184
			return ['comment'=> $this->commentService->add(null, $token, $message)];
185
		});
186
	}
187
188
	/**
189
	 * Delete Comment
190
	 * @NoAdminRequired
191
	 * @PublicPage
192
	 * @param int $commentId
193
	 * @param string $token
194
	 * @return DataResponse
195
	 */
196
	public function commentDelete($commentId, $token) {
197
		return $this->responseDeleteTolerant(function () use ($commentId, $token) {
198
			return ['comment'=> $this->commentService->delete($commentId, $token)];
199
		});
200
	}
201
	/**
202
	 * Get subscription status
203
	 * @PublicPage
204
	 * @NoAdminRequired
205
	 * @param int $pollId
206
	 * @return DataResponse
207
	 * @throws DoesNotExistException
208
	 */
209
	public function subscription($token) {
210
		return $this->response(function () use ($token) {
211
			return ['subscribed' => $this->subscriptionService->get(0, $token)];
212
		});
213
	}
214
215
	/**
216
	 * subscribe
217
	 * @PublicPage
218
	 * @NoAdminRequired
219
	 * @param string $token
220
	 * @return DataResponse
221
	 */
222
	public function subscribe($token) {
223
		return $this->response(function () use ($token) {
224
			return ['subscribed' => $this->subscriptionService->set(0, $token, true)];
225
		});
226
	}
227
228
	/**
229
	 * Unsubscribe
230
	 * @PublicPage
231
	 * @NoAdminRequired
232
	 * @param string $token
233
	 * @return DataResponse
234
	 */
235
	public function unsubscribe($token) {
236
		return $this->response(function () use ($token) {
237
			return ['subscribed' => $this->subscriptionService->set(0, $token, false)];
238
		});
239
	}
240
241
	/**
242
	 * Validate it the user name is reservrd
243
	 * return false, if this username already exists as a user or as
244
	 * a participant of the poll
245
	 * @NoAdminRequired
246
	 * @PublicPage
247
	 * @return DataResponse
248
	 */
249
	public function validatePublicUsername($userName, $token) {
250
		try {
251
			return new DataResponse(['result' => $this->systemService->validatePublicUsername($userName, $token), 'name' => $userName], Http::STATUS_OK);
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Controller\Http 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...
252
		} catch (\Exception $e) {
253
			return new DataResponse(['message' => $e->getMessage()], Http::STATUS_CONFLICT);
254
		}
255
	}
256
257
	/**
258
	 * Validate email address (simple validation)
259
	 * @NoAdminRequired
260
	 * @PublicPage
261
	 * @return DataResponse
262
	 */
263
	public function validateEmailAddress($emailAddress) {
264
		try {
265
			return new DataResponse(['result' => $this->systemService->validateEmailAddress($emailAddress), 'emailAddress' => $emailAddress], Http::STATUS_OK);
266
		} catch (\Exception $e) {
267
			return new DataResponse(['message' => $e->getMessage()], Http::STATUS_CONFLICT);
268
		}
269
	}
270
271
	/**
272
	 * get complete poll
273
	 * @NoAdminRequired
274
	 * @param int $pollId
275
	 * @param string $token
276
	 * @return Array
277
	 */
278
	private function buildPoll() {
279
		try {
280
			$comments = $this->commentService->list($this->poll->getId(), $this->acl);
281
		} catch (Exception $e) {
282
			$comments = [];
283
		}
284
285
		try {
286
			$options = $this->optionService->list($this->poll->getId());
287
		} catch (Exception $e) {
288
			$options = [];
289
		}
290
291
		try {
292
			$votes = $this->voteService->list($this->poll->getId());
293
		} catch (Exception $e) {
294
			$votes = [];
295
		}
296
297
		return [
298
			'acl' => $this->acl,
299
			'poll' => $this->poll,
300
			'comments' => $comments,
301
			'options' => $options,
302
			'share' => $this->share,
303
			'shares' => [],
304
			'votes' => $votes,
305
		];
306
	}
307
}
308