Completed
Pull Request — master (#546)
by Maxence
03:01
created

DeprecatedCirclesRequest::getFromContactGroup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php declare(strict_types=1);
2
3
4
/**
5
 * Circles - Bring cloud-users closer together.
6
 *
7
 * This file is licensed under the Affero General Public License version 3 or
8
 * later. See the COPYING file.
9
 *
10
 * @author Maxence Lange <[email protected]>
11
 * @copyright 2017
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
30
namespace OCA\Circles\Db;
31
32
33
use OCA\Circles\Exceptions\CircleAlreadyExistsException;
34
use OCA\Circles\Exceptions\CircleDoesNotExistException;
35
use OCA\Circles\Exceptions\ConfigNoCircleAvailableException;
36
use OCA\Circles\Exceptions\GSStatusException;
37
use OCA\Circles\Model\DeprecatedCircle;
38
use OCA\Circles\Model\DeprecatedMember;
39
40
class DeprecatedCirclesRequest extends DeprecatedCirclesRequestBuilder {
41
42
43
	/**
44
	 * forceGetCircle();
45
	 *
46
	 * returns data of a circle from its Id.
47
	 *
48
	 * WARNING: This function does not filters data regarding the current user/viewer.
49
	 *          In case of interaction with users, Please use getCircle() instead.
50
	 *
51
	 * @param string $circleUniqueId
52
	 * @param bool $allSettings
53
	 *
54
	 * @return DeprecatedCircle
55
	 * @throws CircleDoesNotExistException
56
	 */
57
	public function forceGetCircle($circleUniqueId, bool $allSettings = false) {
58
		$qb = $this->getCirclesSelectSql();
59
60
		$this->leftJoinOwner($qb, '');
61
		$this->limitToUniqueId($qb, $circleUniqueId);
62
63
		$cursor = $qb->execute();
64
		$data = $cursor->fetch();
65
		$cursor->closeCursor();
66
67
		if ($data === false) {
68
			throw new CircleDoesNotExistException($this->l10n->t('Circle not found'));
0 ignored issues
show
Deprecated Code introduced by
The class OCA\Circles\Exceptions\CircleDoesNotExistException has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
69
		}
70
71
		return $this->parseCirclesSelectSql($data, $allSettings);
72
	}
73
74
75
	/**
76
	 * forceGetCircles();
77
	 *
78
	 * returns data of a all circles.
79
	 *
80
	 * WARNING: This function does not filters data regarding the current user/viewer.
81
	 *          In case of interaction with users, Please use getCircles() instead.
82
	 *
83
	 * @param string $ownerId
84
	 *
85
	 * @return DeprecatedCircle[]
86
	 */
87 View Code Duplication
	public function forceGetCircles(string $ownerId = '') {
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...
88
		$qb = $this->getCirclesSelectSql();
89
		$this->leftJoinOwner($qb, $ownerId);
90
91
		$circles = [];
92
		$cursor = $qb->execute();
93
		while ($data = $cursor->fetch()) {
94
			$circles[] = $this->parseCirclesSelectSql($data, true);
95
		}
96
		$cursor->closeCursor();
97
98
		return $circles;
99
	}
100
101
102
	/**
103
	 * forceGetCircleByName();
104
	 *
105
	 * returns data of a circle from its Name.
106
	 *
107
	 * WARNING: This function does not filters data regarding the current user/viewer.
108
	 *          In case of interaction with users, do not use this method.
109
	 *
110
	 * @param $name
111
	 *
112
	 * @return null|DeprecatedCircle
113
	 * @throws CircleDoesNotExistException
114
	 */
115 View Code Duplication
	public function forceGetCircleByName($name) {
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->getCirclesSelectSql();
118
119
		$this->limitToName($qb, $name);
120
121
		$cursor = $qb->execute();
122
		$data = $cursor->fetch();
123
		$cursor->closeCursor();
124
125
		if ($data === false) {
126
			throw new CircleDoesNotExistException($this->l10n->t('Circle not found'));
0 ignored issues
show
Deprecated Code introduced by
The class OCA\Circles\Exceptions\CircleDoesNotExistException has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
127
		}
128
129
		return $this->parseCirclesSelectSql($data);
130
	}
131
132
133
	/**
134
	 * @param string $userId
135
	 * @param int $circleType
136
	 * @param string $name
137
	 * @param int $level
138
	 * @param bool $forceAll
139
	 * @param string $ownerId
140
	 *
141
	 * @return DeprecatedCircle[]
142
	 * @throws ConfigNoCircleAvailableException
143
	 * @throws GSStatusException
144
	 */
145
	public function getCircles(
146
		string $userId, int $circleType = 0, string $name = '', int $level = 0, bool $forceAll = false,
147
		string $ownerId = ''
148
	) {
149
		if ($circleType === 0) {
150
			$circleType = DeprecatedCircle::CIRCLES_ALL;
151
		}
152
153
		// todo - make it works based on $type
154
		$typeViewer = DeprecatedMember::TYPE_USER;
155
156
		$qb = $this->getCirclesSelectSql();
157
		$this->leftJoinUserIdAsViewer($qb, $userId, $typeViewer, '');
158
		$this->leftJoinOwner($qb, $ownerId);
159
		$this->leftJoinNCGroupAndUser($qb, $userId, 'c.unique_id');
160
161
		if ($level > 0) {
162
			$this->limitToLevel($qb, $level, ['u', 'g']);
163
		}
164
		$this->limitRegardingCircleType($qb, $userId, -1, $circleType, $name, $forceAll);
165
166
		$circles = [];
167
		$cursor = $qb->execute();
168
		while ($data = $cursor->fetch()) {
169
			if ($name === '' || stripos(strtolower($data['name']), strtolower($name)) !== false
170
				|| stripos(strtolower($data['alt_name']), strtolower($name)) !== false) {
171
				$circles[] = $this->parseCirclesSelectSql($data);
172
			}
173
		}
174
		$cursor->closeCursor();
175
176
		return $circles;
177
	}
178
179
180
	/**
181
	 *
182
	 * @param string $circleUniqueId
183
	 * @param string $viewerId
184
	 * @param int $type
185
	 * @param string $instanceId
186
	 * @param bool $forceAll
187
	 *
188
	 * @return DeprecatedCircle
189
	 * @throws CircleDoesNotExistException
190
	 * @throws ConfigNoCircleAvailableException
191
	 */
192
	public function getCircle(
193
		string $circleUniqueId, string $viewerId, int $type = DeprecatedMember::TYPE_USER, string $instanceId = '',
194
		bool $forceAll = false
195
	) {
196
		$qb = $this->getCirclesSelectSql();
197
198
		$this->limitToUniqueId($qb, $circleUniqueId);
199
200
		$this->leftJoinUserIdAsViewer($qb, $viewerId, $type, $instanceId);
201
		$this->leftJoinOwner($qb);
202
		if ($instanceId === '') {
203
			$this->leftJoinNCGroupAndUser($qb, $viewerId, 'c.unique_id');
204
		}
205
206
		$this->limitRegardingCircleType($qb, $viewerId, $circleUniqueId, DeprecatedCircle::CIRCLES_ALL, '', $forceAll);
207
208
		$cursor = $qb->execute();
209
		$data = $cursor->fetch();
210
		$cursor->closeCursor();
211
212
		if ($data === false) {
213
			throw new CircleDoesNotExistException($this->l10n->t('Circle not found ' . $circleUniqueId));
0 ignored issues
show
Deprecated Code introduced by
The class OCA\Circles\Exceptions\CircleDoesNotExistException has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
214
		}
215
216
		$circle = $this->parseCirclesSelectSql($data);
217
		if ($instanceId === '') {
218
			$circle->setGroupViewer(
219
				$this->membersRequest->forceGetHigherLevelGroupFromUser($circleUniqueId, $viewerId)
220
			);
221
		}
222
223
		return $circle;
224
	}
225
226
227
	/**
228
	 * createCircle();
229
	 *
230
	 * Create a circle with $userId as its owner.
231
	 * Will returns the circle
232
	 *
233
	 * @param DeprecatedCircle $circle
234
	 */
235 View Code Duplication
	public function createCircle(DeprecatedCircle $circle) {
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...
236
		$qb = $this->getCirclesInsertSql();
237
		$qb->setValue('unique_id', $qb->createNamedParameter($circle->getUniqueId()))
238
		   ->setValue('long_id', $qb->createNamedParameter($circle->getUniqueId(true)))
239
		   ->setValue('name', $qb->createNamedParameter($circle->getName(true)))
240
		   ->setValue('alt_name', $qb->createNamedParameter($circle->getAltName()))
241
		   ->setValue('description', $qb->createNamedParameter($circle->getDescription()))
242
		   ->setValue('contact_addressbook', $qb->createNamedParameter($circle->getContactAddressBook()))
243
		   ->setValue('contact_groupname', $qb->createNamedParameter($circle->getContactGroupName()))
244
		   ->setValue('settings', $qb->createNamedParameter($circle->getSettings(true)))
245
		   ->setValue('type', $qb->createNamedParameter($circle->getType()));
246
		$qb->execute();
247
	}
248
249
250
	/**
251
	 * remove a circle
252
	 *
253
	 * @param string $circleUniqueId
254
	 */
255
	public function destroyCircle($circleUniqueId) {
256
		$qb = $this->getCirclesDeleteSql($circleUniqueId);
257
258
		$qb->execute();
259
	}
260
261
262
	/**
263
	 * returns if the circle is already in database
264
	 *
265
	 * @param DeprecatedCircle $circle
266
	 * @param string $userId
267
	 *
268
	 * @return bool
269
	 * @throws ConfigNoCircleAvailableException
270
	 */
271
	public function isCircleUnique(DeprecatedCircle $circle, $userId = '') {
272
		if ($circle->getType() === DeprecatedCircle::CIRCLES_PERSONAL) {
273
			return $this->isPersonalCircleUnique($circle, $userId);
274
		}
275
276
		$qb = $this->getCirclesSelectSql();
277
		$this->limitToNonPersonalCircle($qb);
278
279
		$cursor = $qb->execute();
280
		while ($data = $cursor->fetch()) {
281
			if (strtolower($data['name']) === strtolower($circle->getName())
282
				&& $circle->getUniqueId(true) !== $data['unique_id']) {
283
				return false;
284
			}
285
		}
286
		$cursor->closeCursor();
287
288
		return true;
289
	}
290
291
292
	/**
293
	 * return if the personal circle is unique
294
	 *
295
	 * @param DeprecatedCircle $circle
296
	 * @param string $userId
297
	 *
298
	 * @return bool
299
	 * @throws ConfigNoCircleAvailableException
300
	 */
301
	private function isPersonalCircleUnique(DeprecatedCircle $circle, $userId = '') {
302
		if ($userId === '') {
303
			return true;
304
		}
305
306
		$list = $this->getCircles(
307
			$userId, DeprecatedCircle::CIRCLES_PERSONAL, $circle->getName(),
308
			DeprecatedMember::LEVEL_OWNER
309
		);
310
311
		foreach ($list as $test) {
312
			if (strtolower($test->getName()) === strtolower($circle->getName())
313
				&& $circle->getUniqueId(true) !== $test->getUniqueId(true)) {
314
				return false;
315
			}
316
		}
317
318
		return true;
319
	}
320
321
322
	/**
323
	 * @param DeprecatedCircle $circle
324
	 * @param string $userId
325
	 *
326
	 * @throws CircleAlreadyExistsException
327
	 * @throws ConfigNoCircleAvailableException
328
	 */
329
	public function updateCircle(DeprecatedCircle $circle, $userId = '') {
330
		if (!$this->isCircleUnique($circle, $userId)) {
331
			throw new CircleAlreadyExistsException(
332
				$this->l10n->t('A circle with that name exists')
333
			);
334
		}
335
336
		$qb = $this->getCirclesUpdateSql($circle->getUniqueId(true));
337
		$qb->set('name', $qb->createNamedParameter($circle->getName()))
338
		   ->set('description', $qb->createNamedParameter($circle->getDescription()))
339
		   ->set('settings', $qb->createNamedParameter($circle->getSettings(true)));
340
341
		$qb->execute();
342
	}
343
344
345
	/**
346
	 * @param string $uniqueId
347
	 *
348
	 * @return DeprecatedCircle
349
	 * @throws CircleDoesNotExistException
350
	 */
351 View Code Duplication
	public function getCircleFromUniqueId($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...
352
		$qb = $this->getCirclesSelectSql();
353
		$this->limitToUniqueId($qb, (string)$uniqueId);
354
355
		$cursor = $qb->execute();
356
		$data = $cursor->fetch();
357
		$cursor->closeCursor();
358
359
		if ($data === false) {
360
			throw new CircleDoesNotExistException($this->l10n->t('Circle not found'));
0 ignored issues
show
Deprecated Code introduced by
The class OCA\Circles\Exceptions\CircleDoesNotExistException has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
361
		}
362
363
		return $this->parseCirclesSelectSql($data);
364
	}
365
366
367
	/**
368
	 * @param int $addressBookId
369
	 *
370
	 * @return array
371
	 */
372
	public function getFromBook(int $addressBookId) {
373
		$qb = $this->getCirclesSelectSql();
374
		$this->limitToAddressBookId($qb, $addressBookId);
375
376
		$circles = [];
377
		$cursor = $qb->execute();
378
		while ($data = $cursor->fetch()) {
379
			$circles[] = $this->parseCirclesSelectSql($data);
380
		}
381
		$cursor->closeCursor();
382
383
		return $circles;
384
	}
385
386
387
	/**
388
	 * @param int $addressBookId
389
	 *
390
	 * @return DeprecatedCircle[]
391
	 */
392 View Code Duplication
	public function getFromContactBook(int $addressBookId): array {
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...
393
		$qb = $this->getCirclesSelectSql();
394
395
		if ($addressBookId > 0) {
396
			$this->limitToAddressBookId($qb, $addressBookId);
397
		}
398
399
400
		$circles = [];
401
		$cursor = $qb->execute();
402
		while ($data = $cursor->fetch()) {
403
			$circles[] = $this->parseCirclesSelectSql($data);
404
		}
405
		$cursor->closeCursor();
406
407
		return $circles;
408
	}
409
410
411
	/**
412
	 * @param int $addressBookId
413
	 * @param string $group
414
	 *
415
	 * @return DeprecatedCircle
416
	 * @throws CircleDoesNotExistException
417
	 */
418 View Code Duplication
	public function getFromContactGroup(int $addressBookId, string $group): DeprecatedCircle {
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...
419
		$qb = $this->getCirclesSelectSql();
420
		$this->limitToAddressBookId($qb, $addressBookId);
421
		$this->limitToContactGroup($qb, $group);
422
423
		$cursor = $qb->execute();
424
		$data = $cursor->fetch();
425
		$cursor->closeCursor();
426
427
		if ($data === false) {
428
			throw new CircleDoesNotExistException($this->l10n->t('Circle not found'));
0 ignored issues
show
Deprecated Code introduced by
The class OCA\Circles\Exceptions\CircleDoesNotExistException has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
429
		}
430
431
		return $this->parseCirclesSelectSql($data);
432
	}
433
434
}
435