Completed
Pull Request — master (#966)
by René
29:43 queued 25:49
created

VoteApiController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 13
ccs 0
cts 13
cp 0
crap 2
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 OCP\AppFramework\Db\DoesNotExistException;
28
29
use OCP\IRequest;
30
use OCP\ILogger;
31
use OCP\AppFramework\ApiController;
32
use OCP\AppFramework\Http;
33
use OCP\AppFramework\Http\DataResponse;
34
35
use OCA\Polls\Service\VoteService;
36
37
class VoteApiController extends ApiController {
38
39
	private $logger;
40
	private $voteService;
41
42
	/**
43
	 * VoteController constructor.
44
	 * @param string $appName
45
	 * @param IRequest $request
46
	 * @param ILogger $logger
47
	 * @param VoteService $voteService
48
	 */
49
	public function __construct(
50
		string $appName,
51
		IRequest $request,
52
		ILogger $logger,
53
		VoteService $voteService
54
	) {
55
		parent::__construct($appName,
56
			$request,
57
			'PUT, GET, DELETE',
58
            'Authorization, Content-Type, Accept',
59
            1728000);
60
		$this->voteService = $voteService;
61
		$this->logger = $logger;
62
	}
63
64
	/**
65
	 * Get all votes of given poll
66
	 * Read all votes of a poll based on the poll id and return list as array
67
	 * @NoAdminRequired
68
	 * @NoCSRFRequired
69
	 * @CORS
70
	 * @param integer $pollId
71
	 * @return DataResponse
72
	 */
73
	public function list($pollId) {
74
		try {
75
			return new DataResponse($this->voteService->list($pollId), Http::STATUS_OK);
76
		} catch (NotAuthorizedException $e) {
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Controller\NotAuthorizedException 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...
77
			return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
0 ignored issues
show
Bug introduced by
'Unauthorized' 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

77
			return new DataResponse(/** @scrutinizer ignore-type */ 'Unauthorized', Http::STATUS_FORBIDDEN);
Loading history...
78
		} catch (DoesNotExistException $e) {
79
			return new DataResponse('No votes', Http::STATUS_NOT_FOUND);
80
		}
81
	}
82
83
	/**
84
	 * set
85
	 * @NoAdminRequired
86
	 * @NoCSRFRequired
87
	 * @CORS
88
	 * @param integer $pollId
89
	 * @param Array $option
90
	 * @param string $userId
91
	 * @param string $setTo
92
	 * @return DataResponse
93
	 */
94
	public function set($pollId, $pollOptionText, $setTo) {
95
		try {
96
			return new DataResponse($this->voteService->set($pollId, $pollOptionText, $setTo), Http::STATUS_OK);
97
		} catch (NotAuthorizedException $e) {
98
			return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
99
		} catch (DoesNotExistException $e) {
100
			return new DataResponse('Option not found', Http::STATUS_NOT_FOUND);
101
		}
102
103
	}
104
105
106
	/**
107
	 * delete
108
	 * @NoAdminRequired
109
	 * @NoCSRFRequired
110
	 * @CORS
111
	 * @param integer $voteId
112
	 * @param string $userId
113
	 * @param integer $pollId
114
	 * @return DataResponse
115
	 */
116
	public function delete($pollId, $userId) {
117
		try {
118
			return new DataResponse($this->voteService->delete($userId, $pollId), Http::STATUS_OK);
0 ignored issues
show
Bug introduced by
$userId of type string is incompatible with the type integer expected by parameter $pollId of OCA\Polls\Service\VoteService::delete(). ( Ignorable by Annotation )

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

118
			return new DataResponse($this->voteService->delete(/** @scrutinizer ignore-type */ $userId, $pollId), Http::STATUS_OK);
Loading history...
119
		} catch (NotAuthorizedException $e) {
120
			return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
0 ignored issues
show
Bug introduced by
'Unauthorized' 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

120
			return new DataResponse(/** @scrutinizer ignore-type */ 'Unauthorized', Http::STATUS_FORBIDDEN);
Loading history...
121
		} catch (DoesNotExistException $e) {
122
			return new DataResponse('', Http::STATUS_NOT_FOUND);
123
		}
124
	}
125
126
}
127