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

CommentService::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 3
b 0
f 1
nc 2
nop 2
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 6
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\Service;
25
26
use \Exception;
27
use OCP\ILogger;
28
29
use OCA\Polls\Exceptions\NotAuthorizedException;
30
use OCA\Polls\Db\Comment;
31
use OCA\Polls\Db\CommentMapper;
32
use OCA\Polls\Model\Acl;
33
use OCA\Polls\Service\AnonymizeService;
34
35
36
37
class CommentService {
38
39
	private $comment;
40
	private $commentMapper;
41
	private $logger;
42
	private $anonymizer;
43
	private $acl;
44
45
	/**
46
	 * CommentService constructor.
47
	 * @param ILogger $logger
48
	 * @param CommentMapper $commentMapper
49
	 * @param Comment $comment
50
	 * @param AnonymizeService $anonymizer
51
	 * @param Acl $acl
52
	 */
53
54
	public function __construct(
55
		ILogger $logger,
56
		CommentMapper $commentMapper,
57
		Comment $comment,
0 ignored issues
show
Unused Code introduced by
The parameter $comment 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

57
		/** @scrutinizer ignore-unused */ Comment $comment,

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...
58
		AnonymizeService $anonymizer,
59
		Acl $acl
60
	) {
61
		$this->commentMapper = $commentMapper;
62
		$this->logger = $logger;
63
		$this->anonymizer = $anonymizer;
64
		$this->acl = $acl;
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
	 * @param integer $pollId
72
	 * @param string $token
73
	 * @return Array
74
	 */
75
	public function list($pollId = 0, $token = '') {
76
77
		if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowView()) {
78
			throw new NotAuthorizedException;
79
		}
80
81
		if (!$this->acl->getAllowSeeUsernames()) {
82
			$this->anonymizer->set($this->acl->getPollId(), $this->acl->getUserId());
83
			return $this->anonymizer->getComments();
84
		} else {
85
			return $this->commentMapper->findByPoll($this->acl->getPollId());
86
		}
87
	}
88
89
	/**
90
	 * Write a new comment to the db and returns the new comment as array
91
	 * @NoAdminRequired
92
	 * @param string $message
93
	 * @param int $pollId
94
	 * @param string $token
95
	 * @return Comment
96
	 */
97
	public function add($pollId = 0, $message, $token = '') {
98
99
		if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowComment()) {
100
			throw new NotAuthorizedException;
101
		}
102
103
		try {
104
			if ($this->acl->getAllowComment()) {
105
				$this->comment = new Comment();
106
				$this->comment->setPollId($this->acl->getPollId());
107
				$this->comment->setUserId($this->acl->getUserId());
108
				$this->comment->setComment($message);
109
				$this->comment->setDt(date('Y-m-d H:i:s'));
110
				$this->comment = $this->commentMapper->insert($this->comment);
111
				return $this->comment;
112
			} else {
113
				throw new NotAuthorizedException;
114
			}
115
116
		} catch (\Exception $e) {
117
			$this->logger->alert('Error writing comment for pollId ' . $pollId . ': '. $e);
118
			throw new NotAuthorizedException($e);
119
		}
120
121
	}
122
123
	/**
124
	 * delete
125
	 * Delete Comment
126
	 * @NoAdminRequired
127
	 * @param int $commentId
128
	 * @param string $token
129
	 * @return Comment
130
	 */
131
	public function delete($commentId, $token = '') {
132
		$this->comment = $this->commentMapper->find($commentId);
133
134
		if ($this->acl->setPollIdOrToken($this->comment->getPollId(), $token)->getUserId() !== $this->acl->getUserId()) {
135
			throw new NotAuthorizedException;
136
		}
137
138
		$this->commentMapper->delete($this->comment);
139
		return $this->comment;
140
141
	}
142
143
}
144