Completed
Push — master ( 5d3621...1755ef )
by Maxence
02:25
created

ShareWrapperService::getShareByToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2021
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles\Service;
33
34
35
use OCA\Circles\Db\ShareWrapperRequest;
36
use OCA\Circles\Exceptions\RequestBuilderException;
37
use OCA\Circles\Exceptions\ShareWrapperNotFoundException;
38
use OCA\Circles\Model\FederatedUser;
39
use OCA\Circles\Model\ShareWrapper;
40
use OCP\Files\NotFoundException;
41
use OCP\Share\IShare;
42
43
44
/**
45
 * Class ShareWrapperService
46
 *
47
 * @package OCA\Circles\Service
48
 */
49
class ShareWrapperService {
50
51
52
	/** @var ShareWrapperRequest */
53
	private $shareWrapperRequest;
54
55
56
	/**
57
	 * ShareWrapperService constructor.
58
	 *
59
	 * @param ShareWrapperRequest $shareWrapperRequest
60
	 */
61
	public function __construct(ShareWrapperRequest $shareWrapperRequest) {
62
		$this->shareWrapperRequest = $shareWrapperRequest;
63
	}
64
65
66
	/**
67
	 * @param $singleId
68
	 * @param $nodeId
69
	 *
70
	 * @return ShareWrapper
71
	 * @throws ShareWrapperNotFoundException
72
	 */
73
	public function searchShare(string $singleId, int $nodeId): ShareWrapper {
74
		return $this->shareWrapperRequest->searchShare($singleId, $nodeId);
75
	}
76
77
78
	/**
79
	 * @param IShare $share
80
	 *
81
	 * @throws NotFoundException
82
	 */
83
	public function save(IShare $share): void {
84
		$this->shareWrapperRequest->save($share);
85
	}
86
87
88
	/**
89
	 * @param ShareWrapper $shareWrapper
90
	 */
91
	public function update(ShareWrapper $shareWrapper): void {
92
		$this->shareWrapperRequest->update($shareWrapper);
93
	}
94
95
96
	/**
97
	 * @param ShareWrapper $shareWrapper
98
	 */
99
	public function delete(ShareWrapper $shareWrapper): void {
100
		$this->shareWrapperRequest->delete((int)$shareWrapper->getId());
101
	}
102
103
104
	/**
105
	 * @param string $circleId
106
	 * @param FederatedUser|null $shareRecipient
107
	 * @param FederatedUser|null $shareInitiator
108
	 *
109
	 * @return ShareWrapper[]
110
	 * @throws RequestBuilderException
111
	 */
112
	public function getSharesToCircle(
113
		string $circleId,
114
		?FederatedUser $shareRecipient = null,
115
		?FederatedUser $shareInitiator = null,
116
		bool $completeDetails = false
117
	): array {
118
		return $this->shareWrapperRequest->getSharesToCircle(
119
			$circleId,
120
			$shareRecipient,
121
			$shareInitiator,
122
			$completeDetails
123
		);
124
	}
125
126
127
	/**
128
	 * @param int $shareId
129
	 * @param FederatedUser|null $federatedUser
130
	 *
131
	 * @return ShareWrapper
132
	 * @throws ShareWrapperNotFoundException
133
	 * @throws RequestBuilderException
134
	 */
135
	public function getShareById(int $shareId, ?FederatedUser $federatedUser = null): ShareWrapper {
136
		return $this->shareWrapperRequest->getShareById($shareId, $federatedUser);
137
	}
138
139
140
	/**
141
	 * @param int $fileId
142
	 *
143
	 * @return ShareWrapper[]
144
	 * @throws RequestBuilderException
145
	 */
146
	public function getSharesByFileId(int $fileId, bool $getData = false): array {
147
		return $this->shareWrapperRequest->getSharesByFileId($fileId, $getData);
148
	}
149
150
	/**
151
	 * @param string $token
152
	 * @param FederatedUser|null $federatedUser
153
	 *
154
	 * @return ShareWrapper
155
	 * @throws RequestBuilderException
156
	 * @throws ShareWrapperNotFoundException
157
	 */
158
	public function getShareByToken(string $token, ?FederatedUser $federatedUser = null): ShareWrapper {
159
		return $this->shareWrapperRequest->getShareByToken($token, $federatedUser);
160
	}
161
162
163
	/**
164
	 * @param FederatedUser $federatedUser
165
	 * @param int $nodeId
166
	 * @param int $offset
167
	 * @param int $limit
168
	 * @param bool $getData
169
	 *
170
	 * @return ShareWrapper[]
171
	 * @throws RequestBuilderException
172
	 */
173
	public function getSharedWith(
174
		FederatedUser $federatedUser,
175
		int $nodeId,
176
		int $offset,
177
		int $limit,
178
		bool $getData = false
179
	): array {
180
		return $this->shareWrapperRequest->getSharedWith($federatedUser, $nodeId, $offset, $limit, $getData);
181
	}
182
183
184
	/**
185
	 * @param FederatedUser $federatedUser
186
	 * @param int $nodeId
187
	 * @param bool $reshares
188
	 * @param int $offset
189
	 * @param int $limit
190
	 * @param bool $getData
191
	 *
192
	 * @return ShareWrapper[]
193
	 * @throws RequestBuilderException
194
	 */
195
	public function getSharesBy(
196
		FederatedUser $federatedUser,
197
		int $nodeId,
198
		bool $reshares,
199
		int $offset,
200
		int $limit,
201
		bool $getData = false,
202
		bool $completeDetails = false
203
	): array {
204
		return $this->shareWrapperRequest->getSharesBy(
205
			$federatedUser, $nodeId, $reshares, $offset, $limit, $getData, $completeDetails
206
		);
207
	}
208
209
210
	/**
211
	 * @param FederatedUser $federatedUser
212
	 * @param int $nodeId
213
	 * @param bool $reshares
214
	 *
215
	 * @return ShareWrapper[]
216
	 * @throws RequestBuilderException
217
	 */
218
	public function getSharesInFolder(FederatedUser $federatedUser, int $nodeId, bool $reshares): array {
219
		return $this->shareWrapperRequest->getSharesInFolder($federatedUser, $nodeId, $reshares);
220
	}
221
222
223
	/**
224
	 * @param FederatedUser $federatedUser
225
	 * @param IShare $share
226
	 *
227
	 * @return ShareWrapper
228
	 * @throws NotFoundException
229
	 * @throws ShareWrapperNotFoundException
230
	 */
231
	public function getChild(IShare $share, FederatedUser $federatedUser): ShareWrapper {
232
		try {
233
			return $this->shareWrapperRequest->getChild($federatedUser, (int)$share->getId());
234
		} catch (ShareWrapperNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
235
		}
236
237
		return $this->createChild($share, $federatedUser);
238
	}
239
240
241
	/**
242
	 * @param FederatedUser $federatedUser
243
	 * @param IShare $share
244
	 *
245
	 * @return ShareWrapper
246
	 * @throws ShareWrapperNotFoundException
247
	 * @throws NotFoundException
248
	 */
249
	private function createChild(IShare $share, FederatedUser $federatedUser): ShareWrapper {
250
		$share->setSharedWith($federatedUser->getSingleId());
251
		$childId = $this->shareWrapperRequest->save($share, (int)$share->getId());
252
253
		return $this->getShareById($childId, $federatedUser);
254
	}
255
256
}
257
258