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

PollController   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 26
eloc 58
c 2
b 0
f 0
dl 0
loc 156
ccs 0
cts 79
cp 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A list() 0 7 3
A deletePermanently() 0 7 3
A delete() 0 7 3
A update() 0 13 6
A get() 0 7 3
A add() 0 9 4
A clone() 0 7 3
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(['error' => $e->getMessage()], $e->getStatus());
81
		}
82
	}
83
84
85
	/**
86
	 * get
87
	 * @NoAdminRequired
88
	 * @NoCSRFRequired
89
	 * @PublicPage
90
	 * @param integer $pollId
91
	 * @return array
92
	 */
93
 	public function get($pollId, $token) {
94
		try {
95
			return new DataResponse($this->pollService->get($pollId, $token), Http::STATUS_OK);
96
		} catch (DoesNotExistException $e) {
97
			return new DataResponse(['error' => 'Not found'], Http::STATUS_NOT_FOUND);
98
		} catch (NotAuthorizedException $e) {
99
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
100
		}
101
 	}
102
103
	/**
104
	 * delete
105
	 * @NoAdminRequired
106
	 * @NoCSRFRequired
107
	 * @param Array $poll
108
	 * @return DataResponse
109
	 */
110
111
	public function delete($pollId) {
112
		try {
113
			return new DataResponse($this->pollService->delete($pollId), Http::STATUS_OK);
114
		} catch (DoesNotExistException $e) {
115
			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
116
		} catch (NotAuthorizedException $e) {
117
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
118
		}
119
	}
120
121
	/**
122
	 * deletePermanently
123
	 * @NoAdminRequired
124
	 * @NoCSRFRequired
125
	 * @param Array $poll
126
	 * @return DataResponse
127
	 */
128
129
	public function deletePermanently($pollId) {
130
		try {
131
			return new DataResponse($this->pollService->deletePermanently($pollId), Http::STATUS_OK);
132
		} catch (DoesNotExistException $e) {
133
			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
134
		} catch (NotAuthorizedException $e) {
135
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
136
		}
137
	}
138
139
140
	/**
141
	 * write
142
	 * @NoAdminRequired
143
	 * @NoCSRFRequired
144
	 * @param Array $poll
145
	 * @return DataResponse
146
	 */
147
148
	public function add($type, $title) {
149
		try {
150
			return new DataResponse($this->pollService->add($type, $title), Http::STATUS_OK);
151
		} catch (NotAuthorizedException $e) {
152
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
153
		} catch (InvalidPollTypeException $e) {
154
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
155
		} catch (EmptyTitleException $e) {
156
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
157
		}
158
	}
159
160
	/**
161
	 * write
162
	 * @NoAdminRequired
163
	 * @NoCSRFRequired
164
	 * @param Array $poll
165
	 * @return DataResponse
166
	 */
167
168
	public function update($pollId, $poll) {
169
		try {
170
			return new DataResponse($this->pollService->update($pollId, $poll), Http::STATUS_OK);
171
		} catch (DoesNotExistException $e) {
172
			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
173
		} catch (NotAuthorizedException $e) {
174
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
175
		} catch (InvalidAccessException $e) {
176
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
177
		} catch (InvalidShowResultsException $e) {
178
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
179
		} catch (EmptyTitleException $e) {
180
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
181
		}
182
	}
183
184
	/**
185
	 * clone
186
	 * @NoAdminRequired
187
	 * @NoCSRFRequired
188
	 * @param integer $pollId
189
	 * @return DataResponse
190
	 */
191
	public function clone($pollId) {
192
		try {
193
			return new DataResponse($this->pollService->clone($pollId), Http::STATUS_OK);
194
		} catch (DoesNotExistException $e) {
195
			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
196
		} catch (NotAuthorizedException $e) {
197
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
198
		}
199
	}
200
201
}
202