Completed
Pull Request — master (#966)
by René
04:37
created

CommentService::add()   A

Complexity

Conditions 4
Paths 10

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 4
eloc 16
c 2
b 0
f 1
nc 10
nop 3
dl 0
loc 22
ccs 0
cts 19
cp 0
crap 20
rs 9.7333
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,
58
		AnonymizeService $anonymizer,
59
		Acl $acl
60
	) {
61
		$this->commentMapper = $commentMapper;
62
		$this->comment = $comment;
63
		$this->logger = $logger;
64
		$this->anonymizer = $anonymizer;
65
		$this->acl = $acl;
66
	}
67
68
	/**
69
	 * get
70
	 * Read all comments of a poll based on the poll id and return list as array
71
	 * @NoAdminRequired
72
	 * @param integer $pollId
73
	 * @param string $token
74
	 * @return Array
75
	 */
76
	public function list($pollId = 0, $token = '') {
77
78
		if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowView()) {
79
			throw new NotAuthorizedException;
80
		}
81
82
		if (!$this->acl->getAllowSeeUsernames()) {
83
			$this->anonymizer->set($this->acl->getPollId(), $this->acl->getUserId());
84
			return $this->anonymizer->getComments();
85
		} else {
86
			return $this->commentMapper->findByPoll($this->acl->getPollId());
87
		}
88
	}
89
90
	/**
91
	 * Write a new comment to the db and returns the new comment as array
92
	 * @NoAdminRequired
93
	 * @param string $message
94
	 * @param int $pollId
95
	 * @param string $token
96
	 * @return Comment
97
	 */
98
	public function add($pollId = 0, $message, $token = '') {
99
100
		if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowComment()) {
101
			throw new NotAuthorizedException;
102
		}
103
104
		try {
105
			if ($this->acl->getAllowComment()) {
106
				$this->comment = new Comment();
107
				$this->comment->setPollId($this->acl->getPollId());
108
				$this->comment->setUserId($this->acl->getUserId());
109
				$this->comment->setComment($message);
110
				$this->comment->setDt(date('Y-m-d H:i:s'));
111
				$this->comment = $this->commentMapper->insert($this->comment);
112
				return $this->comment;
113
			} else {
114
				throw new NotAuthorizedException;
115
			}
116
117
		} catch (\Exception $e) {
118
			$this->logger->alert('Error writing comment for pollId ' . $pollId . ': '. $e);
119
			throw new NotAuthorizedException($e);
120
		}
121
122
	}
123
124
	/**
125
	 * delete
126
	 * Delete Comment
127
	 * @NoAdminRequired
128
	 * @param int $commentId
129
	 * @param string $token
130
	 * @return Comment
131
	 */
132
	public function delete($commentId, $token = '') {
133
		$this->comment = $this->commentMapper->find($commentId);
134
135
		if ($this->acl->setPollIdOrToken($this->comment->getPollId(), $token)->getUserId() !== $this->acl->getUserId()) {
136
			throw new NotAuthorizedException;
137
		}
138
139
		$this->commentMapper->delete($this->comment);
140
		return $this->comment;
141
142
	}
143
144
}
145