Passed
Pull Request — master (#966)
by René
03:56
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 OCP\Security\ISecureRandom;
34
35
use OCA\Polls\Db\Poll;
36
use OCA\Polls\Db\PollMapper;
37
use OCA\Polls\Db\Option;
38
use OCA\Polls\Db\OptionMapper;
39
use OCA\Polls\Service\LogService;
40
use OCA\Polls\Model\Acl;
41
42
class OptionService  {
43
44
	private $userId;
45
	private $optionMapper;
46
	private $options;
0 ignored issues
show
introduced by
The private property $options is not used, and could be removed.
Loading history...
47
	private $option;
48
	private $groupManager;
49
	private $pollMapper;
50
	private $logger;
51
	private $logService;
52
	private $acl;
53
54
	/**
55
	 * OptionController constructor.
56
	 * @param string $appName
57
	 * @param $userId
58
	 * @param ILogger $logger
59
	 * @param OptionMapper $optionMapper
60
	 * @param IGroupManager $groupManager
61
	 * @param PollMapper $pollMapper
62
	 * @param LogService $logService
63
	 * @param Acl $acl
64
	 */
65
66
	public function __construct(
67
		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

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

216
		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...
217
218
	}
219
}
220