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

CommentService::delete()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 4
eloc 9
c 2
b 0
f 1
nc 3
nop 2
dl 0
loc 15
ccs 0
cts 12
cp 0
crap 20
rs 9.9666
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\Service;
25
26
use \Exception;
27
use OCP\AppFramework\Db\DoesNotExistException;
28
29
use OCP\IGroupManager;
30
use OCP\ILogger;
31
32
use OCA\Polls\Exceptions\NotAuthorizedException;
33
34
use OCA\Polls\Db\Comment;
35
use OCA\Polls\Db\CommentMapper;
36
use OCA\Polls\Db\Poll;
37
use OCA\Polls\Db\PollMapper;
38
use OCA\Polls\Model\Acl;
39
use OCA\Polls\Service\AnonymizeService;
40
41
42
43
class CommentService {
44
45
	private $userId;
46
	private $comment;
47
	private $commentMapper;
48
	private $logger;
49
	private $groupManager;
50
	private $pollMapper;
51
	private $anonymizer;
52
	private $acl;
53
54
	/**
55
	 * CommentService constructor.
56
	 * @param string $appName
57
	 * @param $UserId
58
	 * @param CommentMapper $commentMapper
59
	 * @param IGroupManager $groupManager
60
	 * @param PollMapper $pollMapper
61
	 * @param AnonymizeService $anonymizer
62
	 * @param Acl $acl
63
	 */
64
65
	public function __construct(
66
		string $appName,
0 ignored issues
show
Unused Code introduced by
The parameter $appName is not used and could be removed. ( Ignorable by Annotation )

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

66
		/** @scrutinizer ignore-unused */ string $appName,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
		$userId,
68
		ILogger $logger,
69
		CommentMapper $commentMapper,
70
		IGroupManager $groupManager,
71
		PollMapper $pollMapper,
72
		AnonymizeService $anonymizer,
73
		Acl $acl
74
	) {
75
		$this->userId = $userId;
76
		$this->commentMapper = $commentMapper;
77
		$this->logger = $logger;
78
		$this->groupManager = $groupManager;
79
		$this->pollMapper = $pollMapper;
80
		$this->anonymizer = $anonymizer;
81
		$this->acl = $acl;
82
	}
83
84
	/**
85
	 * get
86
	 * Read all comments of a poll based on the poll id and return list as array
87
	 * @NoAdminRequired
88
	 * @param integer $pollId
89
	 * @param string $token
90
	 * @return Array
91
	 */
92
	public function get($pollId = 0, $token = '') {
93
		$this->logger->debug('call commentService->get(' . $pollId . ', '. $token . ')');
94
95
		if (!$this->acl->checkAuthorize($pollId, $token)) {
96
			throw new NotAuthorizedException;
97
		}
98
99
		try {
100
			if (!$this->acl->getAllowSeeUsernames()) {
101
				$this->anonymizer->set($this->acl->getPollId(), $this->acl->getUserId());
102
				return $this->anonymizer->getComments();
103
			} else {
104
				return $this->commentMapper->findByPoll($this->acl->getPollId());
105
			}
106
107
		} catch (\Exception $e) {
108
			$this->logger->alert('Error reading comments for pollId ' . $pollId . ': '. $e);
109
			throw new DoesNotExistException($e);
110
		}
111
112
	}
113
114
	/**
115
	 * Write a new comment to the db and returns the new comment as array
116
	 * @NoAdminRequired
117
	 * @param string $message
118
	 * @param int $pollId
119
	 * @param string $token
120
	 * @return Comment
121
	 */
122
	public function add($message, $pollId = 0, $token = '') {
123
		$this->logger->debug('call commentService->write("' . $message . '", ' .$pollId . ', "' .$token . '")');
124
125
		if (!$this->acl->checkAuthorize($pollId, $token)) {
126
			throw new NotAuthorizedException;
127
		}
128
129
		try {
130
			if ($this->acl->getAllowComment()) {
131
				$this->comment = new Comment();
132
				$this->comment->setPollId($this->acl->getPollId());
133
				$this->comment->setUserId($this->acl->getUserId());
134
				$this->comment->setComment($message);
135
				$this->comment->setDt(date('Y-m-d H:i:s'));
136
				$this->comment = $this->commentMapper->insert($this->comment);
137
				return $this->comment;
138
			} else {
139
				throw new NotAuthorizedException;
140
			}
141
142
		} catch (\Exception $e) {
143
			$this->logger->alert('Error writing comment for pollId ' . $pollId . ': '. $e);
144
			throw new NotAuthorizedException($e);
145
		}
146
	}
147
148
	/**
149
	 * delete
150
	 * Delete Comment
151
	 * @NoAdminRequired
152
	 * @param int $commentId
153
	 * @param string $token
154
	 * @return Comment
155
	 */
156
	public function delete($commentId, $token = '') {
157
		$this->logger->debug('call commentService->delete(' . $commentId . ', "' .$token . '")');
158
159
		try {
160
			$this->comment = $this->commentMapper->find($commentId);
161
		} catch (DoesNotExistException $e) {
162
			return new DoesNotExistException($e);
163
		}
164
165
		if (!$this->acl->checkAuthorize($this->comment->getPollId(), $token) || $this->comment->getUserId() !== $this->acl->getUserId()) {
166
			throw new NotAuthorizedException;
167
		}
168
169
		$this->commentMapper->delete($this->comment);
170
		return $this->comment;
171
172
	}
173
174
}
175