Completed
Pull Request — master (#966)
by René
29:43 queued 25:49
created

PollApiController::enum()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
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\EmptyTitleException;
29
 use OCA\Polls\Exceptions\InvalidAccessException;
30
 use OCA\Polls\Exceptions\InvalidShowResultsException;
31
 use OCA\Polls\Exceptions\InvalidPollTypeException;
32
 use OCA\Polls\Exceptions\NotAuthorizedException;
33
34
 use OCP\IRequest;
35
 use OCP\ILogger;
36
 use OCP\AppFramework\ApiController;
37
 use OCP\AppFramework\Http;
38
 use OCP\AppFramework\Http\DataResponse;
39
40
 use OCA\Polls\Service\PollService;
41
42
 class PollApiController extends ApiController {
43
44
 	private $logger;
45
 	private $pollService;
46
47
 	/**
48
 	 * PollController constructor.
49
 	 * @param string $appName
50
 	 * @param $userId
51
 	 * @param IRequest $request
52
 	 * @param ILogger $logger
53
 	 * @param PollService $pollService
54
 	 */
55
56
 	public function __construct(
57
 		string $appName,
58
 		IRequest $request,
59
 		ILogger $logger,
60
		PollService $pollService
61
 	) {
62
 		parent::__construct($appName, $request);
63
 		$this->logger = $logger;
64
 		$this->pollService = $pollService;
65
 	}
66
67
68
	/**
69
	 * list
70
	 * @NoAdminRequired
71
	 * @NoCSRFRequired
72
	 * @CORS
73
	 * @return DataResponse
74
	 */
75
76
	public function list() {
77
		try {
78
			return new DataResponse($this->pollService->list(), Http::STATUS_OK);
79
		} catch (DoesNotExistException $e) {
80
			return new DataResponse([], Http::STATUS_NOT_FOUND);
81
		} catch (NotAuthorizedException $e) {
82
			return new DataResponse($e->getMessage(), $e->getStatus());
0 ignored issues
show
Bug introduced by
$e->getMessage() of type string is incompatible with the type array|object expected by parameter $data of OCP\AppFramework\Http\DataResponse::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
			return new DataResponse(/** @scrutinizer ignore-type */ $e->getMessage(), $e->getStatus());
Loading history...
83
		}
84
	}
85
86
87
	/**
88
	 * get
89
	 * @NoAdminRequired
90
	 * @NoCSRFRequired
91
	 * @param integer $pollId
92
	 * @return array
93
	 */
94
 	public function get($pollId) {
95
		try {
96
			return new DataResponse($this->pollService->get($pollId), Http::STATUS_OK);
97
		} catch (DoesNotExistException $e) {
98
			return new DataResponse('Not found', Http::STATUS_NOT_FOUND);
0 ignored issues
show
Bug introduced by
'Not found' of type string is incompatible with the type array|object expected by parameter $data of OCP\AppFramework\Http\DataResponse::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
			return new DataResponse(/** @scrutinizer ignore-type */ 'Not found', Http::STATUS_NOT_FOUND);
Loading history...
99
		} catch (NotAuthorizedException $e) {
100
			return new DataResponse($e->getMessage(), $e->getStatus());
101
		}
102
 	}
103
104
	/**
105
	 * delete
106
	 * @NoAdminRequired
107
	 * @NoCSRFRequired
108
	 * @param Array $poll
109
	 * @return DataResponse
110
	 */
111
112
	public function delete($pollId) {
113
		try {
114
			return new DataResponse($this->pollService->delete($pollId), Http::STATUS_OK);
115
		} catch (DoesNotExistException $e) {
116
			return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND);
0 ignored issues
show
Bug introduced by
'Poll not found' of type string is incompatible with the type array|object expected by parameter $data of OCP\AppFramework\Http\DataResponse::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

116
			return new DataResponse(/** @scrutinizer ignore-type */ 'Poll not found', Http::STATUS_NOT_FOUND);
Loading history...
117
		} catch (NotAuthorizedException $e) {
118
			return new DataResponse($e->getMessage(), $e->getStatus());
119
		}
120
	}
121
122
	/**
123
	 * deletePermanently
124
	 * @NoAdminRequired
125
	 * @NoCSRFRequired
126
	 * @param Array $poll
127
	 * @return DataResponse
128
	 */
129
130
	public function deletePermanently($pollId) {
131
		try {
132
			return new DataResponse($this->pollService->deletePermanently($pollId), Http::STATUS_OK);
133
		} catch (DoesNotExistException $e) {
134
			return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND);
0 ignored issues
show
Bug introduced by
'Poll not found' of type string is incompatible with the type array|object expected by parameter $data of OCP\AppFramework\Http\DataResponse::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
			return new DataResponse(/** @scrutinizer ignore-type */ 'Poll not found', Http::STATUS_NOT_FOUND);
Loading history...
135
		} catch (NotAuthorizedException $e) {
136
			return new DataResponse($e->getMessage(), $e->getStatus());
137
		}
138
139
	}
140
141
	/**
142
	 * write
143
	 * @NoAdminRequired
144
	 * @NoCSRFRequired
145
	 * @param Array $poll
146
	 * @return DataResponse
147
	 */
148
149
	public function add($type, $title) {
150
		try {
151
			return new DataResponse($this->pollService->add($type, $title), Http::STATUS_OK);
152
		} catch (NotAuthorizedException $e) {
153
			return new DataResponse($e->getMessage(), $e->getStatus());
0 ignored issues
show
Bug introduced by
$e->getMessage() of type string is incompatible with the type array|object expected by parameter $data of OCP\AppFramework\Http\DataResponse::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

153
			return new DataResponse(/** @scrutinizer ignore-type */ $e->getMessage(), $e->getStatus());
Loading history...
154
		} catch (InvalidPollTypeException $e) {
155
			return new DataResponse($e->getMessage(), $e->getStatus());
156
		} catch (EmptyTitleException $e) {
157
			return new DataResponse($e->getMessage(), $e->getStatus());
158
		}
159
	}
160
161
	/**
162
	 * write
163
	 * @NoAdminRequired
164
	 * @NoCSRFRequired
165
	 * @param Array $poll
166
	 * @return DataResponse
167
	 */
168
169
	public function update($pollId, $poll) {
170
		try {
171
			return new DataResponse($this->pollService->update($pollId, $poll), Http::STATUS_OK);
172
		} catch (DoesNotExistException $e) {
173
			return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND);
0 ignored issues
show
Bug introduced by
'Poll not found' of type string is incompatible with the type array|object expected by parameter $data of OCP\AppFramework\Http\DataResponse::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

173
			return new DataResponse(/** @scrutinizer ignore-type */ 'Poll not found', Http::STATUS_NOT_FOUND);
Loading history...
174
		} catch (NotAuthorizedException $e) {
175
			return new DataResponse($e->getMessage(), $e->getStatus());
176
		} catch (InvalidAccessException $e) {
177
			return new DataResponse($e->getMessage(), $e->getStatus());
178
		} catch (InvalidShowResultsException $e) {
179
			return new DataResponse($e->getMessage(), $e->getStatus());
180
		} catch (EmptyTitleException $e) {
181
			return new DataResponse($e->getMessage(), $e->getStatus());
182
		}
183
	}
184
185
	/**
186
	 * clone
187
	 * @NoAdminRequired
188
	 * @NoCSRFRequired
189
	 * @param integer $pollId
190
	 * @return DataResponse
191
	 */
192
	public function clone($pollId) {
193
		try {
194
			return new DataResponse($this->pollService->clone($pollId), Http::STATUS_OK);
195
		} catch (DoesNotExistException $e) {
196
			return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND);
0 ignored issues
show
Bug introduced by
'Poll not found' of type string is incompatible with the type array|object expected by parameter $data of OCP\AppFramework\Http\DataResponse::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

196
			return new DataResponse(/** @scrutinizer ignore-type */ 'Poll not found', Http::STATUS_NOT_FOUND);
Loading history...
197
		} catch (NotAuthorizedException $e) {
198
			return new DataResponse($e->getMessage(), $e->getStatus());
199
		}
200
	}
201
202
	/**
203
	 * enum
204
	 * @NoAdminRequired
205
	 * @NoCSRFRequired
206
	 * @param Array $poll
207
	 * @return DataResponse
208
	 */
209
210
	public function enum() {
211
		return [
212
			'poll' => $this->pollService->getValidEnum()
213
		];
214
	}
215
216
217
}
218