ShareApiController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
eloc 26
c 0
b 0
f 0
dl 0
loc 85
rs 10
ccs 0
cts 35
cp 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 3 1
A __construct() 0 13 1
A sendInvitation() 0 7 1
A get() 0 3 1
A list() 0 3 1
A add() 0 3 1
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 OCP\IRequest;
27
use OCP\AppFramework\ApiController;
28
use OCP\AppFramework\Http\DataResponse;
29
30
use OCA\Polls\Service\ShareService;
31
use OCA\Polls\Service\MailService;
32
33
class ShareApiController extends ApiController {
34
	use ResponseHandle;
35
36
	/** @var ShareService */
37
	private $shareService;
38
39
	/** @var MailService */
40
	private $mailService;
41
42
	public function __construct(
43
		string $appName,
44
		IRequest $request,
45
		MailService $mailService,
46
		ShareService $shareService
47
	) {
48
		parent::__construct($appName,
49
			$request,
50
			'POST, PUT, GET, DELETE',
51
			'Authorization, Content-Type, Accept',
52
			1728000);
53
		$this->shareService = $shareService;
54
		$this->mailService = $mailService;
55
	}
56
57
	/**
58
	 * Read all shares of a poll based on the poll id and return list as array
59
	 * @NoAdminRequired
60
	 * @CORS
61
	 * @NoCSRFRequired
62
	 */
63
	public function list(int $pollId): DataResponse {
64
		return $this->response(function () use ($pollId) {
65
			return ['shares' => $this->shareService->list($pollId)];
66
		});
67
	}
68
69
	/**
70
	 * Get share by token
71
	 * @NoAdminRequired
72
	 * @CORS
73
	 * @NoCSRFRequired
74
	 */
75
	public function get(string $token): DataResponse {
76
		return $this->response(function () use ($token) {
77
			return ['share' => $this->shareService->get($token)];
78
		});
79
	}
80
81
	/**
82
	 * Add share
83
	 * @NoAdminRequired
84
	 * @CORS
85
	 * @NoCSRFRequired
86
	 */
87
	public function add(int $pollId, string $type, string $userId = ''): DataResponse {
88
		return $this->responseCreate(function () use ($pollId, $type, $userId) {
89
			return ['share' => $this->shareService->add($pollId, $type, $userId)];
90
		});
91
	}
92
93
	/**
94
	 * Delete share
95
	 * @NoAdminRequired
96
	 * @CORS
97
	 * @NoCSRFRequired
98
	 */
99
	public function delete(string $token): DataResponse {
100
		return $this->responseDeleteTolerant(function () use ($token) {
101
			return ['share' => $this->shareService->delete($token)];
102
		});
103
	}
104
105
	/**
106
	 * Sent invitation mails for a share
107
	 * @NoAdminRequired
108
	 * @CORS
109
	 * @NoCSRFRequired
110
	 */
111
	public function sendInvitation(string $token): DataResponse {
112
		return $this->response(function () use ($token) {
113
			$sentResult = $this->mailService->sendInvitation($token);
114
			$share = $this->shareService->get($token);
115
			return [
116
				'share' => $share,
117
				'sentResult' => $sentResult
118
			];
119
		});
120
	}
121
}
122