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

CommentService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 9
dl 0
loc 18
ccs 0
cts 18
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
use OCA\Polls\Exceptions\NotAuthorizedException;
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Exceptions\NotAuthorizedException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
30
use OCP\IRequest;
31
use OCP\ILogger;
32
use OCP\AppFramework\Controller;
33
use OCP\AppFramework\Http;
34
use OCP\AppFramework\Http\DataResponse;
35
36
use OCP\IGroupManager;
37
38
use OCA\Polls\Db\Poll;
39
use OCA\Polls\Db\PollMapper;
40
use OCA\Polls\Db\Comment;
41
use OCA\Polls\Db\CommentMapper;
42
use OCA\Polls\Service\AnonymizeService;
43
use OCA\Polls\Model\Acl;
44
45
46
47
class CommentService {
48
49
	private $userId;
50
	private $commentMapper;
51
	private $logger;
52
53
	private $groupManager;
54
	private $pollMapper;
55
	private $anonymizer;
56
	private $acl;
57
	private $comment;
58
59
	/**
60
	 * CommentController constructor.
61
	 * @param string $appName
62
	 * @param $UserId
63
	 * @param CommentMapper $commentMapper
64
	 * @param IGroupManager $groupManager
65
	 * @param PollMapper $pollMapper
66
	 * @param AnonymizeService $anonymizer
67
	 * @param Acl $acl
68
	 */
69
70
	public function __construct(
71
		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

71
		/** @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...
72
		$userId,
73
		IRequest $request,
0 ignored issues
show
Unused Code introduced by
The parameter $request 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

73
		/** @scrutinizer ignore-unused */ IRequest $request,

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