Passed
Pull Request — master (#1016)
by René
04:03
created

CommentController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 7
ccs 0
cts 7
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
use OCP\AppFramework\Db\DoesNotExistException;
28
use OCA\Polls\Exceptions\NotAuthorizedException;
29
30
use OCP\IRequest;
31
use OCP\AppFramework\Controller;
32
use OCP\AppFramework\Http;
33
use OCP\AppFramework\Http\DataResponse;
34
35
use OCA\Polls\Service\CommentService;
36
37
38
39
class CommentController extends Controller {
40
41
	/** @var CommentService */
42
	private $commentService;
43
44
	/**
45
	 * CommentController constructor
46
	 * @param string $appName
47
	 * @param IRequest $request
48
	 * @param CommentService $commentService
49
	 */
50
51
	public function __construct(
52
		string $appName,
53
		IRequest $request,
54
		CommentService $commentService
55
	) {
56
		parent::__construct($appName, $request);
57
		$this->commentService = $commentService;
58
	}
59
60
	// /**
61
	//  * Read all comments of a poll based on the poll id and return list as array
62
	//  * @NoAdminRequired
63
	//  * @param int $pollId
64
	//  * @param string $token
65
	//  * @return DataResponse
66
	//  */
67
	// public function list($pollId) {
68
	// 	return new DataResponse($this->commentService->list($pollId), Http::STATUS_OK);
69
	// }
70
	//
71
	/**
72
	 * Write a new comment to the db and returns the new comment as array
73
	 * @NoAdminRequired
74
	 * @PublicPage
75
	 * @param int $pollId
76
	 * @param string $message
77
	 * @param string $token
78
	 * @return DataResponse
79
	 */
80
	public function add($pollId, $message, $token) {
81
		try {
82
			return new DataResponse($this->commentService->add($pollId, $message, $token), Http::STATUS_OK);
83
		} catch (Exception $e) {
84
			return new DataResponse($e, Http::STATUS_UNAUTHORIZED);
85
		}
86
	}
87
88
	/**
89
	 * Delete Comment
90
	 * @NoAdminRequired
91
	 * @PublicPage
92
	 * @param int $commentId
93
	 * @param string $token
94
	 * @return DataResponse
95
	 */
96
	public function delete($commentId, $token) {
97
		try {
98
			return new DataResponse($this->commentService->delete($commentId, $token), Http::STATUS_OK);
99
		} catch (NotAuthorizedException $e) {
100
			return new DataResponse($e, Http::STATUS_FORBIDDEN);
101
		} catch (DoesNotExistException $e) {
102
			return new DataResponse($e, Http::STATUS_OK);
103
		}
104
105
	}
106
107
}
108