Passed
Pull Request — master (#1339)
by René
05:25 queued 01:34
created

PollController::delete()   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
eloc 2
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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 OCP\IRequest;
27
use OCP\AppFramework\Controller;
28
use OCP\AppFramework\Http\DataResponse;
29
30
use OCA\Polls\Db\Poll;
31
use OCA\Polls\Service\PollService;
32
use OCA\Polls\Service\OptionService;
33
use OCA\Polls\Model\Acl;
34
35
class PollController extends Controller {
36
37
	/** @var Acl */
38
	private $acl;
39
40
	/** @var OptionService */
41
	private $optionService;
42
43
	/** @var PollService */
44
	private $pollService;
45
46
	/** @var Poll */
47
	private $poll;
48
49
	use ResponseHandle;
50
51
	public function __construct(
52
		string $appName,
53
		IRequest $request,
54
		Acl $acl,
55
		OptionService $optionService,
56
		PollService $pollService,
57
		Poll $poll
58
	) {
59
		parent::__construct($appName, $request);
60
		$this->acl = $acl;
61
		$this->optionService = $optionService;
62
		$this->pollService = $pollService;
63
		$this->poll = $poll;
64
	}
65
66
	/**
67
	 * Get list of polls
68
	 * @NoAdminRequired
69
	 */
70
71
	public function list(): DataResponse {
72
		return $this->response(function () {
73
			return $this->pollService->list();
74
		});
75
	}
76
77
	/**
78
	 * get complete poll
79
	 * @NoAdminRequired
80
	 */
81
	public function get(int $pollId): DataResponse {
82
		return $this->response(function () use ($pollId) {
83
			$this->poll = $this->pollService->get($pollId);
84
			$this->acl->setPoll($this->poll)->request(Acl::PERMISSION_VIEW);
85
			return [
86
				'acl' => $this->acl,
87
				'poll' => $this->poll,
88
			];
89
		});
90
	}
91
92
	/**
93
	 * Add poll
94
	 * @NoAdminRequired
95
	 */
96
97
	public function add($type, $title): DataResponse {
98
		return $this->responseCreate(function () use ($type, $title) {
99
			return $this->pollService->add($type, $title);
100
		});
101
	}
102
103
	/**
104
	 * Update poll configuration
105
	 * @NoAdminRequired
106
	 */
107
108
	public function update($pollId, $poll): DataResponse {
109
		return $this->response(function () use ($pollId, $poll) {
110
			return $this->pollService->update($pollId, $poll);
111
		});
112
	}
113
114
	/**
115
	 * Switch deleted status (move to deleted polls)
116
	 * @NoAdminRequired
117
	 */
118
119
	public function switchDeleted($pollId): DataResponse {
120
		return $this->response(function () use ($pollId) {
121
			return $this->pollService->switchDeleted($pollId);
122
		});
123
	}
124
125
	/**
126
	 * Delete poll
127
	 * @NoAdminRequired
128
	 */
129
130
	public function delete($pollId): DataResponse {
131
		return $this->responseDeleteTolerant(function () use ($pollId) {
132
			return $this->pollService->delete($pollId);
133
		});
134
	}
135
136
	/**
137
	 * Clone poll
138
	 * @NoAdminRequired
139
	 */
140
	public function clone($pollId): DataResponse {
141
		return $this->response(function () use ($pollId) {
142
			$poll = $this->pollService->clone($pollId);
143
			$this->optionService->clone($pollId, $poll->getId());
144
145
			return $poll;
146
		});
147
	}
148
149
	/**
150
	 * Collect email addresses from particitipants
151
	 * @NoAdminRequired
152
	 */
153
154
	public function getParticipantsEmailAddresses($pollId): DataResponse {
155
		return $this->response(function () use ($pollId) {
156
			return $this->pollService->getParticipantsEmailAddresses($pollId);
157
		});
158
	}
159
}
160