ShareController::resolveGroup()   B
last analyzed

Complexity

Conditions 7
Paths 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 25
ccs 0
cts 15
cp 0
rs 8.8333
c 0
b 0
f 0
cc 7
nc 1
nop 1
crap 56
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 OCA\Polls\Exceptions\ShareAlreadyExistsException;
27
28
29
use OCP\IRequest;
30
use OCP\AppFramework\Controller;
31
use OCP\AppFramework\Http\DataResponse;
32
33
use OCA\Polls\Db\Share;
34
use OCA\Polls\Service\MailService;
35
use OCA\Polls\Service\ShareService;
36
use OCA\Polls\Service\SystemService;
37
use OCA\Polls\Model\Circle;
38
use OCA\Polls\Model\ContactGroup;
39
40
class ShareController extends Controller {
41
	use ResponseHandle;
42
43
	/** @var MailService */
44
	private $mailService;
45
46
	/** @var ShareService */
47
	private $shareService;
48
49
	/** @var SystemService */
50
	private $systemService;
51
52
	public function __construct(
53
		string $appName,
54
		IRequest $request,
55
		MailService $mailService,
56
		ShareService $shareService,
57
		SystemService $systemService
58
	) {
59
		parent::__construct($appName, $request);
60
		$this->mailService = $mailService;
61
		$this->shareService = $shareService;
62
		$this->systemService = $systemService;
63
	}
64
65
	/**
66
	 * List shares
67
	 *
68
	 * @NoAdminRequired
69
	 *
70
	 * @return DataResponse
71
	 */
72
	public function list(int $pollId): DataResponse {
73
		return $this->response(function () use ($pollId): array {
74
			return ['shares' => $this->shareService->list($pollId)];
75
		});
76
	}
77
78
	/**
79
	 * Get share
80
	 *
81
	 * @NoAdminRequired
82
	 *
83
	 * @return DataResponse
84
	 */
85
	public function get(string $token): DataResponse {
86
		return $this->response(function () use ($token): array {
87
			return ['share' => $this->shareService->get($token, true)];
88
		});
89
	}
90
91
	/**
92
	 * Add share
93
	 * @NoAdminRequired
94
	 */
95
	public function add(int $pollId, string $type, string $userId = ''): DataResponse {
96
		return $this->responseCreate(function () use ($pollId, $type, $userId) {
97
			return ['share' => $this->shareService->add($pollId, $type, $userId)];
98
		});
99
	}
100
101
	/**
102
	 * Set email address
103
	 * @NoAdminRequired
104
	 */
105
	public function setEmailAddress(string $token, string $emailAddress = ''): DataResponse {
106
		return $this->response(function () use ($token, $emailAddress) {
107
			return ['share' => $this->shareService->setEmailAddress($token, $emailAddress)];
108
		});
109
	}
110
111
	/**
112
	 * Create a personal share from a public share
113
	 * or update an email share with the username
114
	 * @NoAdminRequired
115
	 */
116
	public function personal(string $token, string $userName, string $emailAddress = ''): DataResponse {
117
		return $this->responseCreate(function () use ($token, $userName, $emailAddress) {
118
			return ['share' => $this->shareService->personal($token, $userName, $emailAddress)];
119
		});
120
	}
121
122
	/**
123
	 * Delete share
124
	 * @NoAdminRequired
125
	 */
126
127
	public function delete(string $token): DataResponse {
128
		return $this->responseDeleteTolerant(function () use ($token) {
129
			return ['share' => $this->shareService->delete($token)];
130
		});
131
	}
132
133
	/**
134
	 * Sent invitation mails for a share
135
	 * Additionally send notification via notifications
136
	 * @NoAdminRequired
137
	 */
138
	public function sendInvitation(string $token): DataResponse {
139
		return $this->response(function () use ($token) {
140
			return [
141
				'share' => $this->shareService->get($token),
142
				'sentResult' => $this->shareService->sendInvitation($token),
143
			];
144
		});
145
	}
146
147
	/**
148
	 * resolve contact group to individual shares
149
	 * @NoAdminRequired
150
	 */
151
	public function resolveGroup(string $token): DataResponse {
152
		return $this->response(function () use ($token) {
153
			$shares = [];
154
			$share = $this->shareService->get($token);
155
			if ($share->getType() === Share::TYPE_CIRCLE) {
156
				foreach ((new Circle($share->getUserId()))->getMembers() as $member) {
157
					try {
158
						$newShare = $this->shareService->add($share->getPollId(), $member->getType(), $member->getId());
159
						$shares[] = $newShare;
160
					} catch (ShareAlreadyExistsException $e) {
161
						continue;
162
					}
163
				}
164
			} elseif ($share->getType() === Share::TYPE_CONTACTGROUP) {
165
				foreach ((new ContactGroup($share->getUserId()))->getMembers() as $contact) {
166
					try {
167
						$newShare = $this->shareService->add($share->getPollId(), Share::TYPE_CONTACT, $contact->getId());
168
						$shares[] = $newShare;
169
					} catch (ShareAlreadyExistsException $e) {
170
						continue;
171
					}
172
				}
173
			}
174
			$this->shareService->delete($token);
175
			return ['shares' => $shares];
176
		});
177
	}
178
}
179