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

CommentApiController::add()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 12
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\IURLGenerator;
31
use OCP\AppFramework\ApiController;
32
use OCP\AppFramework\Http;
33
use OCP\AppFramework\Http\DataResponse;
34
35
use OCA\Polls\Exceptions\NotAuthorizedException;
36
37
use OCA\Polls\Service\CommentService;
38
39
40
41
class CommentApiController extends ApiController {
42
43
	private $optionService;
0 ignored issues
show
introduced by
The private property $optionService is not used, and could be removed.
Loading history...
44
	private $urlGenerator;
45
	/**
46
	 * CommentApiController constructor.
47
	 * @param string $appName
48
	 * @param IRequest $request
49
	 * @param CommentService $commentService
50
	 */
51
52
	public function __construct(
53
		string $appName,
54
		IRequest $request,
55
		IURLGenerator $urlGenerator,
56
		CommentService $commentService
57
	) {
58
		parent::__construct($appName,
59
			$request,
60
			'POST, GET, DELETE',
61
            'Authorization, Content-Type, Accept',
62
            1728000);
63
		$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...
64
		$this->urlGenerator = $urlGenerator;
65
	}
66
67
	/**
68
	 * get
69
	 * Read all comments of a poll based on the poll id and return list as array
70
	 * @NoAdminRequired
71
	 * @CORS
72
	 * @NoCSRFRequired
73
	 * @param integer $pollId
74
	 * @return DataResponse
75
	 */
76
	public function list($pollId) {
77
		try {
78
			return new DataResponse($this->commentService->list($pollId), Http::STATUS_OK);
79
		} catch (DoesNotExistException $e) {
80
			return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND);
0 ignored issues
show
Bug introduced by
'Poll with id ' . $pollId . ' not found' of type string 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

80
			return new DataResponse(/** @scrutinizer ignore-type */ 'Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND);
Loading history...
81
		} catch (NotAuthorizedException $e) {
82
			return new DataResponse($e->getMessage(), $e->getStatus());
83
		}
84
	}
85
86
	/**
87
	 * Write a new comment to the db and returns the new comment as array
88
	 * @NoAdminRequired
89
	 * @CORS
90
	 * @NoCSRFRequired
91
	 * @param int $pollId
92
	 * @param string $message
93
	 * @return DataResponse
94
	 */
95
	public function add($pollId, $message) {
96
		try {
97
			return new DataResponse($this->commentService->add($message, $pollId), Http::STATUS_CREATED);
0 ignored issues
show
Bug introduced by
$message of type string is incompatible with the type integer expected by parameter $pollId of OCA\Polls\Service\CommentService::add(). ( Ignorable by Annotation )

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

97
			return new DataResponse($this->commentService->add(/** @scrutinizer ignore-type */ $message, $pollId), Http::STATUS_CREATED);
Loading history...
98
		} catch (DoesNotExistException $e) {
99
			return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND);
0 ignored issues
show
Bug introduced by
'Poll with id ' . $pollId . ' not found' of type string 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

99
			return new DataResponse(/** @scrutinizer ignore-type */ 'Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND);
Loading history...
100
		} catch (NotAuthorizedException $e) {
101
			return new DataResponse($e->getMessage(), $e->getStatus());
102
		}
103
	}
104
105
	/**
106
	 * Delete Comment
107
	 * @NoAdminRequired
108
	 * @CORS
109
	 * @NoCSRFRequired
110
	 * @param int $commentId
111
	 * @return DataResponse
112
	 */
113
	public function delete($commentId) {
114
		try {
115
			$this->commentService->delete($commentId);
116
			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

116
			return new DataResponse(/** @scrutinizer ignore-type */ $commentId, Http::STATUS_OK);
Loading history...
117
		} catch (DoesNotExistException $e) {
118
			return new DataResponse('Comment does not exist', Http::STATUS_NOT_FOUND);
0 ignored issues
show
Bug introduced by
'Comment does not exist' of type string 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

118
			return new DataResponse(/** @scrutinizer ignore-type */ 'Comment does not exist', Http::STATUS_NOT_FOUND);
Loading history...
119
		} catch (NotAuthorizedException $e) {
120
			return new DataResponse($e->getMessage(), $e->getStatus());
121
		}
122
	}
123
124
}
125