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

CommentApiController::getByToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
ccs 0
cts 5
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\ApiController;
31
use OCP\AppFramework\OCS\OCSException;
32
use OCP\AppFramework\Http;
33
use OCP\AppFramework\Http\DataResponse;
34
use OCA\Polls\Exceptions\NotAuthorizedException;
35
36
use OCA\Polls\Service\CommentService;
37
38
39
40
class CommentApiController extends ApiController {
41
42
	/**
43
	 * CommentApiController constructor.
44
	 * @param string $appName
45
	 * @param IRequest $request
46
	 * @param CommentService $commentService
47
	 */
48
49
	public function __construct(
50
		string $appName,
51
		IRequest $request,
52
		CommentService $commentService
53
	) {
54
		parent::__construct($appName,
55
			$request,
56
			'POST, GET, DELETE',
57
            'Authorization, Content-Type, Accept',
58
            1728000);
59
		$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...
60
	}
61
62
	/**
63
	 * get
64
	 * Read all comments of a poll based on the poll id and return list as array
65
	 * @NoAdminRequired
66
	 * @CORS
67
	 * @PublicPage
68
	 * @NoCSRFRequired
69
	 * @param integer $pollId
70
	 * @return DataResponse
71
	 */
72
	public function get($pollId, $token = '') {
73
		try {
74
			return new DataResponse($this->commentService->get($pollId, $token), Http::STATUS_OK);
75
		} catch (NotAuthorizedException $e) {
76
			return new DataResponse($e, Http::STATUS_FORBIDDEN);
77
		}
78
	}
79
80
	/**
81
	 * Read all comments of a poll based on a share token and return list as array
82
	 * @NoAdminRequired
83
	 * @CORS
84
	 * @NoCSRFRequired
85
	 * @PublicPage
86
	 * @param string $token
87
	 * @return DataResponse
88
	 */
89
	public function getByToken($token) {
90
		try {
91
			return new DataResponse($this->commentService->get(0, $token), Http::STATUS_OK);
92
		} catch (NotAuthorizedException $e) {
93
			return new DataResponse($e, Http::STATUS_FORBIDDEN);
94
		}
95
	}
96
97
	/**
98
	 * Write a new comment to the db and returns the new comment as array
99
	 * @NoAdminRequired
100
	 * @CORS
101
	 * @NoCSRFRequired
102
	 * @PublicPage
103
	 * @param int $pollId
104
	 * @param string $message
105
	 * @param string $token
106
	 * @return DataResponse
107
	 */
108
	public function add($message, $pollId, $token) {
109
		try {
110
			return new DataResponse($this->commentService->add($message, $pollId, $token), Http::STATUS_OK);
111
		} catch (NotAuthorizedException $e) {
112
			return new DataResponse($e, Http::STATUS_FORBIDDEN);
113
		}
114
	}
115
116
	/**
117
	 * Delete Comment
118
	 * @NoAdminRequired
119
	 * @CORS
120
	 * @NoCSRFRequired
121
	 * @PublicPage
122
	 * @param int $commentId
123
	 * @param string $token
124
	 * @return DataResponse
125
	 */
126
	public function delete($commentId, $token) {
127
		try {
128
			return new DataResponse($this->commentService->delete($commentId, $token), Http::STATUS_OK);
129
		} catch (NotAuthorizedException $e) {
130
			return new DataResponse($e, Http::STATUS_FORBIDDEN);
131
		}
132
	}
133
134
}
135