Completed
Pull Request — master (#1038)
by René
06:51 queued 55s
created

ShareController::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 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 10
cc 3
nc 3
nop 4
crap 12
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\Controller;
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 ShareController extends Controller {
41
42
	/** @var ShareService */
43
	private $shareService;
44
45
	/** @var MailService */
46
	private $mailService;
47
48
	/**
49
	 * ShareController constructor.
50
	 * @param string $appName
51
	 * @param IRequest $request
52
	 * @param MailService $mailService
53
	 * @param ShareService $shareService
54
	 */
55
	public function __construct(
56
		string $appName,
57
		IRequest $request,
58
		MailService $mailService,
59
		ShareService $shareService
60
	) {
61
		parent::__construct($appName, $request);
62
		$this->shareService = $shareService;
63
		$this->mailService = $mailService;
64
	}
65
66
	/**
67
	 * Add share
68
	 * @NoAdminRequired
69
	 * @param int $pollId
70
	 * @param int $pollId
71
	 * @param string $type
72
	 * @param string $userId
73
	 * @param string $userEmail
74
	 * @return DataResponse
75
	 */
76
	 public function add($pollId, $type, $userId = '', $userEmail = '') {
77
 		try {
78
 			return new DataResponse(['share' => $this->shareService->add($pollId, $type, $userId, $userEmail)], Http::STATUS_CREATED);
79
		} catch (NotAuthorizedException $e) {
80
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
81
		} catch (\Exception $e) {
82
			return new DataResponse($e, Http::STATUS_CONFLICT);
83
		}
84
	}
85
86
	/**
87
	 * Create a personal share from a public share
88
	 * or update an email share with the username
89
	 * @NoAdminRequired
90
	 * @PublicPage
91
	 * @param string $token
92
	 * @param string $userName
93
	 * @return DataResponse
94
	 */
95
	public function personal($token, $userName) {
96
97
		try {
98
			return new DataResponse($this->shareService->personal($token, $userName), Http::STATUS_CREATED);
99
		} catch (NotAuthorizedException $e) {
100
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
101
		} catch (InvalidUsername $e) {
102
			return new DataResponse(['error' => $userName . ' is not valid'], Http::STATUS_CONFLICT);
103
		} catch (DoesNotExistException $e) {
104
			// return forbidden in all not catched error cases
105
			return new DataResponse($e, Http::STATUS_FORBIDDEN);
106
		}
107
	}
108
109
	/**
110
	 * Delete share
111
	 * @NoAdminRequired
112
	 * @param string $token
113
	 * @return DataResponse
114
	 */
115
116
	public function delete($token) {
117
		try {
118
			return new DataResponse($this->shareService->delete($token), Http::STATUS_OK);
119
		} catch (NotAuthorizedException $e) {
120
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
121
		} catch (Exception $e) {
122
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
123
		}
124
	}
125
126
	/**
127
	 * Sent invitation mails for a share
128
	 * @NoAdminRequired
129
	 * @PublicPage
130
	 * @param string $token
131
	 * @return DataResponse
132
	 */
133
	public function sendInvitation($token) {
134
		try {
135
			$sentResult = $this->mailService->sendInvitationMail($token);
136
			$share = $this->shareService->get($token);
137
			return new DataResponse(['share' => $share, 'sentResult' => $sentResult], Http::STATUS_OK);
138
		} catch (Exception $e) {
139
			return new DataResponse(['error' => $e], Http::STATUS_CONFLICT);
140
		}
141
	}
142
}
143