Completed
Pull Request — master (#966)
by René
04:37
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 ShareService $shareService
49
	 */
50
	public function __construct(
51
		string $appName,
52
		IRequest $request,
53
		ShareService $shareService
54
	) {
55
		parent::__construct($appName,
56
			$request,
57
			'POST, PUT, GET, DELETE',
58
            'Authorization, Content-Type, Accept',
59
            1728000);
60
		$this->shareService = $shareService;
61
	}
62
63
	/**
64
	 * list
65
	 * Read all shares of a poll based on the poll id and return list as array
66
	 * @NoAdminRequired
67
	 * @CORS
68
	 * @NoCSRFRequired
69
	 * @param integer $pollId
70
	 * @return DataResponse
71
	 */
72
	public function list($pollId) {
73
		try {
74
			return new DataResponse(['shares' => $this->shareService->list($pollId)], Http::STATUS_OK);
75
		} catch (DoesNotExistException $e) {
76
			return new DataResponse(['error' => 'No shares for poll with id ' . $pollId . ' not found'], Http::STATUS_NOT_FOUND);
77
		} catch (NotAuthorizedException $e) {
78
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
79
		}
80
	}
81
82
	/**
83
	* get share by token
84
	* Get pollId by token
85
	* @NoAdminRequired
86
	* @NoCSRFRequired
87
	* @CORS
88
	* @param string $token
89
	* @return DataResponse
90
	*/
91
	public function get($token) {
92
		try {
93
			return new DataResponse(['share' => $this->shareService->get($token)], Http::STATUS_OK);
94
		} catch (DoesNotExistException $e) {
95
			return new DataResponse(['error' => 'Token ' . $token . ' not found'], Http::STATUS_NOT_FOUND);
96
		} catch (NotAuthorizedException $e) {
97
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
98
		}
99
	}
100
101
	/**
102
	 * Write a new share to the db and returns the new share as array
103
	 * @NoAdminRequired
104
	 * @CORS
105
	 * @NoCSRFRequired
106
	 * @param int $pollId
107
	 * @param string $type
108
	 * @param string $userId
109
	 * @param string $userEmail
110
	 * @return DataResponse
111
	 */
112
	public function add($pollId, $type, $userId = '', $userEmail = '') {
113
		try {
114
			return new DataResponse(['share' => $this->shareService->add($pollId, $type, $userId, $userEmail)], Http::STATUS_CREATED);
115
		} catch (\Exception $e) {
116
			return new DataResponse(['error' => $e], Http::STATUS_CONFLICT);
117
		} catch (NotAuthorizedException $e) {
118
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
119
		}
120
121
	}
122
123
	/**
124
	 * delete share
125
	 * @NoAdminRequired
126
	 * @CORS
127
	 * @NoCSRFRequired
128
	 * @param string $token
129
	 * @return DataResponse
130
	 */
131
132
	public function delete($token) {
133
		try {
134
			return new DataResponse(['share' => $this->shareService->remove($token)], Http::STATUS_OK);
135
		} catch (NotAuthorizedException $e) {
136
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
137
		} catch (Exception $e) {
138
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
139
		}
140
	}
141
}
142