Completed
Push — master ( dc9b8a...1415c3 )
by Maxence
02:30
created

CirclesRequest::getLinkFromToken()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 19
loc 19
rs 9.4285
cc 3
eloc 12
nc 2
nop 2
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
28
namespace OCA\Circles\Db;
29
30
31
use OC\L10N\L10N;
32
use OCA\Circles\Exceptions\CircleDoesNotExistException;
33
use OCA\Circles\Exceptions\FederatedLinkDoesNotExistException;
34
use OCA\Circles\Model\Circle;
35
use OCA\Circles\Model\FederatedLink;
36
use OCA\Circles\Model\Member;
37
use OCA\Circles\Model\SharingFrame;
38
use OCA\Circles\Service\MiscService;
39
use OCP\IDBConnection;
40
41
class CirclesRequest extends CirclesRequestBuilder {
42
43
44
	/**
45
	 * forceGetCircle();
46
	 *
47
	 * returns data of a circle from its Id.
48
	 *
49
	 * WARNING: This function does not filters data regarding the current user/viewer.
50
	 *          In case of interaction with users, Please use getGroup() instead.
51
	 *
52
	 * @param int $circleId
53
	 *
54
	 * @return Circle
0 ignored issues
show
Documentation introduced by
Should the return type not be Circle|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
55
	 * @throws CircleDoesNotExistException
56
	 */
57 View Code Duplication
	public function forceGetCircle($circleId) {
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...
58
		$qb = $this->getCirclesSelectSql();
59
		$this->limitToId($qb, $circleId);
60
		$cursor = $qb->execute();
61
62
		$data = $cursor->fetch();
63
		if ($data === false || $data === null) {
64
			throw new CircleDoesNotExistException($this->l10n->t('Circle not found'));
65
		}
66
67
		$entry = $this->parseCirclesSelectSql($data);
68
69
		return $entry;
70
	}
71
72
73
	/**
74
	 *
75
	 * @param int $circleId
76
	 * @param string $userId
77
	 *
78
	 * @return Circle
0 ignored issues
show
Documentation introduced by
Should the return type not be Circle|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
79
	 * @throws CircleDoesNotExistException
80
	 */
81 View Code Duplication
	public function getCircle($circleId, $userId) {
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...
82
		$qb = $this->getCirclesSelectSql();
83
84
		$this->limitToId($qb, $circleId);
85
		$this->leftJoinUserIdAsViewer($qb, $userId);
86
87
88
//		$this->leftjoinOwner($qb);
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
89
//		$this->buildWithMemberLevel($qb, 'u.level', $level);
90
//		$this->buildWithCircleId($qb, 'c.id', $circleId);
91
//		$this->buildWithOrXTypes($qb, $userId, $type, $name, $circleId);
92
93
		$cursor = $qb->execute();
94
		$data = $cursor->fetch();
95
		if ($data === false || $data === null) {
96
			throw new CircleDoesNotExistException(
97
				$this->l10n->t('Circle not found')
98
			);
99
		}
100
101
		$circle = $this->parseCirclesSelectSql($data);
102
103
//		if ($circle->getUser()->getLevel)
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
104
		return $circle;
105
	}
106
107
108
	/**
109
	 * saveFrame()
110
	 *
111
	 * Insert a new entry in the database to save the SharingFrame.
112
	 *
113
	 * @param SharingFrame $frame
114
	 */
115 View Code Duplication
	public function saveFrame(SharingFrame $frame) {
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...
116
117
		$qb = $this->getSharesInsertSql();
118
		$qb->setValue('circle_id', $qb->createNamedParameter($frame->getCircleId()))
119
		   ->setValue('source', $qb->createNamedParameter($frame->getSource()))
120
		   ->setValue('type', $qb->createNamedParameter($frame->getType()))
121
		   ->setValue('headers', $qb->createNamedParameter($frame->getHeaders(true)))
122
		   ->setValue('author', $qb->createNamedParameter($frame->getAuthor()))
123
		   ->setValue('cloud_id', $qb->createNamedParameter($frame->getCloudId()))
124
		   ->setValue('unique_id', $qb->createNamedParameter($frame->getUniqueId()))
125
		   ->setValue('payload', $qb->createNamedParameter($frame->getPayload(true)));
126
127
		$qb->execute();
128
	}
129
130
131 View Code Duplication
	public function updateFrame(SharingFrame $frame) {
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...
132
		$qb = $this->getSharesUpdateSql($frame->getUniqueId());
133
		$qb->set('circle_id', $qb->createNamedParameter($frame->getCircleId()))
134
		   ->set('source', $qb->createNamedParameter($frame->getSource()))
135
		   ->set('type', $qb->createNamedParameter($frame->getType()))
136
		   ->set('headers', $qb->createNamedParameter($frame->getHeaders(true)))
137
		   ->set('author', $qb->createNamedParameter($frame->getAuthor()))
138
		   ->set('cloud_id', $qb->createNamedParameter($frame->getCloudId()))
139
		   ->set('unique_id', $qb->createNamedParameter($frame->getUniqueId()))
140
		   ->set('payload', $qb->createNamedParameter($frame->getPayload(true)));
141
142
		$qb->execute();
143
	}
144
145
146
	public function updateCircle(Circle $circle) {
147
		$qb = $this->getCirclesUpdateSql($circle->getId());
148
		$qb->set('name', $qb->createNamedParameter($circle->getName()))
149
		   ->set('description', $qb->createNamedParameter($circle->getDescription()))
150
		   ->set('settings', $qb->createNamedParameter($circle->getSettings(true)));
151
152
		$qb->execute();
153
	}
154
155
156
	/**
157
	 * @param string $uniqueId
158
	 *
159
	 * @return Circle
0 ignored issues
show
Documentation introduced by
Should the return type not be Circle|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
160
	 * @throws CircleDoesNotExistException
161
	 */
162
	public function getCircleFromUniqueId($uniqueId) {
163
		$qb = $this->getCirclesSelectSql();
164
		$this->limitToUniqueId($qb, (string)$uniqueId);
165
166
		$cursor = $qb->execute();
167
		$data = $cursor->fetch();
168
169
		if ($data === false) {
170
			throw new CircleDoesNotExistException(
171
				$this->l10n->t('Circle not found')
172
			);
173
		}
174
		$entry = $this->parseCirclesSelectSql($data);
175
		$cursor->closeCursor();
176
177
		return $entry;
178
	}
179
180
181
	/**
182
	 * @param int $circleId
183
	 * @param string $uniqueId
184
	 *
185
	 * @return SharingFrame
0 ignored issues
show
Documentation introduced by
Should the return type not be SharingFrame|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
186
	 */
187
	public function getFrame($circleId, $uniqueId) {
188
		$qb = $this->getSharesSelectSql();
189
		$this->limitToUniqueId($qb, (string)$uniqueId);
190
		$this->limitToCircleId($qb, (int)$circleId);
191
192
		$cursor = $qb->execute();
193
		$data = $cursor->fetch();
194
		$entry = $this->parseSharesSelectSql($data);
195
196
		return $entry;
197
	}
198
199
200
	/**
201
	 * return the FederatedLink identified by a remote Circle UniqueId and the Token of the link
202
	 *
203
	 * @param string $token
204
	 * @param string $uniqueId
205
	 *
206
	 * @return FederatedLink
0 ignored issues
show
Documentation introduced by
Should the return type not be FederatedLink|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
207
	 * @throws FederatedLinkDoesNotExistException
208
	 */
209 View Code Duplication
	public function getLinkFromToken($token, $uniqueId) {
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...
210
		$qb = $this->getLinksSelectSql();
211
		$this->limitToUniqueId($qb, (string)$uniqueId);
212
		$this->limitToToken($qb, (string)$token);
213
214
		$cursor = $qb->execute();
215
		$data = $cursor->fetch();
216
217
		if ($data === false || $data === null) {
218
			throw new FederatedLinkDoesNotExistException(
219
				$this->l10n->t('Federated Link not found')
220
			);
221
		}
222
223
		$entry = $this->parseLinksSelectSql($data);
224
		$cursor->closeCursor();
225
226
		return $entry;
227
	}
228
229
230
	/**
231
	 * return the FederatedLink identified by a its Id
232
	 *
233
	 * @param int $linkId
234
	 *
235
	 * @return FederatedLink
0 ignored issues
show
Documentation introduced by
Should the return type not be FederatedLink|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
236
	 * @throws FederatedLinkDoesNotExistException
237
	 */
238 View Code Duplication
	public function getLinkFromId($linkId) {
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...
239
		$qb = $this->getLinksSelectSql();
240
		$this->limitToId($qb, (string)$linkId);
241
242
		$cursor = $qb->execute();
243
		$data = $cursor->fetch();
244
		$cursor->closeCursor();
245
246
		if ($data === false || $data === null) {
247
			throw new FederatedLinkDoesNotExistException(
248
				$this->l10n->t('Federated Link not found')
249
			);
250
		}
251
252
		$entry = $this->parseLinksSelectSql($data);
253
254
		return $entry;
255
	}
256
257
258
	/**
259
	 * returns all FederatedLink from a circle
260
	 *
261
	 * @param int $circleId
262
	 *
263
	 * @return FederatedLink[]
264
	 */
265
	public function getLinksFromCircle($circleId) {
266
		$qb = $this->getLinksSelectSql();
267
		$this->limitToCircleId($qb, $circleId);
268
269
		$links = [];
270
		$cursor = $qb->execute();
271
		while ($data = $cursor->fetch()) {
272
			$link = $this->parseLinksSelectSql($data);
273
			if ($link !== null) {
274
				$links[] = $link;
275
			}
276
		}
277
		$cursor->closeCursor();
278
279
		return $links;
280
	}
281
282
	/**
283
	 * @param integer $circleId
284
	 * @param int $level
285
	 *
286
	 * @return Member[]
287
	 */
288
	public function getMembers($circleId, $level = Member::LEVEL_MEMBER) {
289
		$qb = $this->getMembersSelectSql();
290
		$this->limitToLevel($qb, $level);
291
292
		$this->joinCircles($qb, 'm.circle_id');
293
		$this->limitToCircleId($qb, $circleId);
294
295
		$qb->selectAlias('c.name', 'circle_name');
296
297
		$users = [];
298
		$cursor = $qb->execute();
299
		while ($data = $cursor->fetch()) {
300
			$member = $this->parseMembersSelectSql($data);
301
			if ($member !== null) {
302
				$users[] = $member;
303
			}
304
		}
305
		$cursor->closeCursor();
306
307
		return $users;
308
	}
309
310
311
}