Passed
Pull Request — master (#1038)
by René
03:48
created

ShareController   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 23
eloc 56
dl 0
loc 168
ccs 0
cts 55
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 7 3
A __construct() 0 11 1
A setEmailAddress() 0 9 4
A get() 0 7 3
A sendInvitation() 0 7 2
A resolveContactGroup() 0 11 3
A personal() 0 10 4
A delete() 0 7 3
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
use OCA\Polls\Exceptions\NotAuthorizedException;
29
use OCA\Polls\Exceptions\InvalidUsernameException;
30
use OCA\Polls\Exceptions\InvalidShareType;
31
32
33
use OCP\IRequest;
34
use OCP\AppFramework\Controller;
35
use OCP\AppFramework\Http;
36
use OCP\AppFramework\Http\DataResponse;
37
38
use OCA\Polls\Service\ShareService;
39
use OCA\Polls\Service\MailService;
40
use OCA\Polls\Service\SystemService;
41
42
class ShareController extends Controller {
43
44
	/** @var ShareService */
45
	private $shareService;
46
47
	/** @var MailService */
48
	private $mailService;
49
50
	/** @var SystemService */
51
	private $systemService;
52
53
	/**
54
	 * ShareController constructor.
55
	 * @param string $appName
56
	 * @param IRequest $request
57
	 * @param MailService $mailService
58
	 * @param ShareService $shareService
59
	 * @param SystemService $systemService
60
	 */
61
	public function __construct(
62
		string $appName,
63
		IRequest $request,
64
		MailService $mailService,
65
		ShareService $shareService,
66
		SystemService $systemService
67
	) {
68
		parent::__construct($appName, $request);
69
		$this->shareService = $shareService;
70
		$this->mailService = $mailService;
71
		$this->systemService = $systemService;
72
	}
73
74
	/**
75
	 * Add share
76
	 * @NoAdminRequired
77
	 * @param int $pollId
78
	 * @param int $pollId
79
	 * @param string $type
80
	 * @param string $userId
81
	 * @param string $userEmail
82
	 * @return DataResponse
83
	 */
84
	public function add($pollId, $type, $userId = '', $userEmail = '') {
85
		try {
86
			return new DataResponse(['share' => $this->shareService->add($pollId, $type, $userId, $userEmail)], Http::STATUS_CREATED);
87
		} catch (NotAuthorizedException $e) {
88
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
89
		} catch (\Exception $e) {
90
			return new DataResponse($e, Http::STATUS_CONFLICT);
91
		}
92
	}
93
94
	/**
95
	 * Get share
96
	 * @NoAdminRequired
97
	 * @param int $pollId
98
	 * @param int $pollId
99
	 * @param string $type
100
	 * @param string $userId
101
	 * @param string $userEmail
102
	 * @return DataResponse
103
	 */
104
	public function get($token) {
105
		try {
106
			return new DataResponse(['share' => $this->shareService->get($token)], Http::STATUS_CREATED);
107
		} catch (NotAuthorizedException $e) {
108
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
109
		} catch (\Exception $e) {
110
			return new DataResponse($e, Http::STATUS_CONFLICT);
111
		}
112
	}
113
114
	/**
115
	 * Set email address
116
	 * @NoAdminRequired
117
	 * @PublicPage
118
	 * @param int $pollId
119
	 * @param int $pollId
120
	 * @param string $type
121
	 * @param string $userId
122
	 * @param string $userEmail
123
	 * @return DataResponse
124
	 */
125
	public function setEmailAddress($token, $userEmail) {
126
		try {
127
			return new DataResponse(['share' => $this->shareService->setEmailAddress($token, $userEmail)], Http::STATUS_OK);
128
		} catch (NotAuthorizedException $e) {
129
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
130
		} catch (InvalidShareType $e) {
131
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
132
		} catch (\Exception $e) {
133
			return new DataResponse($e, Http::STATUS_CONFLICT);
134
		}
135
	}
136
137
	/**
138
	 * Create a personal share from a public share
139
	 * or update an email share with the username
140
	 * @NoAdminRequired
141
	 * @PublicPage
142
	 * @param string $token
143
	 * @param string $userName
144
	 * @return DataResponse
145
	 */
146
	public function personal($token, $userName, $emailAddress = '') {
147
		try {
148
			return new DataResponse($this->shareService->personal($token, $userName, $emailAddress), Http::STATUS_CREATED);
149
		} catch (NotAuthorizedException $e) {
150
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
151
		} catch (InvalidUsernameException $e) {
152
			return new DataResponse(['error' => $userName . ' is not valid'], Http::STATUS_CONFLICT);
153
		} catch (DoesNotExistException $e) {
154
			// return forbidden in all not catched error cases
155
			return new DataResponse($e, Http::STATUS_FORBIDDEN);
156
		}
157
	}
158
159
	/**
160
	 * Delete share
161
	 * @NoAdminRequired
162
	 * @param string $token
163
	 * @return DataResponse
164
	 */
165
166
	public function delete($token) {
167
		try {
168
			return new DataResponse($this->shareService->delete($token), Http::STATUS_OK);
169
		} catch (NotAuthorizedException $e) {
170
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
171
		} catch (Exception $e) {
172
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
173
		}
174
	}
175
176
	/**
177
	 * Sent invitation mails for a share
178
	 * @NoAdminRequired
179
	 * @PublicPage
180
	 * @param string $token
181
	 * @return DataResponse
182
	 */
183
	public function sendInvitation($token) {
184
		try {
185
			$sentResult = $this->mailService->sendInvitationMail($token);
186
			$share = $this->shareService->get($token);
187
			return new DataResponse(['share' => $share, 'sentResult' => $sentResult], Http::STATUS_OK);
188
		} catch (Exception $e) {
189
			return new DataResponse(['error' => $e], Http::STATUS_CONFLICT);
190
		}
191
	}
192
193
	/**
194
	 * resolve Contact groupe to individual shares
195
	 * @NoAdminRequired
196
	 * @param string $token
197
	 * @return DataResponse
198
	 */
199
	public function resolveContactGroup($token) {
200
		$shares = [];
201
		try {
202
			$share = $this->shareService->get($token);
203
			foreach ($this->systemService->getContactsGroupMembers($share->getUserId()) as $member) {
204
				$shares[] = $this->shareService->add($share->getpollId(), 'contact', $member['user'], $member['emailAddress']);
205
			}
206
207
			return new DataResponse(['shares' => $shares], Http::STATUS_OK);
208
		} catch (Exception $e) {
209
			return new DataResponse(['error' => $e], Http::STATUS_CONFLICT);
210
		}
211
	}
212
}
213