Passed
Pull Request — master (#966)
by René
04:09
created

PollController   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 71
c 2
b 0
f 0
dl 0
loc 181
ccs 0
cts 95
cp 0
rs 9.84
wmc 32

9 Methods

Rating   Name   Duplication   Size   Complexity  
A clone() 0 7 3
A deletePermanently() 0 7 3
A delete() 0 7 3
A __construct() 0 9 1
A update() 0 14 6
A get() 0 7 3
A add() 0 9 4
A list() 0 7 3
A write() 0 13 6
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\Controller;
37
use OCP\AppFramework\Http;
38
use OCP\AppFramework\Http\DataResponse;
39
40
use OCA\Polls\Service\PollService;
41
42
 class PollController extends Controller {
43
44
	 private $logger;
45
	 private $pollService;
46
47
 	/**
48
 	 * PollController constructor.
49
 	 * @param string $appName
50
 	 * @param IRequest $request
51
 	 * @param ILogger $logger
52
 	 * @param PollService $pollService
53
 	 */
54
55
 	public function __construct(
56
		string $appName,
57
 		IRequest $request,
58
 		ILogger $logger,
59
 		PollService $pollService
60
 	) {
61
 		parent::__construct($appName, $request);
62
 		$this->pollService = $pollService;
63
 		$this->logger = $logger;
64
 	}
65
66
67
	/**
68
	 * list
69
	 * @NoAdminRequired
70
	 * @NoCSRFRequired
71
	 * @return DataResponse
72
	 */
73
74
	public function list() {
75
		try {
76
			return new DataResponse($this->pollService->list(), Http::STATUS_OK);
77
		} catch (DoesNotExistException $e) {
78
			return new DataResponse([], Http::STATUS_NOT_FOUND);
79
		} catch (NotAuthorizedException $e) {
80
			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

80
			return new DataResponse(/** @scrutinizer ignore-type */ $e->getMessage(), $e->getStatus());
Loading history...
81
		}
82
	}
83
84
85
	/**
86
	 * get
87
	 * @NoAdminRequired
88
	 * @NoCSRFRequired
89
	 * @param integer $pollId
90
	 * @return array
91
	 */
92
 	public function get($pollId, $token) {
93
		try {
94
			return new DataResponse($this->pollService->get($pollId, $token), Http::STATUS_OK);
95
		} catch (DoesNotExistException $e) {
96
			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

96
			return new DataResponse(/** @scrutinizer ignore-type */ 'Not found', Http::STATUS_NOT_FOUND);
Loading history...
97
		} catch (NotAuthorizedException $e) {
98
			return new DataResponse($e->getMessage(), $e->getStatus());
99
		}
100
 	}
101
102
	/**
103
	 * delete
104
	 * @NoAdminRequired
105
	 * @NoCSRFRequired
106
	 * @param Array $poll
107
	 * @return DataResponse
108
	 */
109
110
	public function delete($pollId) {
111
		try {
112
			return new DataResponse($this->pollService->delete($pollId), Http::STATUS_OK);
113
		} catch (DoesNotExistException $e) {
114
			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

114
			return new DataResponse(/** @scrutinizer ignore-type */ 'Poll not found', Http::STATUS_NOT_FOUND);
Loading history...
115
		} catch (NotAuthorizedException $e) {
116
			return new DataResponse($e->getMessage(), $e->getStatus());
117
		}
118
	}
119
120
	/**
121
	 * deletePermanently
122
	 * @NoAdminRequired
123
	 * @NoCSRFRequired
124
	 * @param Array $poll
125
	 * @return DataResponse
126
	 */
127
128
	public function deletePermanently($pollId) {
129
		try {
130
			return new DataResponse($this->pollService->deletePermanently($pollId), Http::STATUS_OK);
131
		} catch (DoesNotExistException $e) {
132
			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

132
			return new DataResponse(/** @scrutinizer ignore-type */ 'Poll not found', Http::STATUS_NOT_FOUND);
Loading history...
133
		} catch (NotAuthorizedException $e) {
134
			return new DataResponse($e->getMessage(), $e->getStatus());
135
		}
136
	}
137
138
139
	/**
140
	 * write
141
	 * @NoAdminRequired
142
	 * @NoCSRFRequired
143
	 * @param Array $poll
144
	 * @return DataResponse
145
	 */
146
147
	public function add($type, $title) {
148
		try {
149
			return new DataResponse($this->pollService->add($type, $title), Http::STATUS_OK);
150
		} catch (NotAuthorizedException $e) {
151
			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

151
			return new DataResponse(/** @scrutinizer ignore-type */ $e->getMessage(), $e->getStatus());
Loading history...
152
		} catch (InvalidPollTypeException $e) {
153
			return new DataResponse($e->getMessage(), $e->getStatus());
154
		} catch (EmptyTitleException $e) {
155
			return new DataResponse($e->getMessage(), $e->getStatus());
156
		}
157
	}
158
159
	/**
160
	 * write
161
	 * @NoAdminRequired
162
	 * @NoCSRFRequired
163
	 * @param Array $poll
164
	 * @return DataResponse
165
	 */
166
167
	public function update($pollId, $poll) {
168
		$this->logger->alert(json_encode($poll));
169
		try {
170
			return new DataResponse($this->pollService->update($pollId, $poll), Http::STATUS_OK);
171
		} catch (DoesNotExistException $e) {
172
			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

172
			return new DataResponse(/** @scrutinizer ignore-type */ 'Poll not found', Http::STATUS_NOT_FOUND);
Loading history...
173
		} catch (NotAuthorizedException $e) {
174
			return new DataResponse($e->getMessage(), $e->getStatus());
175
		} catch (InvalidAccessException $e) {
176
			return new DataResponse($e->getMessage(), $e->getStatus());
177
		} catch (InvalidShowResultsException $e) {
178
			return new DataResponse($e->getMessage(), $e->getStatus());
179
		} catch (EmptyTitleException $e) {
180
			return new DataResponse($e->getMessage(), $e->getStatus());
181
		}
182
	}
183
184
	/**
185
	 * write
186
	 * @NoAdminRequired
187
	 * @NoCSRFRequired
188
	 * @depicated
189
	 * @param Array $poll
190
	 * @return DataResponse
191
	 */
192
193
	public function write($poll) {
194
		try {
195
			return new DataResponse($this->pollService->write($poll), Http::STATUS_OK);
196
		} catch (DoesNotExistException $e) {
197
			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

197
			return new DataResponse(/** @scrutinizer ignore-type */ 'Poll not found', Http::STATUS_NOT_FOUND);
Loading history...
198
		} catch (NotAuthorizedException $e) {
199
			return new DataResponse($e->getMessage(), $e->getStatus());
200
		} catch (InvalidAccessException $e) {
201
			return new DataResponse($e->getMessage(), $e->getStatus());
202
		} catch (InvalidShowResultsException $e) {
203
			return new DataResponse($e->getMessage(), $e->getStatus());
204
		} catch (EmptyTitleException $e) {
205
			return new DataResponse($e->getMessage(), $e->getStatus());
206
		}
207
	}
208
209
	/**
210
	 * clone
211
	 * @NoAdminRequired
212
	 * @NoCSRFRequired
213
	 * @param integer $pollId
214
	 * @return DataResponse
215
	 */
216
	public function clone($pollId) {
217
		try {
218
			return new DataResponse($this->pollService->clone($pollId), Http::STATUS_OK);
219
		} catch (DoesNotExistException $e) {
220
			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

220
			return new DataResponse(/** @scrutinizer ignore-type */ 'Poll not found', Http::STATUS_NOT_FOUND);
Loading history...
221
		} catch (NotAuthorizedException $e) {
222
			return new DataResponse($e->getMessage(), $e->getStatus());
223
		}
224
	}
225
226
}
227