Passed
Pull Request — master (#1272)
by René
03:46
created

PollController::deletePermanently()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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\Exception;
27
28
use OCP\IRequest;
29
use OCP\AppFramework\Controller;
30
use OCP\AppFramework\Http\DataResponse;
31
32
use OCA\Polls\DB\Share;
33
use OCA\Polls\DB\Poll;
34
use OCA\Polls\Service\PollService;
35
use OCA\Polls\Service\CommentService;
36
use OCA\Polls\Service\OptionService;
37
use OCA\Polls\Service\ShareService;
38
use OCA\Polls\Service\VoteService;
39
use OCA\Polls\Model\Acl;
40
41
class PollController extends Controller {
42
43
	/** @var Acl */
44
	private $acl;
45
46
	/** @var CommentService */
47
	private $commentService;
48
49
	/** @var OptionService */
50
	private $optionService;
51
52
	/** @var PollService */
53
	private $pollService;
54
55
	/** @var Poll */
56
	private $poll;
57
58
	/** @var ShareService */
59
	private $shareService;
60
61
	/** @var Share */
62
	private $share;
63
64
	/** @var VoteService */
65
	private $voteService;
66
67
	use ResponseHandle;
68
69
	/**
70
	 * PollController constructor.
71
	 * @param string $appName
72
	 * @param IRequest $request
73
	 * @param Acl $acl
74
	 * @param CommentService $commentService
75
	 * @param OptionService $optionService
76
	 * @param PollService $pollService
77
	 * @param Poll $poll
78
	 * @param ShareService $shareService
79
	 * @param Share $share
80
	 * @param VoteService $voteService
81
	 */
82
83
	public function __construct(
84
		string $appName,
85
		IRequest $request,
86
		Acl $acl,
87
		CommentService $commentService,
88
		OptionService $optionService,
89
		PollService $pollService,
90
		Poll $poll,
91
		ShareService $shareService,
92
		Share $share,
93
		VoteService $voteService
94
	) {
95
		parent::__construct($appName, $request);
96
		$this->acl = $acl;
97
		$this->commentService = $commentService;
98
		$this->optionService = $optionService;
99
		$this->pollService = $pollService;
100
		$this->poll = $poll;
101
		$this->shareService = $shareService;
102
		$this->share = $share;
103
		$this->voteService = $voteService;
104
	}
105
106
	/**
107
	 * Get list of polls
108
	 * @NoAdminRequired
109
	 * @return DataResponse
110
	 */
111
112
	public function list() {
113
		return $this->response(function () {
114
			return $this->pollService->list();
115
		});
116
	}
117
118
119
	/**
120
	 * get complete poll
121
	 * @NoAdminRequired
122
	 * @param int $pollId
123
	 * @return DataResponse
124
	 */
125
	public function get(int $pollId) {
126
		return $this->response(function () use ($pollId) {
127
			$this->share = null;
128
			$this->poll = $this->pollService->get($pollId);
129
			$this->acl->setPoll($this->poll);
130
			return $this->build();
131
		});
132
	}
133
134
	/**
135
	 * get complete poll
136
	 * @NoAdminRequired
137
	 * @return Array
138
	 */
139
	private function build() {
140
		try {
141
			$comments = $this->commentService->list($this->poll->getId(), $this->acl);
142
		} catch (Exception $e) {
143
			$comments = [];
144
		}
145
146
		try {
147
			$options = $this->optionService->list($this->poll->getId());
148
		} catch (Exception $e) {
149
			$options = [];
150
		}
151
152
		try {
153
			$votes = $this->voteService->list($this->poll->getId());
154
		} catch (Exception $e) {
155
			$votes = [];
156
		}
157
158
		try {
159
			$shares = $this->shareService->list($this->poll->getId());
160
		} catch (Exception $e) {
161
			$shares = [];
162
		}
163
164
		return [
165
			'acl' => $this->acl,
166
			'poll' => $this->poll,
167
			'comments' => $comments,
168
			'options' => $options,
169
			'share' => $this->share,
170
			'shares' => $shares,
171
			'votes' => $votes,
172
		];
173
	}
174
175
	/**
176
	 * Add poll
177
	 * @NoAdminRequired
178
	 * @param string $type
179
	 * @param string $title
180
	 * @return DataResponse
181
	 */
182
183
	public function add($type, $title) {
184
		return $this->responseCreate(function () use ($type, $title) {
185
			return $this->pollService->add($type, $title);
186
		});
187
	}
188
189
	/**
190
	 * Update poll configuration
191
	 * @NoAdminRequired
192
	 * @param int $pollId
193
	 * @param array $poll
194
	 * @return DataResponse
195
	 */
196
197
	public function update($pollId, $poll) {
198
		return $this->response(function () use ($pollId, $poll) {
199
			return $this->pollService->update($pollId, $poll);
200
		});
201
	}
202
203
	/**
204
	 * Switch deleted status (move to deleted polls)
205
	 * @NoAdminRequired
206
	 * @param int $pollId
207
	 * @return DataResponse
208
	 */
209
210
	public function switchDeleted($pollId) {
211
		return $this->response(function () use ($pollId) {
212
			return $this->pollService->switchDeleted($pollId);
213
		});
214
	}
215
216
	/**
217
	 * Delete poll
218
	 * @NoAdminRequired
219
	 * @param Array $poll
220
	 * @return DataResponse
221
	 */
222
223
	public function delete($pollId) {
224
		return $this->responseDeleteTolerant(function () use ($pollId) {
225
			return $this->pollService->delete($pollId);
226
		});
227
	}
228
229
	/**
230
	 * Clone poll
231
	 * @NoAdminRequired
232
	 * @param int $pollId
233
	 * @return DataResponse
234
	 */
235
	public function clone($pollId) {
236
		return $this->response(function () use ($pollId) {
237
			$poll = $this->pollService->clone($pollId);
238
			$this->optionService->clone($pollId, $poll->getId());
239
240
			return $poll;
241
		});
242
	}
243
244
	/**
245
	 * Collect email addresses from particitipants
246
	 * @NoAdminRequired
247
	 * @param Array $poll
248
	 * @return DataResponse
249
	 */
250
251
	public function getParticipantsEmailAddresses($pollId) {
252
		return $this->response(function () use ($pollId) {
253
			return $this->pollService->getParticipantsEmailAddresses($pollId);
254
		});
255
	}
256
}
257