Completed
Pull Request — master (#778)
by René
04:23
created

ShareController::__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
nc 1
nop 9
dl 0
loc 19
ccs 0
cts 19
cp 0
crap 2
rs 10
c 1
b 0
f 0

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 $mapper;
53
	private $userId;
54
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 $mapper
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 $mapper,
77
		PollMapper $pollMapper,
78
		SystemController $systemController,
79
		MailService $mailService,
80
		Acl $acl
81
	) {
82
		parent::__construct($appName, $request);
83
		$this->logger = $logger;
84
		$this->userId = $userId;
85
		$this->mapper = $mapper;
86
		$this->pollMapper = $pollMapper;
87
		$this->systemController = $systemController;
88
		$this->mailService = $mailService;
89
		$this->acl = $acl;
90
	}
91
92
	/**
93
	 * getByToken
94
	 * Get pollId by token
95
	 * @NoAdminRequired
96
	 * @NoCSRFRequired
97
	 * @PublicPage
98
	 * @param string $token
99
	 * @return DataResponse
100
	 */
101
	public function get($token) {
102
		try {
103
			$share = $this->mapper->findByToken($token);
104
			return new DataResponse($share, Http::STATUS_OK);
105
106
		} catch (DoesNotExistException $e) {
107
			return new DataResponse(null, Http::STATUS_NOT_FOUND);
108
		}
109
	}
110
111
	/**
112
	 * get
113
	 * Read all shares of a poll based on the poll id and return list as array
114
	 * @NoAdminRequired
115
	 * @param integer $pollId
116
	 * @return DataResponse
117
	 */
118
	public function getShares($pollId) {
119
		if ($this->acl->setPollId($pollId)->getAllowEdit()) {
120
			try {
121
				$shares = $this->mapper->findByPoll($pollId);
122
				return new DataResponse((array) $shares, Http::STATUS_OK);
123
124
			} catch (DoesNotExistException $e) {
125
				return new DataResponse($e, Http::STATUS_NOT_FOUND);
126
			}
127
128
		} else {
129
			$this->logger->alert('no access');
130
131
			return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
132
		}
133
134
	}
135
136
	/**
137
	 * write
138
	 * Write a new share to the db and returns the new share as array
139
	 * @NoAdminRequired
140
	 * @NoCSRFRequired
141
	 * @PublicPage
142
	 * @param int $pollId
143
	 * @param string $message
144
	 * @return DataResponse
145
	 */
146
	public function write($pollId, $share) {
147
		$this->acl->setPollId($pollId);
148
		if (!$this->acl->getAllowEdit()) {
149
			return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
150
		}
151
152
		$newShare = new Share();
153
		$newShare->setType($share['type']);
154
		$newShare->setPollId($share['pollId']);
155
		$newShare->setUserId($share['userId']);
156
		$newShare->setUserEmail(isset($share['userEmail']) ? $share['userEmail'] : '');
157
		$newShare->setToken(\OC::$server->getSecureRandom()->generate(
158
			16,
159
			ISecureRandom::CHAR_DIGITS .
160
			ISecureRandom::CHAR_LOWER .
161
			ISecureRandom::CHAR_UPPER
162
		));
163
164
		try {
165
			$newShare = $this->mapper->insert($newShare);
166
			// $this->logger->debug('Share inserted, sending out invitation mail now.');
167
			$sendResult = $this->mailService->sendInvitationMail($newShare->getToken());
168
			// $this->logger->debug('Sending result ' . json_encode($sendResult));
169
170
			return new DataResponse([
171
				'share' => $newShare,
172
				'sendResult' => $sendResult
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
	 * Write a new share to the db and returns the new share as array
184
	 * @NoAdminRequired
185
	 * @NoCSRFRequired
186
	 * @PublicPage
187
	 * @param int $pollId
188
	 * @param string $message
189
	 * @return DataResponse
190
	 */
191
	public function createPersonalShare($token, $userName) {
192
193
		try {
194
			$publicShare = $this->mapper->findByToken($token);
195
			if (!$this->systemController->validatePublicUsername($publicShare->getPollId(), $userName)) {
196
				return new DataResponse(['message' => 'invalid userName'], Http::STATUS_CONFLICT);
197
			}
198
199
			if ($publicShare->getType() === 'public') {
200
201
				$userShare = new Share();
202
				$userShare->setToken(\OC::$server->getSecureRandom()->generate(
203
					16,
204
					ISecureRandom::CHAR_DIGITS .
205
					ISecureRandom::CHAR_LOWER .
206
					ISecureRandom::CHAR_UPPER
207
				));
208
				$userShare->setType('external');
209
				$userShare->setPollId($publicShare->getPollId());
210
				$userShare->setUserId($userName);
211
				$userShare->setUserEmail('');
212
				$this->logger->debug('Create share: '. json_encode($userShare));
213
				$userShare = $this->mapper->insert($userShare);
214
				return new DataResponse($userShare, Http::STATUS_OK);
215
216
			} else {
217
				return new DataResponse(['message'=> 'Wrong share type: ' . $userShare->getType()], Http::STATUS_FORBIDDEN);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $userShare seems to be never defined.
Loading history...
218
			}
219
220
		} catch (DoesNotExistException $e) {
221
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
222
		}
223
	}
224
225
226
	public function remove($share) {
227
		try {
228
			if ($this->acl->setPollId($share['pollId'])->getAllowEdit()) {
229
				$this->mapper->remove($share['id']);
230
231
				return new DataResponse(array(
232
					'action' => 'deleted',
233
					'shareId' => $share['id']
234
				), Http::STATUS_OK);
235
			} else {
236
				return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
237
			}
238
239
		} catch (Exception $e) {
240
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
241
		}
242
	}
243
}
244