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

CommentApiController::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
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
35
use OCA\Polls\Service\CommentService;
36
37
38
39
class CommentApiController extends ApiController {
40
41
	/**
42
	 * CommentApiController constructor.
43
	 * @param string $appName
44
	 * @param IRequest $request
45
	 * @param CommentService $commentService
46
	 */
47
48
	public function __construct(
49
		string $appName,
50
		IRequest $request,
51
		CommentService $commentService
52
	) {
53
		parent::__construct($appName,
54
			$request,
55
			'POST, GET, DELETE',
56
            'Authorization, Content-Type, Accept',
57
            1728000);
58
		$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...
59
	}
60
61
	/**
62
	 * get
63
	 * Read all comments of a poll based on the poll id and return list as array
64
	 * @NoAdminRequired
65
	 * @CORS
66
	 * @PublicPage
67
	 * @NoCSRFRequired
68
	 * @param integer $pollId
69
	 * @return DataResponse
70
	 */
71
	public function get($pollId, $token = '') {
72
		return new DataResponse($this->commentService->get($pollId, $token), Http::STATUS_OK);
73
	}
74
75
	/**
76
	 * Read all comments of a poll based on a share token and return list as array
77
	 * @NoAdminRequired
78
	 * @CORS
79
	 * @NoCSRFRequired
80
	 * @PublicPage
81
	 * @param string $token
82
	 * @return DataResponse
83
	 */
84
	public function getByToken($token) {
85
		return new DataResponse($this->commentService->get(0, $token), Http::STATUS_OK);
86
	}
87
88
	/**
89
	 * Write a new comment to the db and returns the new comment as array
90
	 * @NoAdminRequired
91
	 * @CORS
92
	 * @NoCSRFRequired
93
	 * @PublicPage
94
	 * @param int $pollId
95
	 * @param string $message
96
	 * @param string $token
97
	 * @return DataResponse
98
	 */
99
	public function add($message, $pollId, $token) {
100
		try {
101
			return new DataResponse($this->commentService->add($message, $pollId, $token), Http::STATUS_OK);
102
		} catch (Exception $e) {
103
			return new OCSForbiddenException($e);
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Controller\OCSForbiddenException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
104
		}
105
	}
106
107
	/**
108
	 * Delete Comment
109
	 * @NoAdminRequired
110
	 * @CORS
111
	 * @NoCSRFRequired
112
	 * @PublicPage
113
	 * @param int $commentId
114
	 * @param string $token
115
	 * @return DataResponse
116
	 */
117
	public function delete($commentId, $token) {
118
		try {
119
			return new DataResponse($this->commentService->delete($commentId, $token), Http::STATUS_OK);
120
		} catch (Exception $e) {
121
			return new DataResponse($e, Http::STATUS_UNAUTHORIZED);
122
		}
123
124
	}
125
126
}
127