Passed
Pull Request — master (#948)
by René
04:03
created

ShareController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 21
ccs 0
cts 20
cp 0
rs 9.9666
cc 1
nc 1
nop 10
crap 2

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\Controller;
25
26
use Exception;
27
use OCP\AppFramework\Db\DoesNotExistException;
28
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\Security\ISecureRandom;
37
38
use OCA\Polls\Db\Poll;
39
40
use OCA\Polls\Model\Acl;
41
use OCA\Polls\Db\PollMapper;
42
use OCA\Polls\Db\Share;
43
use OCA\Polls\Db\ShareMapper;
44
use OCA\Polls\Service\MailService;
45
// TODO: Change to Service
46
use OCA\Polls\Controller\SystemController;
47
48
class ShareController extends Controller {
49
50
	private $logger;
51
	private $acl;
52
	private $shareMapper;
53
	private $share;
54
	private $userId;
55
	private $pollMapper;
56
	private $systemController;
57
	private $mailService;
58
59
	/**
60
	 * ShareController constructor.
61
	 * @param string $appName
62
	 * @param string $userId
63
	 * @param IRequest $request
64
	 * @param ILogger $logger
65
	 * @param ShareMapper $shareMapper
66
	 * @param PollMapper $pollMapper
67
	 * @param SystemController $systemController
68
	 * @param MailService $mailService
69
	 * @param Acl $acl
70
	 */
71
	public function __construct(
72
		string $appName,
73
		$userId,
74
		IRequest $request,
75
		ILogger $logger,
76
		ShareMapper $shareMapper,
77
		Share $share,
78
		PollMapper $pollMapper,
79
		SystemController $systemController,
80
		MailService $mailService,
81
		Acl $acl
82
	) {
83
		parent::__construct($appName, $request);
84
		$this->logger = $logger;
85
		$this->userId = $userId;
86
		$this->shareMapper = $shareMapper;
87
		$this->share = $share;
88
		$this->pollMapper = $pollMapper;
89
		$this->systemController = $systemController;
90
		$this->mailService = $mailService;
91
		$this->acl = $acl;
92
	}
93
94
	/**
95
	 * get
96
	 * Get share by token
97
	 * @NoAdminRequired
98
	 * @NoCSRFRequired
99
	 * @PublicPage
100
	 * @param string $token
101
	 * @return DataResponse Share
102
	 */
103
	public function get($token) {
104
		try {
105
			$this->share = $this->shareMapper->findByToken($token);
106
			return new DataResponse($this->share, Http::STATUS_OK);
107
108
		} catch (DoesNotExistException $e) {
109
			return new DataResponse(null, Http::STATUS_NOT_FOUND);
110
		}
111
	}
112
113
	/**
114
	 * list
115
	 * Generates array of shares based on $pollId
116
	 * @NoAdminRequired
117
	 * @NoCSRFRequired
118
	 * @param integer $pollId
119
	 * @return DataResponse Array of Share
120
	 */
121
	public function list($pollId) {
122
		if ($this->acl->setPollId($pollId)->getAllowEdit()) {
123
			try {
124
				$shares = $this->shareMapper->findByPoll($pollId);
125
				return new DataResponse((array) $shares, Http::STATUS_OK);
126
127
			} catch (DoesNotExistException $e) {
128
				return new DataResponse($e, Http::STATUS_NOT_FOUND);
129
			}
130
131
		} else {
132
			$this->logger->alert('no access');
133
134
			return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
135
		}
136
137
	}
138
139
	/**
140
	 * add
141
	 * Add a share
142
	 * @NoAdminRequired
143
	 * @NoCSRFRequired
144
	 * @param int $pollId
145
	 * @param Array $share
146
	 * @return DataResponse Array of Share
147
	 */
148
	public function add($pollId, $share) {
149
		$this->acl->setPollId($pollId);
150
		if (!$this->acl->getAllowEdit()) {
151
			return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
152
		}
153
154
		$this->share = new Share();
155
		$this->share->setType($share['type']);
156
		$this->share->setPollId($share['pollId']);
157
		$this->share->setUserId($share['userId']);
158
		$this->share->setUserEmail(isset($share['userEmail']) ? $share['userEmail'] : '');
159
		$this->share->setToken(\OC::$server->getSecureRandom()->generate(
160
			16,
161
			ISecureRandom::CHAR_DIGITS .
162
			ISecureRandom::CHAR_LOWER .
163
			ISecureRandom::CHAR_UPPER
164
		));
165
166
		try {
167
			$this->share = $this->shareMapper->insert($this->share);
168
			$sendResult = $this->mailService->sendInvitationMail($this->share->getToken());
169
170
			return new DataResponse([
171
				'sendResult' => $sendResult,
172
				'shares' => $this->shareMapper->findByPoll($pollId),
173
			], Http::STATUS_OK);
174
175
		} catch (\Exception $e) {
176
			return new DataResponse($e, Http::STATUS_CONFLICT);
177
		}
178
179
	}
180
181
	/**
182
	 * createPersonalShare
183
	 * Create a new personal share from public share
184
	 * or update email share
185
	 * @NoAdminRequired
186
	 * @PublicPage
187
	 * @NoCSRFRequired
188
	 * @param string $token
189
	 * @param string $userName
190
	 * @param string $userEmail
191
	 * @return DataResponse Share
192
	 */
193
	public function createPersonalShare($token, $userName, $userEmail = '') {
194
195
		try {
196
			$this->share = $this->shareMapper->findByToken($token);
197
198
			// Return of validatePublicUsername is a DataResponse
199
			$checkUsername = $this->systemController->validatePublicUsername($this->share->getPollId(), $userName, $token);
200
201
			// if status is not 200, return DataResponse from validatePublicUsername
202
			if ($checkUsername->getStatus() !== 200) {
203
				return $checkUsername;
204
			}
205
206
			if ($this->share->getType() === 'email') {
207
208
				$this->share->setType('external');
209
				$this->share->setUserId($userName);
210
				$this->shareMapper->update($this->share);
211
212
			} elseif ($this->share->getType() === 'public') {
213
214
				$pollId = $this->share->getPollId();
215
				$this->share = new Share();
216
				$this->share->setToken(\OC::$server->getSecureRandom()->generate(
217
					16,
218
					ISecureRandom::CHAR_DIGITS .
219
					ISecureRandom::CHAR_LOWER .
220
					ISecureRandom::CHAR_UPPER
221
				));
222
				$this->share->setType('external');
223
				$this->share->setPollId($pollId);
224
				$this->share->setUserId($userName);
225
				$this->share->setUserEmail($userEmail);
226
				$this->share = $this->shareMapper->insert($this->share);
227
228
			} else {
229
				return new DataResponse([
230
					'message'=> 'Wrong share type: ' . $this->share->getType()
231
				], Http::STATUS_FORBIDDEN);
232
			}
233
234
			return new DataResponse($this->share, Http::STATUS_OK);
235
236
		} catch (DoesNotExistException $e) {
237
			return new DataResponse($e, Http::STATUS_FORBIDDEN);
238
		}
239
	}
240
241
	/**
242
	 * remove
243
	 * remove share
244
	 * @NoAdminRequired
245
	 * @NoCSRFRequired
246
	 * @param Array $share
247
	 * @return DataResponse
248
	 */
249
250
	public function remove($share) {
251
		try {
252
			if ($this->acl->setPollId($share['pollId'])->getAllowEdit()) {
253
				$this->shareMapper->remove($share['id']);
254
255
				return new DataResponse(array(
256
					'shares' => $this->shareMapper->findByPoll($share['pollId']),
257
				), Http::STATUS_OK);
258
			} else {
259
				return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
260
			}
261
262
		} catch (Exception $e) {
263
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
264
		}
265
	}
266
}
267