Completed
Push — master ( 3c965b...3f9ba1 )
by René
16s queued 13s
created

ShareApiController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 15
eloc 39
c 1
b 0
f 1
dl 0
loc 121
ccs 0
cts 55
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 7 3
A __construct() 0 13 1
A sendInvitation() 0 5 2
A get() 0 7 3
A list() 0 7 3
A add() 0 7 3
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
use OCA\Polls\Service\MailService;
39
40
class ShareApiController extends ApiController {
41
42
	private $shareService;
43
	private $mailService;
44
45
	/**
46
	 * ShareController constructor.
47
	 * @param string $appName
48
	 * @param string $userId
49
	 * @param IRequest $request
50
	 * @param MailService $mailService
51
	 * @param ShareService $shareService
52
	 */
53
	public function __construct(
54
		string $appName,
55
		IRequest $request,
56
		MailService $mailService,
57
		ShareService $shareService
58
	) {
59
		parent::__construct($appName,
60
			$request,
61
			'POST, PUT, GET, DELETE',
62
            'Authorization, Content-Type, Accept',
63
            1728000);
64
		$this->shareService = $shareService;
65
		$this->mailService = $mailService;
66
	}
67
68
	/**
69
	 * list
70
	 * Read all shares of a poll based on the poll id and return list as array
71
	 * @NoAdminRequired
72
	 * @CORS
73
	 * @NoCSRFRequired
74
	 * @param integer $pollId
75
	 * @return DataResponse
76
	 */
77
	public function list($pollId) {
78
		try {
79
			return new DataResponse(['shares' => $this->shareService->list($pollId)], Http::STATUS_OK);
80
		} catch (DoesNotExistException $e) {
81
			return new DataResponse(['error' => 'No shares for poll with id ' . $pollId . ' not found'], Http::STATUS_NOT_FOUND);
82
		} catch (NotAuthorizedException $e) {
83
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
84
		}
85
	}
86
87
	/**
88
	* get share by token
89
	* Get pollId by token
90
	* @NoAdminRequired
91
	* @NoCSRFRequired
92
	* @CORS
93
	* @param string $token
94
	* @return DataResponse
95
	*/
96
	public function get($token) {
97
		try {
98
			return new DataResponse(['share' => $this->shareService->get($token)], Http::STATUS_OK);
99
		} catch (DoesNotExistException $e) {
100
			return new DataResponse(['error' => 'Token ' . $token . ' not found'], Http::STATUS_NOT_FOUND);
101
		} catch (NotAuthorizedException $e) {
102
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
103
		}
104
	}
105
106
	/**
107
	 * Write a new share to the db and returns the new share as array
108
	 * @NoAdminRequired
109
	 * @CORS
110
	 * @NoCSRFRequired
111
	 * @param int $pollId
112
	 * @param string $type
113
	 * @param string $userId
114
	 * @param string $userEmail
115
	 * @return DataResponse
116
	 */
117
	public function add($pollId, $type, $userId = '', $userEmail = '') {
118
		try {
119
			return new DataResponse(['share' => $this->shareService->add($pollId, $type, $userId, $userEmail)], Http::STATUS_CREATED);
120
		} catch (\Exception $e) {
121
			return new DataResponse(['error' => $e], Http::STATUS_CONFLICT);
122
		} catch (NotAuthorizedException $e) {
123
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
124
		}
125
126
	}
127
128
	/**
129
	 * SendInvitation
130
	 * Sent invitation mails for a share
131
	 * @NoAdminRequired
132
	 * @CORS
133
	 * @NoCSRFRequired
134
	 * @param string $token
135
	 * @return DataResponse
136
	 */
137
	public function sendInvitation($token) {
138
		try {
139
			return new DataResponse($this->mailService->sendInvitationMail($token), Http::STATUS_OK);
140
		} catch (Exception $e) {
141
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
0 ignored issues
show
Bug introduced by
The method getStatus() does not exist on Exception. It seems like you code against a sub-type of Exception such as OCA\Polls\Exceptions\EmptyTitleException or OCA\Polls\Exceptions\InvalidUsername or OCA\Polls\Exceptions\InvalidPollTypeException or OCA\Polls\Exceptions\InvalidShowResultsException or OCA\Polls\Exceptions\InvalidAccessException or OCA\Polls\Exceptions\NotAuthorizedException. ( Ignorable by Annotation )

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

141
			return new DataResponse(['error' => $e->getMessage()], $e->/** @scrutinizer ignore-call */ getStatus());
Loading history...
142
		}
143
	}
144
145
	/**
146
	 * delete share
147
	 * @NoAdminRequired
148
	 * @CORS
149
	 * @NoCSRFRequired
150
	 * @param string $token
151
	 * @return DataResponse
152
	 */
153
154
	public function delete($token) {
155
		try {
156
			return new DataResponse(['share' => $this->shareService->remove($token)], Http::STATUS_OK);
157
		} catch (NotAuthorizedException $e) {
158
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
159
		} catch (Exception $e) {
160
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
161
		}
162
	}
163
}
164