Completed
Push — master ( 9466d5...cf3e21 )
by René
18s queued 15s
created

OptionApiController::add()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 15
ccs 0
cts 14
cp 0
crap 20
rs 9.8666
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
use OCP\AppFramework\ApiController;
33
use OCP\AppFramework\Http;
34
use OCP\AppFramework\Http\DataResponse;
35
36
37
use OCA\Polls\Service\OptionService;
38
39
class OptionApiController extends ApiController {
40
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 integer $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(['error' => 'Poll with id ' . $pollId . ' not found'], 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 to poll
84
	 * @NoAdminRequired
85
	 * @CORS
86
	 * @NoCSRFRequired
87
	 * @param integer $pollId
88
	 * @param string $pollOptionText
89
	 * @param integer $timestamp
90
	 * @return DataResponse
91
	 */
92
	public function add($pollId, $pollOptionText = '', $timestamp = 0) {
93
		$option = [
94
			'pollId' => $pollId,
95
			'pollOptionText' => $pollOptionText,
96
			'timestamp' => $timestamp
97
		];
98
99
		try {
100
			return new DataResponse(['option' => $this->optionService->add($option)], Http::STATUS_CREATED);
101
		} catch (DoesNotExistException $e) {
102
			return new DataResponse(['error' => 'Poll with id ' . $pollId . ' not found'], Http::STATUS_NOT_FOUND);
103
		} catch (UniqueConstraintViolationException $e) {
104
			return new DataResponse(['error' => 'Option exists'], Http::STATUS_CONFLICT);
105
		} catch (NotAuthorizedException $e) {
106
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
107
		}
108
	}
109
110
111
	/**
112
	 * Update poll option
113
	 * @NoAdminRequired
114
	 * @CORS
115
	 * @NoCSRFRequired
116
	 * @param array $option
117
	 * @return DataResponse
118
	 */
119
	public function update($option) {
120
		try {
121
			return new DataResponse(['option' => $this->optionService->update($option)], Http::STATUS_OK);
122
		} catch (NotAuthorizedException $e) {
123
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
124
		}
125
	}
126
127
	/**
128
	 * Remove a single option
129
	 * @NoAdminRequired
130
	 * @CORS
131
	 * @NoCSRFRequired
132
	 * @param integer $optionId
133
	 * @return DataResponse
134
	 */
135
	public function delete($optionId) {
136
		try {
137
			return new DataResponse(['option' => $this->optionService->delete($optionId)], Http::STATUS_OK);
138
		} catch (DoesNotExistException $e) {
139
			return new DataResponse(['error' => 'Option does not exist'], Http::STATUS_NOT_FOUND);
140
		} catch (NotAuthorizedException $e) {
141
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
142
		}
143
	}
144
}
145