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

ShareApiController::add()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 6
c 1
b 0
f 1
nc 3
nop 4
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
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
	 * getByToken
66
	 * Get pollId by token
67
	 * @NoAdminRequired
68
	 * @NoCSRFRequired
69
	 * @CORS
70
	 * @PublicPage
71
	 * @param string $token
72
	 * @return DataResponse
73
	 */
74
	public function get($token) {
75
		try {
76
			return new DataResponse($this->shareService->get($token), Http::STATUS_OK);
77
		} catch (NotAuthorizedException $e) {
78
			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

78
			return new DataResponse(/** @scrutinizer ignore-type */ 'Unauthorized', Http::STATUS_FORBIDDEN);
Loading history...
79
		} catch (DoesNotExistException $e) {
80
			return new DataResponse('Token ' . $token . ' not found', Http::STATUS_NOT_FOUND);
81
		}
82
	}
83
84
	/**
85
	 * get
86
	 * Read all shares of a poll based on the poll id and return list as array
87
	 * @NoAdminRequired
88
	 * @CORS
89
	 * @NoCSRFRequired
90
	 * @param integer $pollId
91
	 * @return DataResponse
92
	 */
93
	public function list($pollId) {
94
		try {
95
			return new DataResponse($this->shareService->list($pollId), Http::STATUS_OK);
96
		} catch (NotAuthorizedException $e) {
97
			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

97
			return new DataResponse(/** @scrutinizer ignore-type */ 'Unauthorized', Http::STATUS_FORBIDDEN);
Loading history...
98
		} catch (DoesNotExistException $e) {
99
			return new DataResponse('No shares for poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND);
100
		}
101
	}
102
103
	/**
104
	 * Write a new share to the db and returns the new share as array
105
	 * @NoAdminRequired
106
	 * @CORS
107
	 * @NoCSRFRequired
108
	 * @param int $pollId
109
	 * @param string $message
110
	 * @return DataResponse
111
	 */
112
	public function add($pollId, $type, $userId = '', $userEmail = '') {
113
		try {
114
			return new DataResponse($this->shareService->add($pollId, $type, $userId, $userEmail), Http::STATUS_CREATED);
115
		} catch (NotAuthorizedException $e) {
116
			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

116
			return new DataResponse(/** @scrutinizer ignore-type */ 'Unauthorized', Http::STATUS_FORBIDDEN);
Loading history...
117
		} catch (\Exception $e) {
118
			return new DataResponse($e, Http::STATUS_CONFLICT);
119
		}
120
121
	}
122
123
	/**
124
	 * createPersonalShare
125
	 * Write a new share to the db and returns the new share as array
126
	 * @NoAdminRequired
127
	 * @CORS
128
	 * @PublicPage
129
	 * @NoCSRFRequired
130
	 * @param int $pollId
131
	 * @param string $message
132
	 * @return DataResponse
133
	 */
134
	public function createPersonalShare($token, $userName) {
135
136
		try {
137
			return new DataResponse($this->shareService->createPersonalShare($token, $userName), Http::STATUS_CREATED);
138
		} catch (NotAuthorizedException $e) {
139
			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

139
			return new DataResponse(/** @scrutinizer ignore-type */ 'Unauthorized', Http::STATUS_FORBIDDEN);
Loading history...
140
		} catch (InvalidUsername $e) {
141
			return new DataResponse($userName . ' is not valid', Http::STATUS_CONFLICT);
142
		} catch (DoesNotExistException $e) {
143
			// return forbidden in all not catched error cases
144
			return new DataResponse($e, Http::STATUS_FORBIDDEN);
145
		}
146
	}
147
148
	/**
149
	 * remove
150
	 * remove share
151
	 * @NoAdminRequired
152
	 * @CORS
153
	 * @NoCSRFRequired
154
	 * @param Share $share
155
	 * @return DataResponse
156
	 */
157
158
	public function delete($token) {
159
		try {
160
			return new DataResponse($this->shareService->remove($token), Http::STATUS_OK);
161
		} catch (NotAuthorizedException $e) {
162
			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

162
			return new DataResponse(/** @scrutinizer ignore-type */ 'Unauthorized', Http::STATUS_FORBIDDEN);
Loading history...
163
		} catch (Exception $e) {
164
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
165
		}
166
	}
167
}
168