Completed
Pull Request — master (#1016)
by René
04:12
created

OptionApiController::setOrder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 2
crap 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 \Doctrine\DBAL\Exception\UniqueConstraintViolationException;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Exception\...raintViolationException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use OCP\AppFramework\Db\DoesNotExistException;
29
use OCA\Polls\Exceptions\NotAuthorizedException;
30
31
use OCP\IRequest;
32
33
use OCP\AppFramework\ApiController;
34
use OCP\AppFramework\Http;
35
use OCP\AppFramework\Http\DataResponse;
36
use OCA\Polls\Service\OptionService;
37
38
class OptionApiController extends ApiController {
39
40
	/** @var OptionService */
41
	private $optionService;
42
43
	/**
44
	 * OptionApiController constructor.
45
	 * @param string $appName
46
	 * @param IRequest $request
47
	 * @param OptionService $optionService
48
	 */
49
50
	public function __construct(
51
		string $appName,
52
		IRequest $request,
53
		OptionService $optionService
54
	) {
55
		parent::__construct($appName,
56
			$request,
57
			'POST, PUT, GET, DELETE',
58
            'Authorization, Content-Type, Accept',
59
            1728000);
60
		$this->optionService = $optionService;
61
	}
62
63
	/**
64
	 * Get all options of given poll
65
	 * @NoAdminRequired
66
	 * @CORS
67
	 * @NoCSRFRequired
68
	 * @param int $pollId
69
	 * @return DataResponse
70
	 */
71
	public function list($pollId) {
72
		try {
73
			return new DataResponse(['options' => $this->optionService->list($pollId)], Http::STATUS_OK);
74
		} catch (DoesNotExistException $e) {
75
			return new DataResponse([], Http::STATUS_NOT_FOUND);
76
		} catch (NotAuthorizedException $e) {
77
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
78
		}
79
	}
80
81
82
	/**
83
	 * Add a new option
84
	 * @NoAdminRequired
85
	 * @CORS
86
	 * @NoCSRFRequired
87
	 * @param int $pollId
88
	 * @param string $pollOptionText
89
	 * @param int $timestamp
90
	 * @return DataResponse
91
	 */
92
	public function add($pollId, $timestamp = 0, $pollOptionText = '') {
93
		try {
94
			return new DataResponse(['option' => $this->optionService->add($pollId, $timestamp, $pollOptionText)], Http::STATUS_CREATED);
95
		} catch (UniqueConstraintViolationException $e) {
96
			return new DataResponse(['error' => 'Option exists'], Http::STATUS_CONFLICT);
97
		} catch (NotAuthorizedException $e) {
98
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
99
		}
100
	}
101
102
103
	/**
104
	 * Update option
105
	 * @NoAdminRequired
106
	 * @CORS
107
	 * @NoCSRFRequired
108
	 * @param array $option
109
	 * @return DataResponse
110
	 */
111
	public function update($optionId, $timestamp = 0 , $pollOptionText = '') {
112
		try {
113
			return new DataResponse(['option' => $this->optionService->update($optionId, $timestamp, $pollOptionText, $order)], Http::STATUS_OK);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $order seems to be never defined.
Loading history...
114
		} catch (NotAuthorizedException $e) {
115
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
116
		}
117
	}
118
119
	/**
120
	 * Delete option
121
	 * @NoAdminRequired
122
	 * @CORS
123
	 * @NoCSRFRequired
124
	 * @param int $optionId
125
	 * @return DataResponse
126
	 */
127
	public function delete($optionId) {
128
		try {
129
			return new DataResponse(['option' => $this->optionService->delete($optionId)], Http::STATUS_OK);
130
		} catch (DoesNotExistException $e) {
131
			return new DataResponse(['error' => 'Option does not exist'], Http::STATUS_NOT_FOUND);
132
		} catch (NotAuthorizedException $e) {
133
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
134
		}
135
	}
136
137
	/**
138
	 * Switch option confirmation
139
	 * @NoAdminRequired
140
	 * @CORS
141
	 * @NoCSRFRequired
142
	 * @param int $optionId
143
	 * @return DataResponse
144
	 */
145
	public function confirm($optionId) {
146
		try {
147
			return new DataResponse(['option' => $this->optionService->confirm($optionId)], Http::STATUS_OK);
148
		} catch (DoesNotExistException $e) {
149
			return new DataResponse(['error' => 'Option does not exist'], Http::STATUS_NOT_FOUND);
150
		} catch (NotAuthorizedException $e) {
151
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
152
		}
153
	}
154
155
	/**
156
	 * Set order position for option
157
	 * @NoAdminRequired
158
	 * @CORS
159
	 * @NoCSRFRequired
160
	 * @param array $option
161
	 * @return DataResponse
162
	 */
163
	public function setOrder($optionId, $order) {
164
		try {
165
			return new DataResponse(['option' => $this->optionService->setOrder($optionId, $order)], Http::STATUS_OK);
166
		} catch (NotAuthorizedException $e) {
167
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
168
		}
169
	}
170
}
171