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

CommentService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 7
c 2
b 0
f 1
nc 1
nop 8
dl 0
loc 17
ccs 0
cts 17
cp 0
crap 2
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
			$this->logger->debug('Acl UserId ' . $this->acl->getUserId());
97
			$this->logger->debug('Acl PollId ' . $this->acl->getPollId());
98
			$this->logger->debug('Unauthorized access');
99
			throw new NotAuthorizedException;
100
		}
101
102
		if (!$this->acl->getAllowSeeUsernames()) {
103
			$this->anonymizer->set($this->acl->getPollId(), $this->acl->getUserId());
104
			return $this->anonymizer->getComments();
105
		} else {
106
			return $this->commentMapper->findByPoll($this->acl->getPollId());
107
		}
108
109
110
	}
111
112
	/**
113
	 * Write a new comment to the db and returns the new comment as array
114
	 * @NoAdminRequired
115
	 * @param string $message
116
	 * @param int $pollId
117
	 * @param string $token
118
	 * @return Comment
119
	 */
120
	public function add($message, $pollId = 0, $token = '') {
121
		$this->logger->debug('call commentService->write("' . $message . '", ' .$pollId . ', "' .$token . '")');
122
123
		if (!$this->acl->checkAuthorize($pollId, $token)) {
124
			throw new NotAuthorizedException;
125
		}
126
127
		try {
128
			if ($this->acl->getAllowComment()) {
129
				$this->comment = new Comment();
130
				$this->comment->setPollId($this->acl->getPollId());
131
				$this->comment->setUserId($this->acl->getUserId());
132
				$this->comment->setComment($message);
133
				$this->comment->setDt(date('Y-m-d H:i:s'));
134
				$this->comment = $this->commentMapper->insert($this->comment);
135
				return $this->comment;
136
			} else {
137
				throw new NotAuthorizedException;
138
			}
139
140
		} catch (\Exception $e) {
141
			$this->logger->alert('Error writing comment for pollId ' . $pollId . ': '. $e);
142
			throw new NotAuthorizedException($e);
143
		}
144
	}
145
146
	/**
147
	 * delete
148
	 * Delete Comment
149
	 * @NoAdminRequired
150
	 * @param int $commentId
151
	 * @param string $token
152
	 * @return Comment
153
	 */
154
	public function delete($commentId, $token = '') {
155
		$this->logger->debug('call commentService->delete(' . $commentId . ', "' .$token . '")');
156
157
		$this->comment = $this->commentMapper->find($commentId);
158
		if (!$this->acl->checkAuthorize($this->comment->getPollId(), $token) || $this->comment->getUserId() !== $this->acl->getUserId()) {
159
			throw new NotAuthorizedException;
160
		}
161
		$this->commentMapper->delete($this->comment);
162
163
		return $this->comment;
164
165
	}
166
167
}
168