Completed
Pull Request — master (#966)
by René
29:43 queued 25:49
created

CommentService::add()   A

Complexity

Conditions 4
Paths 10

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 4
eloc 17
c 2
b 0
f 1
nc 10
nop 3
dl 0
loc 23
ccs 0
cts 20
cp 0
crap 20
rs 9.7
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
28
use OCP\IGroupManager;
29
use OCP\ILogger;
30
31
use OCA\Polls\Exceptions\NotAuthorizedException;
32
33
use OCA\Polls\Db\Comment;
34
use OCA\Polls\Db\CommentMapper;
35
use OCA\Polls\Db\Poll;
36
use OCA\Polls\Db\PollMapper;
37
use OCA\Polls\Model\Acl;
38
use OCA\Polls\Service\AnonymizeService;
39
40
41
42
class CommentService {
43
44
	private $userId;
45
	private $comment;
46
	private $commentMapper;
47
	private $logger;
48
	private $groupManager;
49
	private $pollMapper;
50
	private $anonymizer;
51
	private $acl;
52
53
	/**
54
	 * CommentService constructor.
55
	 * @param string $appName
56
	 * @param $UserId
57
	 * @param CommentMapper $commentMapper
58
	 * @param IGroupManager $groupManager
59
	 * @param PollMapper $pollMapper
60
	 * @param AnonymizeService $anonymizer
61
	 * @param Acl $acl
62
	 */
63
64
	public function __construct(
65
		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

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