Passed
Pull Request — master (#966)
by René
04:02
created

CommentController::writeByToken()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 6
rs 10
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 Exception;
27
28
use OCP\IRequest;
29
use OCP\ILogger;
30
use OCP\AppFramework\Controller;
31
use OCP\AppFramework\Http;
32
use OCP\AppFramework\Http\DataResponse;
33
34
use OCA\Polls\Service\CommentService;
35
36
37
38
class CommentController extends Controller {
39
40
	/**
41
	 * CommentController constructor.
42
	 * @param string $appName
43
	 * @param IRequest $request
44
	 * @param CommentService $commentService
45
	 */
46
47
	public function __construct(
48
		string $appName,
49
		IRequest $request,
50
		CommentService $commentService
51
	) {
52
		parent::__construct($appName, $request);
53
		$this->commentService = $commentService;
0 ignored issues
show
Bug Best Practice introduced by
The property commentService does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
54
	}
55
56
	/**
57
	 * get
58
	 * Read all comments of a poll based on the poll id and return list as array
59
	 * @NoAdminRequired
60
	 * @NoCSRFRequired
61
	 * @param integer $pollId
62
	 * @return DataResponse
63
	 */
64
	public function get($pollId) {
65
		return new DataResponse($this->commentService->get($pollId), Http::STATUS_OK);
66
	}
67
68
	/**
69
	 * Read all comments of a poll based on a share token and return list as array
70
	 * @NoAdminRequired
71
	 * @NoCSRFRequired
72
	 * @PublicPage
73
	 * @param string $token
74
	 * @return DataResponse
75
	 */
76
	public function getByToken($token) {
77
		return new DataResponse($this->commentService->get(0, $token), Http::STATUS_OK);
78
	}
79
80
	/**
81
	 * Write a new comment to the db and returns the new comment as array
82
	 * @NoAdminRequired
83
	 * @PublicPage
84
	 * @param int $pollId
85
	 * @param string $message
86
	 * @param string $token
87
	 * @return DataResponse
88
	 */
89
	public function add($message, $pollId, $token) {
90
		try {
91
			return new DataResponse($this->commentService->add($message, $pollId, $token), Http::STATUS_OK);
92
		} catch (Exception $e) {
93
			return new DataResponse($e, Http::STATUS_UNAUTHORIZED);
94
		}
95
	}
96
97
	/**
98
	 * Delete Comment
99
	 * @NoAdminRequired
100
	 * @PublicPage
101
	 * @param int $commentId
102
	 * @param string $token
103
	 * @return DataResponse
104
	 */
105
	public function delete($commentId, $token) {
106
		try {
107
			return new DataResponse($this->commentService->delete($commentId, $token), Http::STATUS_OK);
108
		} catch (Exception $e) {
109
			return new DataResponse($e, Http::STATUS_UNAUTHORIZED);
110
		}
111
112
	}
113
114
}
115