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

ShareApiController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

77
			return new DataResponse(/** @scrutinizer ignore-type */ 'No shares for poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND);
Loading history...
78
		} catch (NotAuthorizedException $e) {
79
			return new DataResponse($e->getMessage(), $e->getStatus());
80
		}
81
	}
82
83
	/**
84
	* get share by token
85
	* Get pollId by token
86
	* @NoAdminRequired
87
	* @NoCSRFRequired
88
	* @CORS
89
	* @param string $token
90
	* @return DataResponse
91
	*/
92
	public function get($token) {
93
		try {
94
			return new DataResponse($this->shareService->get($token), Http::STATUS_OK);
95
		} catch (DoesNotExistException $e) {
96
			return new DataResponse('Token ' . $token . ' not found', Http::STATUS_NOT_FOUND);
0 ignored issues
show
Bug introduced by
'Token ' . $token . ' 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

96
			return new DataResponse(/** @scrutinizer ignore-type */ 'Token ' . $token . ' not found', Http::STATUS_NOT_FOUND);
Loading history...
97
		} catch (NotAuthorizedException $e) {
98
			return new DataResponse($e->getMessage(), $e->getStatus());
99
		}
100
	}
101
102
	/**
103
	 * Write a new share to the db and returns the new share as array
104
	 * @NoAdminRequired
105
	 * @CORS
106
	 * @NoCSRFRequired
107
	 * @param int $pollId
108
	 * @param string $type
109
	 * @param string $userId
110
	 * @param string $userEmail
111
	 * @return DataResponse
112
	 */
113
	public function add($pollId, $type, $userId = '', $userEmail = '') {
114
		try {
115
			return new DataResponse($this->shareService->add($pollId, $type, $userId, $userEmail), Http::STATUS_CREATED);
116
		} catch (\Exception $e) {
117
			return new DataResponse($e, Http::STATUS_CONFLICT);
118
		} catch (NotAuthorizedException $e) {
119
			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

119
			return new DataResponse(/** @scrutinizer ignore-type */ $e->getMessage(), $e->getStatus());
Loading history...
120
		}
121
122
	}
123
124
	/**
125
	 * delete share
126
	 * @NoAdminRequired
127
	 * @CORS
128
	 * @NoCSRFRequired
129
	 * @param string $token
130
	 * @return DataResponse
131
	 */
132
133
	public function delete($token) {
134
		try {
135
			return new DataResponse($this->shareService->remove($token), Http::STATUS_OK);
136
		} catch (NotAuthorizedException $e) {
137
			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

137
			return new DataResponse(/** @scrutinizer ignore-type */ 'Unauthorized', Http::STATUS_FORBIDDEN);
Loading history...
138
		} catch (Exception $e) {
139
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
140
		}
141
	}
142
}
143