Completed
Push — master ( 90f02e...53fcb4 )
by Maxence
02:59
created

CirclesRequest::getLinkFromId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 2
eloc 10
nc 2
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
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
	/** @var MiscService */
44
	private $miscService;
45
46
	/**
47
	 * CirclesRequest constructor.
48
	 *
49
	 * @param L10N $l10n
50
	 * @param IDBConnection $connection
51
	 * @param MiscService $miscService
52
	 */
53
	public function __construct(L10N $l10n, IDBConnection $connection, MiscService $miscService) {
54
		$this->l10n = $l10n;
55
		$this->dbConnection = $connection;
56
		$this->miscService = $miscService;
57
	}
58
59
60
	/**
61
	 * @param int $circleId
62
	 * @param string $userId
63
	 *
64
	 * @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...
65
	 * @throws CircleDoesNotExistException
66
	 */
67
	public function getCircleFromId($circleId, $userId = '') {
68
		$qb = $this->getCirclesSelectSql();
69
70
		$this->limitToId($qb, $circleId);
71
		if ($userId !== '') {
72
			$this->leftJoinUserIdAsMember($qb, $userId);
73
		}
74
75
76
//		$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...
77
//		$this->buildWithMemberLevel($qb, 'u.level', $level);
78
//		$this->buildWithCircleId($qb, 'c.id', $circleId);
79
//		$this->buildWithOrXTypes($qb, $userId, $type, $name, $circleId);
80
81
		$cursor = $qb->execute();
82
		$data = $cursor->fetch();
83
		if ($data === false) {
84
			throw new CircleDoesNotExistException(
85
				$this->l10n->t('Circle not found')
86
			);
87
		}
88
89
		$entry = $this->parseCirclesSelectSql($data);
90
91
		return $entry;
92
	}
93
94
95
	/**
96
	 * saveFrame()
97
	 *
98
	 * Insert a new entry in the database to save the SharingFrame.
99
	 *
100
	 * @param SharingFrame $frame
101
	 */
102 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...
103
104
		$qb = $this->getSharesInsertSql();
105
		$qb->setValue('circle_id', $qb->createNamedParameter($frame->getCircleId()))
106
		   ->setValue('source', $qb->createNamedParameter($frame->getSource()))
107
		   ->setValue('type', $qb->createNamedParameter($frame->getType()))
108
		   ->setValue('headers', $qb->createNamedParameter($frame->getHeaders(true)))
109
		   ->setValue('author', $qb->createNamedParameter($frame->getAuthor()))
110
		   ->setValue('cloud_id', $qb->createNamedParameter($frame->getCloudId()))
111
		   ->setValue('unique_id', $qb->createNamedParameter($frame->getUniqueId()))
112
		   ->setValue('payload', $qb->createNamedParameter($frame->getPayload(true)));
113
114
		$qb->execute();
115
	}
116
117
118 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...
119
		$qb = $this->getSharesUpdateSql($frame->getUniqueId());
120
		$qb->set('circle_id', $qb->createNamedParameter($frame->getCircleId()))
121
		   ->set('source', $qb->createNamedParameter($frame->getSource()))
122
		   ->set('type', $qb->createNamedParameter($frame->getType()))
123
		   ->set('headers', $qb->createNamedParameter($frame->getHeaders(true)))
124
		   ->set('author', $qb->createNamedParameter($frame->getAuthor()))
125
		   ->set('cloud_id', $qb->createNamedParameter($frame->getCloudId()))
126
		   ->set('unique_id', $qb->createNamedParameter($frame->getUniqueId()))
127
		   ->set('payload', $qb->createNamedParameter($frame->getPayload(true)));
128
129
		$qb->execute();
130
	}
131
132
133
	public function updateCircle(Circle $circle) {
134
		$qb = $this->getCirclesUpdateSql($circle->getId());
135
		$qb->set('name', $qb->createNamedParameter($circle->getName()))
136
		   ->set('description', $qb->createNamedParameter($circle->getDescription()))
137
		   ->set('settings', $qb->createNamedParameter($circle->getSettings(true)));
138
139
		$qb->execute();
140
	}
141
142
143
	/**
144
	 * @param string $uniqueId
145
	 *
146
	 * @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...
147
	 */
148
	public function getCircleFromUniqueId($uniqueId) {
149
		$qb = $this->getCirclesSelectSql();
150
		$this->limitToUniqueId($qb, (string)$uniqueId);
151
152
		$cursor = $qb->execute();
153
		$data = $cursor->fetch();
154
		$entry = $this->parseCirclesSelectSql($data);
155
156
		return $entry;
157
	}
158
159
160
	/**
161
	 * @param int $circleId
162
	 * @param string $uniqueId
163
	 *
164
	 * @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...
165
	 */
166
	public function getFrame($circleId, $uniqueId) {
167
		$qb = $this->getSharesSelectSql();
168
		$this->limitToUniqueId($qb, (string)$uniqueId);
169
		$this->limitToCircleId($qb, (int)$circleId);
170
171
		$cursor = $qb->execute();
172
		$data = $cursor->fetch();
173
		$entry = $this->parseSharesSelectSql($data);
174
175
		return $entry;
176
	}
177
178
179
	/**
180
	 * return the FederatedLink identified by a remote Circle UniqueId and the Token of the link
181
	 *
182
	 * @param string $token
183
	 * @param string $uniqueId
184
	 *
185
	 * @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...
186
	 */
187
	public function getLinkFromToken($token, $uniqueId) {
188
		$qb = $this->getLinksSelectSql();
189
		$this->limitToUniqueId($qb, (string)$uniqueId);
190
		$this->limitToToken($qb, (string)$token);
191
192
		$cursor = $qb->execute();
193
		$data = $cursor->fetch();
194
		$entry = $this->parseLinksSelectSql($data);
195
196
		return $entry;
197
	}
198
199
200
	/**
201
	 * return the FederatedLink identified by a its Id
202
	 *
203
	 * @param int $linkId
204
	 *
205
	 * @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...
206
	 * @throws FederatedLinkDoesNotExistException
207
	 */
208
	public function getLinkFromId($linkId) {
209
		$qb = $this->getLinksSelectSql();
210
		$this->limitToId($qb, (string)$linkId);
211
212
		$cursor = $qb->execute();
213
		$data = $cursor->fetch();
214
215
		if ($data === false) {
216
			throw new FederatedLinkDoesNotExistException(
217
				$this->l10n->t('Federated Link not found')
218
			);
219
		}
220
221
		$entry = $this->parseLinksSelectSql($data);
222
223
		return $entry;
224
	}
225
226
227
	/**
228
	 * returns all FederatedLink from a circle
229
	 *
230
	 * @param int $circleId
231
	 *
232
	 * @return FederatedLink[]
233
	 */
234
	public function getLinksFromCircle($circleId) {
235
		$qb = $this->getLinksSelectSql();
236
		$this->limitToCircleId($qb, $circleId);
237
238
		$links = [];
239
		$cursor = $qb->execute();
240
		while ($data = $cursor->fetch()) {
241
			$link = $this->parseLinksSelectSql($data);
242
			$link->shortenUniqueId();
243
			$link->shortenToken();
244
			if ($link !== null) {
245
				$links[] = $link;
246
			}
247
		}
248
		$cursor->closeCursor();
249
250
		return $links;
251
	}
252
253
	/**
254
	 * @param integer $circleId
255
	 * @param int $level
256
	 *
257
	 * @return Member[]
258
	 */
259
	public function getMembers($circleId, $level = Member::LEVEL_MEMBER) {
260
		$qb = $this->getMembersSelectSql();
261
		$this->limitToMemberLevel($qb, $level);
262
263
		$this->joinCircles($qb, 'm.circle_id');
264
		$this->limitToCircleId($qb, $circleId);
265
266
		$qb->selectAlias('c.name', 'circle_name');
267
268
		$users = [];
269
		$cursor = $qb->execute();
270
		while ($data = $cursor->fetch()) {
271
			$member = $this->parseMembersSelectSql($data);
272
			if ($member !== null) {
273
				$users[] = $member;
274
			}
275
		}
276
		$cursor->closeCursor();
277
278
		return $users;
279
	}
280
281
282
}