PollApiController   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 25
eloc 53
c 0
b 0
f 0
dl 0
loc 148
ccs 0
cts 70
cp 0
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getParticipantsEmailAddresses() 0 7 3
A enum() 0 2 1
A switchDeleted() 0 7 3
A list() 0 7 3
A __construct() 0 7 1
A get() 0 7 3
A clone() 0 7 3
A delete() 0 7 3
A update() 0 7 3
A add() 0 5 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\AppFramework\Db\DoesNotExistException;
27
 use OCA\Polls\Exceptions\Exception;
28
29
 use OCP\IRequest;
30
 use OCP\AppFramework\ApiController;
31
 use OCP\AppFramework\Http;
32
 use OCP\AppFramework\Http\DataResponse;
33
34
 use OCA\Polls\Service\PollService;
35
36
 class PollApiController extends ApiController {
37
38
	 /** @var PollService */
39
 	private $pollService;
40
41
 	public function __construct(
42
		string $appName,
43
		IRequest $request,
44
		PollService $pollService
45
	) {
46
 		parent::__construct($appName, $request);
47
 		$this->pollService = $pollService;
48
 	}
49
50
 	/**
51
 	 * Get list of polls
52
 	 * @NoAdminRequired
53
 	 * @CORS
54
 	 * @NoCSRFRequired
55
 	 */
56
 	public function list(): DataResponse {
57
 		try {
58
 			return new DataResponse(['polls' => $this->pollService->list()], Http::STATUS_OK);
59
 		} catch (DoesNotExistException $e) {
60
 			return new DataResponse([], Http::STATUS_NOT_FOUND);
61
 		} catch (Exception $e) {
62
 			return new DataResponse(['message' => $e->getMessage()], $e->getStatus());
63
 		}
64
 	}
65
66
 	/**
67
 	 * get poll configuration
68
 	 * @NoAdminRequired
69
 	 * @CORS
70
 	 * @NoCSRFRequired
71
 	 */
72
 	public function get(int $pollId): DataResponse {
73
 		try {
74
 			return new DataResponse(['poll' => $this->pollService->get($pollId)], Http::STATUS_OK);
75
 		} catch (DoesNotExistException $e) {
76
 			return new DataResponse(['error' => 'Not found'], Http::STATUS_NOT_FOUND);
77
 		} catch (Exception $e) {
78
 			return new DataResponse(['message' => $e->getMessage()], $e->getStatus());
79
 		}
80
 	}
81
82
 	/**
83
 	 * Add poll
84
 	 * @NoAdminRequired
85
 	 * @NoCSRFRequired
86
 	 * @CORS
87
 	 */
88
 	public function add(string $type, string $title): DataResponse {
89
 		try {
90
 			return new DataResponse(['poll' => $this->pollService->add($type, $title)], Http::STATUS_CREATED);
91
 		} catch (Exception $e) {
92
 			return new DataResponse(['message' => $e->getMessage()], $e->getStatus());
93
 		}
94
 	}
95
96
 	/**
97
 	 * Update poll configuration
98
 	 * @NoAdminRequired
99
 	 * @CORS
100
 	 * @NoCSRFRequired
101
 	 */
102
 	public function update(int $pollId, array $poll): DataResponse {
103
 		try {
104
 			return new DataResponse(['poll' => $this->pollService->update($pollId, $poll)], Http::STATUS_OK);
105
 		} catch (DoesNotExistException $e) {
106
 			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
107
 		} catch (Exception $e) {
108
 			return new DataResponse(['message' => $e->getMessage()], $e->getStatus());
109
 		}
110
 	}
111
112
 	/**
113
 	 * Switch deleted status (move to deleted polls)
114
 	 * @NoAdminRequired
115
 	 * @CORS
116
 	 * @NoCSRFRequired
117
 	 */
118
 	public function switchDeleted(int $pollId): DataResponse {
119
 		try {
120
 			return new DataResponse(['poll' => $this->pollService->switchDeleted($pollId)], Http::STATUS_OK);
121
 		} catch (DoesNotExistException $e) {
122
 			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
123
 		} catch (Exception $e) {
124
 			return new DataResponse(['message' => $e->getMessage()], $e->getStatus());
125
 		}
126
 	}
127
128
 	/**
129
 	 * Delete poll
130
 	 * @NoAdminRequired
131
 	 * @CORS
132
 	 * @NoCSRFRequired
133
 	 */
134
 	public function delete(int $pollId): DataResponse {
135
 		try {
136
 			return new DataResponse(['poll' => $this->pollService->delete($pollId)], Http::STATUS_OK);
137
 		} catch (DoesNotExistException $e) {
138
 			return new DataResponse(['message' => $e->getMessage()], Http::STATUS_OK);
139
 		} catch (Exception $e) {
140
 			return new DataResponse(['message' => $e->getMessage()], $e->getStatus());
141
 		}
142
 	}
143
144
 	/**
145
 	 * Clone poll
146
 	 * @NoAdminRequired
147
 	 * @CORS
148
 	 * @NoCSRFRequired
149
 	 */
150
 	public function clone(int $pollId): DataResponse {
151
 		try {
152
 			return new DataResponse(['poll' => $this->pollService->clone($pollId)], Http::STATUS_CREATED);
153
 		} catch (DoesNotExistException $e) {
154
 			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
155
 		} catch (Exception $e) {
156
 			return new DataResponse(['message' => $e->getMessage()], $e->getStatus());
157
 		}
158
 	}
159
160
 	/**
161
 	 * Collect email addresses from particitipants
162
 	 * @NoAdminRequired
163
 	 * @CORS
164
 	 * @NoCSRFRequired
165
 	 */
166
 	public function getParticipantsEmailAddresses(int $pollId): DataResponse {
167
 		try {
168
 			return new DataResponse($this->pollService->getParticipantsEmailAddresses($pollId), Http::STATUS_OK);
169
 		} catch (DoesNotExistException $e) {
170
 			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
171
 		} catch (Exception $e) {
172
 			return new DataResponse(['message' => $e->getMessage()], $e->getStatus());
173
 		}
174
 	}
175
176
 	/**
177
 	 * Get valid values for configuration options
178
 	 * @NoAdminRequired
179
 	 * @CORS
180
 	 * @NoCSRFRequired
181
 	 */
182
 	public function enum(): DataResponse {
183
 		return new DataResponse($this->pollService->getValidEnum(), Http::STATUS_OK);
184
 	}
185
 }
186