Completed
Push — master ( 2108fc...d9caec )
by Maxence
13s queued 10s
created

SharesController::checkContactMail()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Controller;
28
29
use daita\MySmallPhpTools\Traits\TStringTools;
30
use Exception;
31
use OCA\Circles\Db\SharesRequest;
32
use OCA\Circles\Db\TokensRequest;
33
use OCA\Circles\Exceptions\MemberDoesNotExistException;
34
use OCA\Circles\Model\Member;
35
use OCA\Circles\Model\SharesToken;
36
use OCA\Circles\Model\SharingFrame;
37
use OCA\Circles\Service\BroadcastService;
38
use OCA\Circles\Service\ConfigService;
39
use OCA\Circles\Service\MembersService;
40
use OCA\Circles\Service\MiscService;
41
use OCA\Circles\Service\SharingFrameService;
42
use OCP\AppFramework\Controller;
43
use OCP\AppFramework\Http\DataResponse;
44
use OCP\IRequest;
45
use OCP\IURLGenerator;
46
47
48
/**
49
 * Class SharesController
50
 *
51
 * @package OCA\Circles\Controller
52
 */
53
class SharesController extends Controller {
54
55
56
	use TStringTools;
57
58
59
	/** @var TokensRequest */
60
	private $tokenRequest;
61
62
	/** @var SharesRequest */
63
	private $sharesRequest;
64
65
	/** @var IURLGenerator */
66
	private $urlGenerator;
67
68
	/** @var MembersService */
69
	private $membersService;
70
71
	/** @var BroadcastService */
72
	private $broadcastService;
73
74
	/** @var SharingFrameService */
75
	private $sharingFrameService;
76
77
	/** @var ConfigService */
78
	private $configService;
79
80
	/** @var MiscService */
81
	private $miscService;
82
83
84
	/**
85
	 * SharesController constructor.
86
	 *
87
	 * @param $appName
88
	 * @param IRequest $request
89
	 * @param TokensRequest $tokenRequest
90
	 * @param SharesRequest $sharesRequest
91
	 * @param IURLGenerator $urlGenerator
92
	 * @param MembersService $membersService
93
	 * @param BroadcastService $broadcastService
94
	 * @param SharingFrameService $sharingFrameService
95
	 * @param ConfigService $configService
96
	 * @param MiscService $miscService
97
	 */
98 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
		$appName, IRequest $request, TokensRequest $tokenRequest, SharesRequest $sharesRequest,
100
		IUrlGenerator $urlGenerator, MembersService $membersService,
101
		BroadcastService $broadcastService,
102
		SharingFrameService $sharingFrameService, ConfigService $configService,
103
		MiscService $miscService
104
	) {
105
		parent::__construct($appName, $request);
106
107
		$this->tokenRequest = $tokenRequest;
108
		$this->sharesRequest = $sharesRequest;
109
		$this->urlGenerator = $urlGenerator;
110
		$this->membersService = $membersService;
111
		$this->broadcastService = $broadcastService;
112
		$this->sharingFrameService = $sharingFrameService;
113
		$this->configService = $configService;
114
		$this->miscService = $miscService;
115
	}
116
117
118
	/**
119
	 * Called by the JavaScript API when creating a new Share item that will be
120
	 * broadcasted to the circle itself, and any other circle linked to it.
121
	 *
122
	 * @NoAdminRequired
123
	 * @NoSubAdminRequired
124
	 *
125
	 * @param string $circleUniqueId
126
	 * @param string $source
127
	 * @param string $type
128
	 * @param array $payload
129
	 *
130
	 * @return DataResponse
131
	 */
132
	public function create($circleUniqueId, $source, $type, $payload) {
133
134
		try {
135
			$share = new SharingFrame($source, $type);
136
			$share->setPayload($payload);
137
138
			$this->sharingFrameService->createFrame($circleUniqueId, $share);
139
		} catch (\Exception $e) {
140
			return $this->fail(
141
				[
142
					'circle_id' => $circleUniqueId,
143
					'source'    => $source,
144
					'type'      => $type,
145
					'payload'   => $payload,
146
					'error'     => $e->getMessage()
147
				]
148
			);
149
		}
150
151
		return $this->success(
152
			[
153
				'circle_id' => $circleUniqueId,
154
				'source'    => $source,
155
				'type'      => $type,
156
				'payload'   => $payload
157
			]
158
		);
159
	}
160
161
162
	/**
163
	 * initShareDelivery()
164
	 *
165
	 * Note: this function will close the request mid-run from the client but will still
166
	 * running its process.
167
	 *
168
	 * Called by locally, the function will get the SharingFrame by its uniqueId from the database.
169
	 * After closing the socket, will broadcast the Frame locally and - if Federated Shares are
170
	 * enable - will deliver it to each remote circles linked to the circle the Payload belongs to.
171
	 *
172
	 * A status response is sent to free the client process before starting to broadcast the item
173
	 * to other federated links.
174
	 *
175
	 * @PublicPage
176
	 * @NoCSRFRequired
177
	 *
178
	 * @param string $circleId
179
	 * @param string $frameId
180
	 *
181
	 * @return DataResponse
182
	 */
183
	public function initShareDelivery($circleId, $frameId) {
184
185
		try {
186
			$frame = $this->sharingFrameService->getFrameFromUniqueId($circleId, $frameId);
187
		} catch (Exception $e) {
188
			return $this->fail($e->getMessage());
189
		}
190
191
		// We don't want to keep the connection up
192
		$this->miscService->asyncAndLeaveClientOutOfThis('done');
193
194
		$this->broadcastService->broadcastFrame($frame);
0 ignored issues
show
Bug introduced by
It seems like $frame defined by $this->sharingFrameServi...Id($circleId, $frameId) on line 186 can be null; however, OCA\Circles\Service\Broa...rvice::broadcastFrame() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
195
196
		// TODO - do not update cloudId to avoid duplicate, use it's own field and keep cloudId
197
		$this->sharingFrameService->updateFrameWithCloudId($frame);
0 ignored issues
show
Bug introduced by
It seems like $frame defined by $this->sharingFrameServi...Id($circleId, $frameId) on line 186 can be null; however, OCA\Circles\Service\Shar...pdateFrameWithCloudId() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
198
		if ($this->configService->isFederatedCirclesAllowed()) {
199
			$this->sharingFrameService->forwardSharingFrame($frame);
0 ignored issues
show
Bug introduced by
It seems like $frame defined by $this->sharingFrameServi...Id($circleId, $frameId) on line 186 can be null; however, OCA\Circles\Service\Shar...::forwardSharingFrame() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
200
		}
201
202
		exit();
203
	}
204
205
206
	/**
207
	 * @param SharesToken $shareToken
208
	 *
209
	 * @throws MemberDoesNotExistException
210
	 */
211
	private function checkContactMail(SharesToken $shareToken) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
212
		try {
213
			$this->membersService->getMember(
214
				$shareToken->getCircleId(), $shareToken->getUserId(), Member::TYPE_MAIL, true
215
			);
216
217
			return;
218
		} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
219
		}
220
221
		try {
222
			$this->membersService->getMember(
223
				$shareToken->getCircleId(), $shareToken->getUserId(), Member::TYPE_MAIL, true
224
			);
225
226
			return;
227
		} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
228
		}
229
230
		throw new MemberDoesNotExistException();
231
	}
232
}
233
234