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

ShareService::__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 1
Metric Value
cc 1
eloc 8
c 1
b 0
f 1
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\ILogger;
29
use OCP\Security\ISecureRandom;
30
31
use OCA\Polls\Exceptions\NotAuthorizedException;
32
use OCA\Polls\Exceptions\InvalidUsername;
33
34
use OCA\Polls\Db\Poll;
35
use OCA\Polls\Db\PollMapper;
36
use OCA\Polls\Db\Share;
37
use OCA\Polls\Db\ShareMapper;
38
use OCA\Polls\Service\MailService;
39
use OCA\Polls\Model\Acl;
40
// TODO: Change to Service
41
use OCA\Polls\Controller\SystemController;
42
43
class ShareService {
44
45
	private $logger;
46
	private $acl;
47
	private $shareMapper;
48
	private $share;
49
	private $userId;
50
51
	private $pollMapper;
52
	private $systemController;
53
	private $mailService;
54
55
	/**
56
	 * ShareController constructor.
57
	 * @param string $appName
58
	 * @param string $userId
59
	 * @param IRequest $request
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Service\IRequest 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...
60
	 * @param ILogger $logger
61
	 * @param ShareMapper $shareMapper
62
	 * @param Share $share
63
	 * @param PollMapper $pollMapper
64
	 * @param SystemController $systemController
65
	 * @param MailService $mailService
66
	 * @param Acl $acl
67
	 */
68
	public function __construct(
69
		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

69
		/** @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...
70
		$userId,
71
		ILogger $logger,
72
		ShareMapper $shareMapper,
73
		Share $share,
74
		PollMapper $pollMapper,
75
		SystemController $systemController,
76
		MailService $mailService,
77
		Acl $acl
78
	) {
79
		$this->logger = $logger;
80
		$this->userId = $userId;
81
		$this->shareMapper = $shareMapper;
82
		$this->share = $share;
83
		$this->pollMapper = $pollMapper;
84
		$this->systemController = $systemController;
85
		$this->mailService = $mailService;
86
		$this->acl = $acl;
87
	}
88
89
	/**
90
	 * get
91
	 * Read all shares of a poll based on the poll id and return list as array
92
	 * @NoAdminRequired
93
	 * @param integer $pollId
94
	 * @return DataResponse
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Service\DataResponse 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...
95
	 */
96
	public function list($pollId) {
97
		if ($this->acl->setPollId($pollId)->getAllowEdit()) {
98
			return $this->shareMapper->findByPoll($pollId);
99
		} else {
100
			throw new NotAuthorizedException;
101
		}
102
	}
103
104
	/**
105
	 * getByToken
106
	 * Get pollId by token
107
	 * @NoAdminRequired
108
	 * @param string $token
109
	 * @return Array
110
	 */
111
	public function get($token) {
112
		$this->share = $this->shareMapper->findByToken($token);
113
		return $this->share;
114
	}
115
116
	/**
117
	 * Write a new share to the db and returns the new share as array
118
	 * @NoAdminRequired
119
	 * @depricated
120
	 * @param int $pollId
121
	 * @param string $share
122
	 * @return Array
123
	 */
124
	 // TODO: Replace with $this->add and separate sending invitations
125
	public function write($pollId, $type, $userId, $userEmail = '') {
126
		$this->acl->setPollId($pollId);
127
		if (!$this->acl->getAllowEdit()) {
128
			throw new NotAuthorizedException;
129
		}
130
131
		$this->share = new Share();
132
		$this->share->setType($type);
133
		$this->share->setPollId($pollId);
134
		$this->share->setUserId($userId);
135
		$this->share->setUserEmail($userEmail);
136
		$this->share->setToken(\OC::$server->getSecureRandom()->generate(
137
			16,
138
			ISecureRandom::CHAR_DIGITS .
139
			ISecureRandom::CHAR_LOWER .
140
			ISecureRandom::CHAR_UPPER
141
		));
142
143
		$this->share = $this->shareMapper->insert($this->share);
144
		$sendResult = $this->mailService->sendInvitationMail($this->share->getToken());
145
146
		return [
147
			'share' => $this->share,
148
			'sendResult' => $sendResult
149
		];
150
	}
151
152
	/**
153
	 * Write a new share to the db and returns the new share as array
154
	 * @NoAdminRequired
155
	 * @param int $pollId
156
	 * @param string $share
157
	 * @return Array
158
	 */
159
	public function add($pollId, $type, $userId, $userEmail = '') {
160
		$this->acl->setPollId($pollId);
161
		if (!$this->acl->getAllowEdit()) {
162
			throw new NotAuthorizedException;
163
		}
164
165
		$this->share = new Share();
166
		$this->share->setType($type);
167
		$this->share->setPollId($pollId);
168
		$this->share->setUserId($userId);
169
		$this->share->setUserEmail($userEmail);
170
		$this->share->setToken(\OC::$server->getSecureRandom()->generate(
171
			16,
172
			ISecureRandom::CHAR_DIGITS .
173
			ISecureRandom::CHAR_LOWER .
174
			ISecureRandom::CHAR_UPPER
175
		));
176
177
		return $this->shareMapper->insert($this->share);
178
179
	}
180
181
	/**
182
	 * createPersonalShare
183
	 * Write a new share to the db and returns the new share as array
184
	 * @NoAdminRequired
185
	 * @param string $token
186
	 * @param string $userName
187
	 * @return Share
188
	 */
189
	public function createPersonalShare($token, $userName) {
190
191
		$publicShare = $this->shareMapper->findByToken($token);
192
193
		// Return of validatePublicUsername is a DataResponse
194
		$checkUsername = $this->systemController->validatePublicUsername($publicShare->getPollId(), $userName, $token);
195
196
		// if status is not 200, return DataResponse from validatePublicUsername
197
		if ($checkUsername->getStatus() !== 200) {
198
			throw new InvalidUsername;
199
		}
200
201
		if ($publicShare->getType() === 'public') {
202
203
			$this->share = new Share();
204
			$this->share->setToken(\OC::$server->getSecureRandom()->generate(
205
				16,
206
				ISecureRandom::CHAR_DIGITS .
207
				ISecureRandom::CHAR_LOWER .
208
				ISecureRandom::CHAR_UPPER
209
			));
210
			$this->share->setType('external');
211
			$this->share->setPollId($publicShare->getPollId());
212
			$this->share->setUserId($userName);
213
			$this->share->setUserEmail('');
214
			$this->share = $this->shareMapper->insert($this->share);
215
			return $this->share;
216
217
		} elseif ($publicShare->getType() === 'email') {
218
219
			$publicShare->setType('external');
220
			$publicShare->setUserId($userName);
221
			$this->shareMapper->update($publicShare);
222
			return new DataResponse($publicShare, Http::STATUS_OK);
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Service\Http 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...
223
224
		} else {
225
			throw new NotAuthorizedException;
226
		}
227
	}
228
229
	/**
230
	 * remove
231
	 * remove share
232
	 * @NoAdminRequired
233
	 * @param string $token
234
	 * @return Share
235
	 */
236
237
	public function remove($token) {
238
		$this->share = $this->shareMapper->findByToken($token);
239
		if ($this->acl->setPollId($this->share->getPollId())->getAllowEdit()) {
240
			$this->shareMapper->delete($this->share);
241
			return $this->share;
242
		} else {
243
			throw new NotAuthorizedException;
244
		}
245
	}
246
}
247