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

ShareController::createPersonalShare()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 4
eloc 8
c 2
b 1
f 0
nc 4
nop 2
dl 0
loc 11
ccs 0
cts 9
cp 0
crap 20
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\ILogger;
34
use OCP\AppFramework\Controller;
35
use OCP\AppFramework\Http;
36
use OCP\AppFramework\Http\DataResponse;
37
38
39
40
use OCA\Polls\Model\Acl;
41
use OCA\Polls\Service\ShareService;
42
use OCA\Polls\Service\MailService;
43
44
class ShareController extends Controller {
45
46
	private $logger;
47
	private $shareService;
48
	private $mailService;
49
	private $userId;
50
51
	/**
52
	 * ShareController constructor.
53
	 * @param string $appName
54
	 * @param string $userId
55
	 * @param IRequest $request
56
	 * @param ILogger $logger
57
	 * @param MailService $mailService
58
	 * @param ShareService $shareService
59
	 */
60
	public function __construct(
61
		string $appName,
62
		$userId,
63
		IRequest $request,
64
		ILogger $logger,
65
		MailService $mailService,
66
		ShareService $shareService
67
	) {
68
		parent::__construct($appName, $request);
69
		$this->logger = $logger;
70
		$this->userId = $userId;
71
		$this->shareService = $shareService;
72
		$this->mailService = $mailService;
73
	}
74
75
	/**
76
	 * Write a new share to the db and returns the new share as array
77
	 * @NoAdminRequired
78
	 * @NoCSRFRequired
79
	 * @param int $pollId
80
	 * @param Array $share
81
	 * @return DataResponse
82
	 */
83
	 public function add($pollId, $type, $userId = '', $userEmail = '') {
84
 		try {
85
 			return new DataResponse(['share' => $this->shareService->add($pollId, $type, $userId, $userEmail)], Http::STATUS_CREATED);
86
		} catch (NotAuthorizedException $e) {
87
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
88
		} catch (\Exception $e) {
89
			return new DataResponse($e, Http::STATUS_CONFLICT);
90
		}
91
	}
92
93
	/**
94
	 * createPersonalShare
95
	 * Write a new share to the db and returns the new share as array
96
	 * @NoAdminRequired
97
	 * @PublicPage
98
	 * @NoCSRFRequired
99
	 * @param string $token
100
	 * @param string $userName
101
	 * @return DataResponse
102
	 */
103
	public function createPersonalShare($token, $userName) {
104
105
		try {
106
			return new DataResponse($this->shareService->createPersonalShare($token, $userName), Http::STATUS_CREATED);
107
		} catch (NotAuthorizedException $e) {
108
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
109
		} catch (InvalidUsername $e) {
110
			return new DataResponse(['error' => $userName . ' is not valid'], Http::STATUS_CONFLICT);
111
		} catch (DoesNotExistException $e) {
112
			// return forbidden in all not catched error cases
113
			return new DataResponse($e, Http::STATUS_FORBIDDEN);
114
		}
115
	}
116
117
	/**
118
	 * SendInvitation
119
	 * Sent invitation mails for a share
120
	 * @NoAdminRequired
121
	 * @PublicPage
122
	 * @NoCSRFRequired
123
	 * @param string $token
124
	 * @return DataResponse
125
	 */
126
	public function sendInvitation($token) {
127
		try {
128
			$sentResult = $this->mailService->sendInvitationMail($token);
129
			$share = $this->shareService->get($token);
130
			return new DataResponse(['share' => $share, 'sentResult' => $sentResult], Http::STATUS_OK);
131
		} catch (Exception $e) {
132
			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

132
			return new DataResponse(['error' => $e->getMessage()], $e->/** @scrutinizer ignore-call */ getStatus());
Loading history...
133
		}
134
	}
135
136
	/**
137
	 * remove
138
	 * remove share
139
	 * @NoAdminRequired
140
	 * @NoCSRFRequired
141
	 * @param Share $share
142
	 * @return DataResponse
143
	 */
144
145
	public function delete($share) {
146
		try {
147
			return new DataResponse(array(
148
				'action' => 'deleted',
149
				'shareId' => $this->shareService->remove($share['token'])->getId()
150
			), Http::STATUS_OK);
151
		} catch (NotAuthorizedException $e) {
152
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
153
		} catch (Exception $e) {
154
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
155
		}
156
	}
157
}
158