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

VoteApiController::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 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 (DoesNotExistException $e) {
77
			return new DataResponse('No votes', Http::STATUS_NOT_FOUND);
0 ignored issues
show
Bug introduced by
'No votes' 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 */ 'No votes', Http::STATUS_NOT_FOUND);
Loading history...
78
		} 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...
79
			return new DataResponse($e->getMessage(), $e->getStatus());
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 (DoesNotExistException $e) {
98
			return new DataResponse('Option not found', Http::STATUS_NOT_FOUND);
99
		} catch (NotAuthorizedException $e) {
100
			return new DataResponse($e->getMessage(), $e->getStatus());
101
		}
102
103
	}
104
}
105