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

OptionService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 9
dl 0
loc 19
ccs 0
cts 19
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
28
use OCP\IGroupManager;
29
use OCP\ILogger;
30
31
use OCA\Polls\Exceptions\NotAuthorizedException;
32
33
use OCA\Polls\Db\Poll;
34
use OCA\Polls\Db\PollMapper;
35
use OCA\Polls\Db\Option;
36
use OCA\Polls\Db\OptionMapper;
37
use OCA\Polls\Service\LogService;
38
use OCA\Polls\Model\Acl;
39
40
class OptionService  {
41
42
	private $userId;
43
	private $optionMapper;
44
	private $options;
0 ignored issues
show
introduced by
The private property $options is not used, and could be removed.
Loading history...
45
	private $option;
46
	private $groupManager;
47
	private $pollMapper;
48
	private $logger;
49
	private $logService;
50
	private $acl;
51
52
	/**
53
	 * OptionController constructor.
54
	 * @param string $appName
55
	 * @param $userId
56
	 * @param ILogger $logger
57
	 * @param OptionMapper $optionMapper
58
	 * @param IGroupManager $groupManager
59
	 * @param PollMapper $pollMapper
60
	 * @param LogService $logService
61
	 * @param Acl $acl
62
	 */
63
64
	public function __construct(
65
		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

65
		/** @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...
66
		$userId,
67
		OptionMapper $optionMapper,
68
		Option $option,
69
		IGroupManager $groupManager,
70
		PollMapper $pollMapper,
71
		ILogger $logger,
72
		LogService $logService,
73
		Acl $acl
74
	) {
75
		$this->userId = $userId;
76
		$this->optionMapper = $optionMapper;
77
		$this->option = $option;
78
		$this->groupManager = $groupManager;
79
		$this->pollMapper = $pollMapper;
80
		$this->logger = $logger;
81
		$this->logService = $logService;
82
		$this->acl = $acl;
83
	}
84
85
	/**
86
	 * Set properties from option array
87
	 * @NoAdminRequired
88
	 * @param Array $option
89
	 */
90
	private function set($option) {
91
92
		$this->option->setPollId($option['pollId']);
93
		$this->option->setPollOptionText(trim(htmlspecialchars($option['pollOptionText'])));
94
		$this->option->setTimestamp($option['timestamp']);
95
96
		if ($option['timestamp']) {
97
			$this->option->setOrder($option['timestamp']);
98
		} else {
99
			$this->option->setOrder($option['order']);
100
		}
101
102
		if ($option['confirmed']) {
103
			// do not update confirmation date, if option is already confirmed
104
			if (!$this->option->getConfirmed()) {
105
				$this->option->setConfirmed(time());
106
			}
107
		} else {
108
			$this->option->setConfirmed(0);
109
		}
110
	}
111
112
	/**
113
	 * Get all options of given poll
114
	 * @NoAdminRequired
115
	 * @param integer $pollId
116
	 * @param string $token
117
	 * @return array Array of Option objects
118
	 */
119
	public function list($pollId = 0, $token = '') {
120
		$this->logger->debug('call optionService->list(' . $pollId . ', '. $token . ')');
121
122
		if (!$this->acl->checkAuthorize($pollId, $token)) {
123
			throw new NotAuthorizedException;
124
		}
125
126
		return $this->optionMapper->findByPoll($pollId);
127
	}
128
129
130
	/**
131
	 * Add a new Option to poll
132
	 * @NoAdminRequired
133
	 * @param Array $option
134
	 * @return Option
135
	 */
136
	public function add($option) {
137
		$this->logger->debug('call optionService->add(' . json_encode($option) . ')');
138
139
		if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) {
140
			throw new NotAuthorizedException;
141
		}
142
		$this->option = new Option();
143
		$this->set($option);
144
		$this->optionMapper->insert($this->option);
145
		$this->logService->setLog($option['pollId'], 'addOption');
146
		return $this->option;
147
	}
148
149
	/**
150
	 * Remove a single option
151
	 * @NoAdminRequired
152
	 * @param Option $option
153
	 * @return array Array of Option objects
154
	 */
155
	public function delete($optionId) {
156
		$this->logger->debug('call optionService->delete(' . json_encode($optionId) . ')');
157
158
		$this->option = $this->optionMapper->find($optionId);
159
		if (!$this->acl->setPollId($this->option->getPollId())->getAllowEdit()) {
160
			throw new NotAuthorizedException;
161
		}
162
163
		$this->optionMapper->delete($this->option);
164
165
		return $this->option;
166
	}
167
168
	/**
169
	 * Update poll option
170
	 * @NoAdminRequired
171
	 * @param array $option
172
	 * @return Option
173
	 */
174
	public function update($option) {
175
		$this->logger->debug('call optionService->update(' . json_encode($option) . ')');
176
177
		if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) {
178
			throw new NotAuthorizedException;
179
		}
180
181
		try {
182
			$this->option = $this->optionMapper->find($option['id']);
183
			$this->set($option);
184
			$this->optionMapper->update($this->option);
185
			$this->logService->setLog($option['pollId'], 'updateOption');
186
			return $this->option;
187
		} catch (Exception $e) {
188
			return new DoesNotExistException($e);
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Service\DoesNotExistException 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...
189
		}
190
	}
191
192
	/**
193
	 * Set order by order of the given array
194
	 * @NoAdminRequired
195
	 * @param array $options
196
	 * @return array Array of Option objects
197
	 */
198
	public function reorder($pollId, $options) {
199
		$this->logger->debug('call optionService->reorder(' . $pollId . ', ' . json_encode($options) . ')');
200
201
		if (!$this->acl->setPollId($pollId)->getAllowEdit()) {
202
			throw new NotAuthorizedException;
203
		}
204
205
		$i = 0;
206
		foreach ($options as $option) {
207
			$this->option = $this->optionMapper->find($option['id']);
208
			if ($pollId === intval($this->option->getPollId())) {
209
				$this->option->setOrder(++$i);
210
				$this->optionMapper->update($this->option);
211
			}
212
		}
213
214
		return $this->get($pollId);
0 ignored issues
show
Bug introduced by
The method get() does not exist on OCA\Polls\Service\OptionService. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

214
		return $this->/** @scrutinizer ignore-call */ get($pollId);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
215
216
	}
217
218
	/**
219
	 * Set order by order of the given array
220
	 * @NoAdminRequired
221
	 * @param array $options
222
	 * @return array Array of Option objects
223
	 */
224
	public function clone($fromPollId, $toPollId) {
225
226
		foreach ($this->optionMapper->findByPoll($fromPollId) as $option) {
227
			$option->setPollId($toPollId);
228
			$this->optionMapper->insert($option);
229
		}
230
231
		return $this->optionMapper->findByPoll($toPollId);
232
233
	}
234
}
235