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

OptionApiController::list()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 12
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 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($this->optionService->list($pollId), Http::STATUS_OK);
74
		} catch (DoesNotExistException $e) {
75
			return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND);
0 ignored issues
show
Bug introduced by
'Poll with id ' . $pollId . ' 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

75
			return new DataResponse(/** @scrutinizer ignore-type */ 'Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND);
Loading history...
76
		} catch (NotAuthorizedException $e) {
77
			return new DataResponse($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($this->optionService->add($option), Http::STATUS_CREATED);
101
		} catch (DoesNotExistException $e) {
102
			return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND);
0 ignored issues
show
Bug introduced by
'Poll with id ' . $pollId . ' 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

102
			return new DataResponse(/** @scrutinizer ignore-type */ 'Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND);
Loading history...
103
		} catch (UniqueConstraintViolationException $e) {
104
			return new DataResponse('Option exists', Http::STATUS_CONFLICT);
105
		} catch (NotAuthorizedException $e) {
106
			return new DataResponse($e->getMessage(), $e->getStatus());
107
		}
108
	}
109
110
111
	/**
112
	 * Update poll option
113
	 * @NoAdminRequired
114
	 * @CORS
115
	 * @NoCSRFRequired
116
	 * @param Option $option
117
	 * @return DataResponse
118
	 */
119
	public function update($option) {
120
		try {
121
			return new DataResponse($this->optionService->update($option), Http::STATUS_OK);
0 ignored issues
show
Bug introduced by
$option of type OCA\Polls\Controller\Option is incompatible with the type array expected by parameter $option of OCA\Polls\Service\OptionService::update(). ( Ignorable by Annotation )

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

121
			return new DataResponse($this->optionService->update(/** @scrutinizer ignore-type */ $option), Http::STATUS_OK);
Loading history...
122
		} catch (NotAuthorizedException $e) {
123
			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

123
			return new DataResponse(/** @scrutinizer ignore-type */ $e->getMessage(), $e->getStatus());
Loading history...
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($this->optionService->delete($optionId), Http::STATUS_OK);
138
		} catch (DoesNotExistException $e) {
139
			return new DataResponse('Option does not exist', Http::STATUS_NOT_FOUND);
0 ignored issues
show
Bug introduced by
'Option does not exist' 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

139
			return new DataResponse(/** @scrutinizer ignore-type */ 'Option does not exist', Http::STATUS_NOT_FOUND);
Loading history...
140
		} catch (NotAuthorizedException $e) {
141
			return new DataResponse($e->getMessage(), $e->getStatus());
142
		}
143
	}
144
}
145