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

CommentApiController::add()   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 3
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
use OCP\AppFramework\Db\DoesNotExistException;
28
29
use OCP\IRequest;
30
use OCP\AppFramework\ApiController;
31
use OCP\AppFramework\Http;
32
use OCP\AppFramework\Http\DataResponse;
33
34
use OCA\Polls\Exceptions\NotAuthorizedException;
35
36
use OCA\Polls\Service\CommentService;
37
38
39
40
class CommentApiController extends ApiController {
41
42
	private $optionService;
0 ignored issues
show
introduced by
The private property $optionService is not used, and could be removed.
Loading history...
43
	/**
44
	 * CommentApiController constructor.
45
	 * @param string $appName
46
	 * @param IRequest $request
47
	 * @param CommentService $commentService
48
	 */
49
50
	public function __construct(
51
		string $appName,
52
		IRequest $request,
53
		CommentService $commentService
54
	) {
55
		parent::__construct($appName,
56
			$request,
57
			'POST, GET, DELETE',
58
            'Authorization, Content-Type, Accept',
59
            1728000);
60
		$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...
61
	}
62
63
	/**
64
	 * get
65
	 * Read all comments of a poll based on the poll id and return list as array
66
	 * @NoAdminRequired
67
	 * @CORS
68
	 * @PublicPage
69
	 * @NoCSRFRequired
70
	 * @param integer $pollId
71
	 * @return DataResponse
72
	 */
73
	public function get($pollId, $token = '') {
74
		try {
75
			return new DataResponse($this->commentService->get($pollId, $token), Http::STATUS_OK);
76
		} catch (NotAuthorizedException $e) {
77
			return new DataResponse($e, Http::STATUS_FORBIDDEN);
78
		} catch (DoesNotExistException $e) {
79
			return new DataResponse($pollId, Http::STATUS_NOT_FOUND);
0 ignored issues
show
Bug introduced by
$pollId of type integer is incompatible with the type array|object expected by parameter $data of OCP\AppFramework\Http\DataResponse::__construct(). ( Ignorable by Annotation )

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

79
			return new DataResponse(/** @scrutinizer ignore-type */ $pollId, Http::STATUS_NOT_FOUND);
Loading history...
80
		}
81
	}
82
83
	/**
84
	 * Read all comments of a poll based on a share token and return list as array
85
	 * @NoAdminRequired
86
	 * @CORS
87
	 * @NoCSRFRequired
88
	 * @PublicPage
89
	 * @param string $token
90
	 * @return DataResponse
91
	 */
92
	public function getByToken($token) {
93
		try {
94
			return new DataResponse($this->commentService->get(0, $token), Http::STATUS_OK);
95
		} catch (NotAuthorizedException $e) {
96
			return new DataResponse($e, Http::STATUS_FORBIDDEN);
97
		}
98
	}
99
100
	/**
101
	 * Write a new comment to the db and returns the new comment as array
102
	 * @NoAdminRequired
103
	 * @CORS
104
	 * @NoCSRFRequired
105
	 * @PublicPage
106
	 * @param int $pollId
107
	 * @param string $message
108
	 * @param string $token
109
	 * @return DataResponse
110
	 */
111
	public function add($message, $pollId, $token) {
112
		try {
113
			return new DataResponse($this->commentService->add($message, $pollId, $token), Http::STATUS_OK);
114
		} catch (NotAuthorizedException $e) {
115
			return new DataResponse($e, Http::STATUS_FORBIDDEN);
116
		}
117
	}
118
119
	/**
120
	 * Delete Comment
121
	 * @NoAdminRequired
122
	 * @CORS
123
	 * @NoCSRFRequired
124
	 * @PublicPage
125
	 * @param int $commentId
126
	 * @param string $token
127
	 * @return DataResponse
128
	 */
129
	public function delete($commentId, $token) {
130
		try {
131
			$this->commentService->delete($commentId, $token);
132
			return new DataResponse($commentId, Http::STATUS_OK);
0 ignored issues
show
Bug introduced by
$commentId of type integer is incompatible with the type array|object expected by parameter $data of OCP\AppFramework\Http\DataResponse::__construct(). ( Ignorable by Annotation )

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

132
			return new DataResponse(/** @scrutinizer ignore-type */ $commentId, Http::STATUS_OK);
Loading history...
133
		} catch (NotAuthorizedException $e) {
134
			return new DataResponse($commentId, Http::STATUS_FORBIDDEN);
135
		} catch (DoesNotExistException $e) {
136
			return new DataResponse($commentId, Http::STATUS_NOT_FOUND);
137
		}
138
	}
139
140
}
141