CommentService   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
eloc 44
c 0
b 0
f 0
dl 0
loc 89
ccs 0
cts 43
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A list() 0 12 3
A __construct() 0 12 1
A add() 0 15 2
A delete() 0 16 3
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 OCA\Polls\Db\Comment;
27
use OCA\Polls\Db\CommentMapper;
28
use OCA\Polls\Db\Watch;
29
use OCA\Polls\Model\Acl;
30
31
class CommentService {
32
33
	/** @var Acl */
34
	private $acl;
35
36
	/** @var AnonymizeService */
37
	private $anonymizer;
38
39
	/** @var CommentMapper */
40
	private $commentMapper;
41
42
	/** @var Comment */
43
	private $comment;
44
45
	/** @var WatchService */
46
	private $watchService;
47
48
	public function __construct(
49
		Acl $acl,
50
		AnonymizeService $anonymizer,
51
		CommentMapper $commentMapper,
52
		Comment $comment,
53
		WatchService $watchService
54
	) {
55
		$this->acl = $acl;
56
		$this->anonymizer = $anonymizer;
57
		$this->commentMapper = $commentMapper;
58
		$this->comment = $comment;
59
		$this->watchService = $watchService;
60
	}
61
62
	/**
63
	 * Get comments
64
	 * Read all comments of a poll based on the poll id and return list as array
65
	 */
66
	public function list(?int $pollId = 0, string $token = ''): array {
67
		if ($token) {
68
			$this->acl->setToken($token);
69
		} else {
70
			$this->acl->setPollId($pollId)->request(Acl::PERMISSION_VIEW);
71
		}
72
73
		if ($this->acl->isAllowed(Acl::PERMISSION_SEE_USERNAMES)) {
74
			return $this->commentMapper->findByPoll($this->acl->getPollId());
75
		} else {
76
			$this->anonymizer->set($this->acl->getPollId(), $this->acl->getUserId());
77
			return $this->anonymizer->getComments();
78
		}
79
	}
80
81
	/**
82
	 * Add comment
83
	 */
84
	public function add(int $pollId = 0, ?string $token = '', string $message): Comment {
85
		if ($token) {
86
			$this->acl->setToken($token)->request(Acl::PERMISSION_COMMENT);
87
		} else {
88
			$this->acl->setPollId($pollId)->request(Acl::PERMISSION_COMMENT);
89
		}
90
		$this->comment = new Comment();
91
		$this->comment->setPollId($this->acl->getPollId());
92
		$this->comment->setUserId($this->acl->getUserId());
93
		$this->comment->setComment($message);
94
		$this->comment->setDt(date('Y-m-d H:i:s'));
95
		$this->comment->setTimestamp(time());
96
		$this->comment = $this->commentMapper->insert($this->comment);
97
		$this->watchService->writeUpdate($this->comment->getPollId(), Watch::OBJECT_COMMENTS);
98
		return $this->comment;
99
	}
100
101
	/**
102
	 * Delete comment
103
	 */
104
	public function delete(int $commentId, string $token = ''): Comment {
105
		$this->comment = $this->commentMapper->find($commentId);
106
107
		if ($token) {
108
			$this->acl->setToken($token);
109
		} else {
110
			$this->acl->setPollId($this->comment->getPollId());
111
		}
112
113
		if (!$this->acl->getIsOwner()) {
114
			$this->acl->validateUserId($this->comment->getUserId());
115
		}
116
117
		$this->commentMapper->delete($this->comment);
118
		$this->watchService->writeUpdate($this->comment->getPollId(), Watch::OBJECT_COMMENTS);
119
		return $this->comment;
120
	}
121
}
122