Passed
Pull Request — master (#966)
by René
03:56
created

CommentApiController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 104
ccs 0
cts 49
cp 0
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A delete() 0 8 3
A getByToken() 0 7 3
A add() 0 7 3
A list() 0 7 3
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
	 * @PublicPage
73
	 * @NoCSRFRequired
74
	 * @param integer $pollId
75
	 * @return DataResponse
76
	 */
77
	public function list($pollId, $token = '') {
78
		try {
79
			return new DataResponse($this->commentService->list($pollId, $token), Http::STATUS_OK);
80
		} catch (NotAuthorizedException $e) {
81
			return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
0 ignored issues
show
Bug introduced by
'Unauthorized' 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

81
			return new DataResponse(/** @scrutinizer ignore-type */ 'Unauthorized', Http::STATUS_FORBIDDEN);
Loading history...
82
		} catch (DoesNotExistException $e) {
83
			return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND);
84
		}
85
	}
86
87
	/**
88
	 * Read all comments of a poll based on a share token and return list as array
89
	 * @NoAdminRequired
90
	 * @CORS
91
	 * @NoCSRFRequired
92
	 * @PublicPage
93
	 * @param string $token
94
	 * @return DataResponse
95
	 */
96
	public function getByToken($token) {
97
		try {
98
			return new DataResponse($this->commentService->get(0, $token), Http::STATUS_OK);
0 ignored issues
show
Bug introduced by
The method get() does not exist on OCA\Polls\Service\CommentService. ( Ignorable by Annotation )

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

98
			return new DataResponse($this->commentService->/** @scrutinizer ignore-call */ get(0, $token), Http::STATUS_OK);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
		} catch (NotAuthorizedException $e) {
100
			return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
0 ignored issues
show
Bug introduced by
'Unauthorized' 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

100
			return new DataResponse(/** @scrutinizer ignore-type */ 'Unauthorized', Http::STATUS_FORBIDDEN);
Loading history...
101
		} catch (DoesNotExistException $e) {
102
			return new DataResponse('Poll with token ' . $token . ' not found', Http::STATUS_NOT_FOUND);
103
		}
104
	}
105
106
	/**
107
	 * Write a new comment to the db and returns the new comment as array
108
	 * @NoAdminRequired
109
	 * @CORS
110
	 * @NoCSRFRequired
111
	 * @PublicPage
112
	 * @param int $pollId
113
	 * @param string $message
114
	 * @param string $token
115
	 * @return DataResponse
116
	 */
117
	public function add($message, $pollId, $token) {
118
		try {
119
			return new DataResponse($this->commentService->add($message, $pollId, $token), Http::STATUS_CREATED);
120
		} catch (NotAuthorizedException $e) {
121
			return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
0 ignored issues
show
Bug introduced by
'Unauthorized' 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

121
			return new DataResponse(/** @scrutinizer ignore-type */ 'Unauthorized', Http::STATUS_FORBIDDEN);
Loading history...
122
		} catch (DoesNotExistException $e) {
123
			return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND);
124
		}
125
	}
126
127
	/**
128
	 * Delete Comment
129
	 * @NoAdminRequired
130
	 * @CORS
131
	 * @NoCSRFRequired
132
	 * @PublicPage
133
	 * @param int $commentId
134
	 * @param string $token
135
	 * @return DataResponse
136
	 */
137
	public function delete($commentId, $token) {
138
		try {
139
			$this->commentService->delete($commentId, $token);
140
			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

140
			return new DataResponse(/** @scrutinizer ignore-type */ $commentId, Http::STATUS_OK);
Loading history...
141
		} catch (NotAuthorizedException $e) {
142
			return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
0 ignored issues
show
Bug introduced by
'Unauthorized' 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

142
			return new DataResponse(/** @scrutinizer ignore-type */ 'Unauthorized', Http::STATUS_FORBIDDEN);
Loading history...
143
		} catch (DoesNotExistException $e) {
144
			return new DataResponse('Comment does not exist', Http::STATUS_NOT_FOUND);
145
		}
146
	}
147
148
}
149