Completed
Pull Request — master (#634)
by Maxence
02:35
created

SyncService::setOccOutput()   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 1
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 ArtificialOwl\MySmallPhpTools\Model\SimpleDataStore;
36
use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Logger;
37
use ArtificialOwl\MySmallPhpTools\Traits\TStringTools;
38
use Exception;
39
use OCA\Circles\AppInfo\Application;
40
use OCA\Circles\Db\CircleRequest;
41
use OCA\Circles\Db\MemberRequest;
42
use OCA\Circles\Exceptions\CircleNotFoundException;
43
use OCA\Circles\Exceptions\ContactAddressBookNotFoundException;
44
use OCA\Circles\Exceptions\ContactFormatException;
45
use OCA\Circles\Exceptions\ContactNotFoundException;
46
use OCA\Circles\Exceptions\FederatedEventException;
47
use OCA\Circles\Exceptions\FederatedItemException;
48
use OCA\Circles\Exceptions\FederatedUserException;
49
use OCA\Circles\Exceptions\FederatedUserNotFoundException;
50
use OCA\Circles\Exceptions\GroupNotFoundException;
51
use OCA\Circles\Exceptions\InitiatorNotConfirmedException;
52
use OCA\Circles\Exceptions\InvalidIdException;
53
use OCA\Circles\Exceptions\MemberNotFoundException;
54
use OCA\Circles\Exceptions\MigrationException;
55
use OCA\Circles\Exceptions\OwnerNotFoundException;
56
use OCA\Circles\Exceptions\RemoteInstanceException;
57
use OCA\Circles\Exceptions\RemoteNotFoundException;
58
use OCA\Circles\Exceptions\RemoteResourceNotFoundException;
59
use OCA\Circles\Exceptions\RequestBuilderException;
60
use OCA\Circles\Exceptions\SingleCircleNotFoundException;
61
use OCA\Circles\Exceptions\UnknownRemoteException;
62
use OCA\Circles\FederatedItems\SingleMemberAdd;
63
use OCA\Circles\Model\Circle;
64
use OCA\Circles\Model\Federated\FederatedEvent;
65
use OCA\Circles\Model\FederatedUser;
66
use OCA\Circles\Model\ManagedModel;
67
use OCA\Circles\Model\Member;
68
use OCP\IDBConnection;
69
use OCP\IGroupManager;
70
use OCP\IUserManager;
71
use OCP\Migration\IOutput;
72
use Symfony\Component\Console\Output\OutputInterface;
73
74
75
/**
76
 * Class SyncService
77
 *
78
 * @package OCA\Circles\Service
79
 */
80
class SyncService {
81
82
83
	use TStringTools;
84
	use TNC22Logger;
85
86
87
	const SYNC_APPS = 1;
88
	const SYNC_USERS = 2;
89
	const SYNC_GROUPS = 4;
90
	const SYNC_GLOBALSCALE = 8;
91
	const SYNC_REMOTES = 16;
92
	const SYNC_CONTACTS = 32;
93
	const SYNC_ALL = 63;
94
95
96
	/** @var IUserManager */
97
	private $userManager;
98
99
	/** @var IGroupManager */
100
	private $groupManager;
101
102
	/** @var IDBConnection */
103
	private $dbConnection;
104
105
	/** @var CircleRequest */
106
	private $circleRequest;
107
108
	/** @var MemberRequest */
109
	private $memberRequest;
110
111
	/** @var FederatedUserService */
112
	private $federatedUserService;
113
114
	/** @var federatedEventService */
115
	private $federatedEventService;
116
117
	/** @var CircleService */
118
	private $circleService;
119
120
	/** @var MemberService */
121
	private $memberService;
122
123
	/** @var MembershipService */
124
	private $membershipService;
125
126
	/** @var TimezoneService */
127
	private $timezoneService;
128
129
	/** @var ConfigService */
130
	private $configService;
131
132
133
	/** @var IOutput */
134
	private $migrationOutput;
135
136
	/** @var OutputInterface */
137
	private $occOutput;
138
139
140
	/**
141
	 * SyncService constructor.
142
	 *
143
	 * @param IUserManager $userManager
144
	 * @param IGroupManager $groupManager
145
	 * @param IDBConnection $dbConnection
146
	 * @param CircleRequest $circleRequest
147
	 * @param MemberRequest $memberRequest
148
	 * @param FederatedUserService $federatedUserService
149
	 * @param federatedEventService $federatedEventService
150
	 * @param CircleService $circleService
151
	 * @param MemberService $memberService
152
	 * @param MembershipService $membershipService
153
	 * @param TimezoneService $timezoneService
154
	 * @param ConfigService $configService
155
	 */
156
	public function __construct(
157
		IUserManager $userManager,
158
		IGroupManager $groupManager,
159
		IDBConnection $dbConnection,
160
		CircleRequest $circleRequest,
161
		MemberRequest $memberRequest,
162
		FederatedUserService $federatedUserService,
163
		federatedEventService $federatedEventService,
164
		CircleService $circleService,
165
		MemberService $memberService,
166
		MembershipService $membershipService,
167
		TimezoneService $timezoneService,
168
		ConfigService $configService
169
	) {
170
		$this->userManager = $userManager;
171
		$this->groupManager = $groupManager;
172
		$this->dbConnection = $dbConnection;
173
		$this->circleRequest = $circleRequest;
174
		$this->memberRequest = $memberRequest;
175
		$this->federatedUserService = $federatedUserService;
176
		$this->federatedEventService = $federatedEventService;
177
		$this->circleService = $circleService;
178
		$this->memberService = $memberService;
179
		$this->membershipService = $membershipService;
180
		$this->timezoneService = $timezoneService;
181
		$this->configService = $configService;
182
183
		$this->setup('app', Application::APP_ID);
184
	}
185
186
187
	/**
188
	 * @param OutputInterface $output
189
	 */
190
	public function setOccOutput(OutputInterface $output): void {
191
		$this->occOutput = $output;
192
	}
193
194
	/**
195
	 * @param IOutput $output
196
	 */
197
	public function setMigrationOutput(IOutput $output): void {
198
		$this->migrationOutput = $output;
199
	}
200
201
202
	/**
203
	 * @param int $sync
204
	 *
205
	 * @return void
206
	 */
207
	public function sync(int $sync = self::SYNC_ALL): void {
208
		if (!is_null($this->migrationOutput)) {
209
			$this->migrationOutput->startProgress(7);
210
		}
211
212
		if ($this->shouldSync(self::SYNC_APPS, $sync)) {
213
			$this->syncApps();
214
		}
215
216
		if ($this->shouldSync(self::SYNC_USERS, $sync)) {
217
			$this->syncNextcloudUsers();
218
		}
219
220
		if ($this->shouldSync(self::SYNC_GROUPS, $sync)) {
221
			$this->syncNextcloudGroups();
222
		}
223
224
		if ($this->shouldSync(self::SYNC_GLOBALSCALE, $sync)) {
225
			$this->syncGlobalScale();
226
		}
227
228
		if ($this->shouldSync(self::SYNC_REMOTES, $sync)) {
229
			$this->syncRemote();
230
		}
231
232
		if ($this->shouldSync(self::SYNC_CONTACTS, $sync)) {
233
			$this->syncContacts();
234
		}
235
236
		if (!is_null($this->migrationOutput)) {
237
			$this->migrationOutput->finishProgress();
238
		}
239
	}
240
241
242
	/**
243
	 * @param int $item
244
	 * @param int $all
245
	 *
246
	 * @return bool
247
	 */
248
	private function shouldSync(int $item, int $all): bool {
249
		return (($item & $all) !== 0);
250
	}
251
252
253
	/**
254
	 */
255
	public function syncApps(): void {
256
		$this->output('Syncing Nextcloud Apps', true);
257
258
		try {
259
			$this->federatedUserService->getAppInitiator('circles', Member::APP_CIRCLES);
260
			$this->federatedUserService->getAppInitiator('occ', Member::APP_OCC);
261
		} catch (Exception $e) {
262
			$this->e($e);
263
		}
264
	}
265
266
267
	/**
268
	 * @return void
269
	 */
270
	public function syncNextcloudUsers(): void {
271
		$this->output('Syncing Nextcloud Users', true);
272
273
		foreach ($this->userManager->search('') as $user) {
274
			try {
275
				$this->syncNextcloudUser($user->getUID());
276
			} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
277
			}
278
		}
279
	}
280
281
	/**
282
	 * @param string $userId
283
	 *
284
	 * @return FederatedUser
285
	 * @throws ContactAddressBookNotFoundException
286
	 * @throws ContactFormatException
287
	 * @throws ContactNotFoundException
288
	 * @throws FederatedUserException
289
	 * @throws FederatedUserNotFoundException
290
	 * @throws InvalidIdException
291
	 * @throws RequestBuilderException
292
	 * @throws SingleCircleNotFoundException
293
	 */
294
	public function syncNextcloudUser(string $userId): FederatedUser {
295
		return $this->federatedUserService->getLocalFederatedUser($userId);
296
	}
297
298
299
	/**
300
	 * @return void
301
	 */
302
	public function syncNextcloudGroups(): void {
303
		$this->output('Syncing Nextcloud Groups', true);
304
305
		foreach ($this->groupManager->search('') as $group) {
306
			try {
307
				$this->syncNextcloudGroup($group->getGID());
308
			} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
309
			}
310
		}
311
	}
312
313
	/**
314
	 * @param string $groupId
315
	 *
316
	 * @return Circle
317
	 * @throws FederatedUserException
318
	 * @throws FederatedUserNotFoundException
319
	 * @throws GroupNotFoundException
320
	 * @throws InvalidIdException
321
	 * @throws SingleCircleNotFoundException
322
	 * @throws FederatedEventException
323
	 * @throws FederatedItemException
324
	 * @throws InitiatorNotConfirmedException
325
	 * @throws OwnerNotFoundException
326
	 * @throws RemoteInstanceException
327
	 * @throws RemoteNotFoundException
328
	 * @throws RemoteResourceNotFoundException
329
	 * @throws UnknownRemoteException
330
	 * @throws RequestBuilderException
331
	 */
332
	public function syncNextcloudGroup(string $groupId): Circle {
333
		$circle = $this->federatedUserService->getGroupCircle($groupId);
334
		$group = $this->groupManager->get($groupId);
335
		foreach ($group->getUsers() as $user) {
336
			$member = $this->generateGroupMember($circle, $user->getUID());
337
			$event = new FederatedEvent(SingleMemberAdd::class);
338
			$event->setCircle($circle);
339
			$event->setMember($member);
340
			$this->federatedEventService->newEvent($event);
341
342
//			$this->memberRequest->insertOrUpdate($member);
343
		}
344
345
//		$this->membershipService->onUpdate($circle->getSingleId());
346
347
		return $circle;
348
	}
349
350
351
	/**
352
	 * @param string $userId
353
	 *
354
	 * @throws ContactAddressBookNotFoundException
355
	 * @throws ContactFormatException
356
	 * @throws ContactNotFoundException
357
	 * @throws FederatedUserException
358
	 * @throws FederatedUserNotFoundException
359
	 * @throws InvalidIdException
360
	 * @throws RequestBuilderException
361
	 */
362
	public function userDeleted(string $userId): void {
363
		try {
364
			$federatedUser = $this->federatedUserService->getLocalFederatedUser($userId, false);
365
		} catch (SingleCircleNotFoundException $e) {
366
			return;
367
		}
368
369
		$this->federatedUserService->setCurrentUser($federatedUser);
370
371
		$memberships = $federatedUser->getMemberships();
372
		foreach ($memberships as $membership) {
373
			if ($membership->getInheritanceDepth() > 1) {
374
				continue;
375
			}
376
377
			try {
378
				$this->circleService->circleLeave($membership->getCircleId(), true);
379
			} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
380
			}
381
		}
382
383
		$this->federatedUserService->deleteFederatedUser($federatedUser);
384
	}
385
386
387
	/**
388
	 * @param string $groupId
389
	 *
390
	 * @throws ContactAddressBookNotFoundException
391
	 * @throws ContactFormatException
392
	 * @throws ContactNotFoundException
393
	 * @throws FederatedUserException
394
	 * @throws InvalidIdException
395
	 * @throws RequestBuilderException
396
	 * @throws SingleCircleNotFoundException
397
	 */
398
	public function groupDeleted(string $groupId): void {
399
		$circle = new Circle();
400
		$circle->setName('group:' . $groupId)
401
			   ->setConfig(Circle::CFG_SYSTEM | Circle::CFG_NO_OWNER | Circle::CFG_HIDDEN)
402
			   ->setSource(Member::TYPE_GROUP);
403
404
		$owner = $this->federatedUserService->getAppInitiator(
405
			Application::APP_ID,
406
			Member::APP_CIRCLES,
407
			Application::APP_NAME
408
		);
409
410
		$member = new Member();
411
		$member->importFromIFederatedUser($owner);
412
		$member->setLevel(Member::LEVEL_OWNER)
413
			   ->setStatus(Member::STATUS_MEMBER);
414
		$circle->setOwner($member);
415
416
		try {
417
			$circle = $this->circleRequest->searchCircle($circle);
418
		} catch (CircleNotFoundException $e) {
419
			return;
420
		}
421
422
		$this->circleRequest->delete($circle);
423
		$this->memberRequest->deleteAllFromCircle($circle);
424
425
		$this->membershipService->onUpdate($circle->getSingleId());
426
	}
427
428
429
	/**
430
	 * @param Circle $circle
431
	 * @param string $userId
432
	 *
433
	 * @return Member
434
	 * @throws ContactAddressBookNotFoundException
435
	 * @throws ContactFormatException
436
	 * @throws ContactNotFoundException
437
	 * @throws FederatedUserException
438
	 * @throws FederatedUserNotFoundException
439
	 * @throws InvalidIdException
440
	 * @throws RequestBuilderException
441
	 * @throws SingleCircleNotFoundException
442
	 */
443
	private function generateGroupMember(Circle $circle, string $userId): Member {
444
		$federatedUser = $this->federatedUserService->getLocalFederatedUser($userId);
445
446
		$member = new Member();
447
		$member->importFromIFederatedUser($federatedUser);
448
		$member->setId($this->token(ManagedModel::ID_LENGTH));
449
		$member->setCircleId($circle->getSingleId());
450
		$member->setLevel(Member::LEVEL_MEMBER);
451
		$member->setStatus(Member::STATUS_MEMBER);
452
		$member->setInvitedBy($this->federatedUserService->getCurrentApp());
0 ignored issues
show
Bug introduced by
It seems like $this->federatedUserService->getCurrentApp() can be null; however, setInvitedBy() 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...
453
454
		return $member;
455
	}
456
457
458
	/**
459
	 * @param string $groupId
460
	 * @param string $userId
461
	 *
462
	 * @return Member
463
	 * @throws FederatedEventException
464
	 * @throws FederatedItemException
465
	 * @throws FederatedUserException
466
	 * @throws FederatedUserNotFoundException
467
	 * @throws GroupNotFoundException
468
	 * @throws InitiatorNotConfirmedException
469
	 * @throws InvalidIdException
470
	 * @throws OwnerNotFoundException
471
	 * @throws RemoteInstanceException
472
	 * @throws RemoteNotFoundException
473
	 * @throws RemoteResourceNotFoundException
474
	 * @throws RequestBuilderException
475
	 * @throws SingleCircleNotFoundException
476
	 * @throws UnknownRemoteException
477
	 */
478
	public function groupMemberAdded(string $groupId, string $userId): void {
479
		$circle = $this->federatedUserService->getGroupCircle($groupId);
480
		$member = $this->generateGroupMember($circle, $userId);
481
482
		$event = new FederatedEvent(SingleMemberAdd::class);
483
		$event->setCircle($circle);
484
		$event->setMember($member);
485
		$this->federatedEventService->newEvent($event);
486
487
//		$this->memberRequest->insertOrUpdate($member);
488
489
//		$this->membershipService->onUpdate($member->getSingleId());
490
	}
491
492
493
	/**
494
	 * @param string $groupId
495
	 * @param string $userId
496
	 *
497
	 * @throws FederatedEventException
498
	 * @throws FederatedItemException
499
	 * @throws FederatedUserException
500
	 * @throws FederatedUserNotFoundException
501
	 * @throws GroupNotFoundException
502
	 * @throws InitiatorNotConfirmedException
503
	 * @throws InvalidIdException
504
	 * @throws OwnerNotFoundException
505
	 * @throws RemoteInstanceException
506
	 * @throws RemoteNotFoundException
507
	 * @throws RemoteResourceNotFoundException
508
	 * @throws RequestBuilderException
509
	 * @throws SingleCircleNotFoundException
510
	 * @throws UnknownRemoteException
511
	 */
512
	public function groupMemberRemoved(string $groupId, string $userId): void {
513
		$circle = $this->federatedUserService->getGroupCircle($groupId);
514
		$federatedUser = $this->federatedUserService->getLocalFederatedUser($userId);
515
516
		$this->memberRequest->deleteFederatedUserFromCircle($federatedUser, $circle);
517
		$this->membershipService->onUpdate($federatedUser->getSingleId());
518
	}
519
520
521
	/**
522
	 * @return void
523
	 */
524
	public function syncContacts(): void {
525
		$this->output('Syncing Contacts', true);
526
	}
527
528
529
	/**
530
	 * @return void
531
	 */
532
	public function syncGlobalScale(): void {
533
		$this->output('Syncing GlobalScale', true);
534
	}
535
536
537
	/**
538
	 * @return void
539
	 */
540
	public function syncRemote(): void {
541
		$this->output('Syncing Remote Instance', true);
542
	}
543
544
545
	/**
546
	 * @param string $circleId
547
	 *
548
	 * @return void
549
	 */
550
	public function syncRemoteCircle(string $circleId): void {
0 ignored issues
show
Unused Code introduced by
The parameter $circleId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
551
	}
552
553
554
	/**
555
	 * @param bool $force
556
	 *
557
	 * @throws MigrationException
558
	 * @throws RequestBuilderException
559
	 */
560
	public function migration(bool $force = false): void {
561
		if ($this->configService->getAppValueBool(ConfigService::MIGRATION_RUN)) {
562
			throw new MigrationException('A migration process is already running');
563
		}
564
		$this->configService->setAppValue(ConfigService::MIGRATION_RUN, '1');
565
566
		if ($force) {
567
			$this->configService->setAppValue(ConfigService::MIGRATION_22, '0');
568
//			$this->configService->setAppValue(ConfigService::MIGRATION_23, '0');
569
		}
570
571
		$this->migrationTo22();
572
		//	$this->migrationTo23();
573
574
		$this->configService->setAppValue(ConfigService::MIGRATION_RUN, '0');
575
	}
576
577
	/**
578
	 * @return void
579
	 * @throws RequestBuilderException
580
	 */
581
	private function migrationTo22(): void {
582
		if ($this->configService->getAppValueBool(ConfigService::MIGRATION_22)) {
583
			return;
584
		}
585
586
		$this->output('Migrating to 22');
587
588
		if (!is_null($this->migrationOutput)) {
589
			$this->migrationOutput->startProgress(2);
590
		}
591
592
		$this->migrationTo22_Circles();
593
		$this->migrationTo22_Members();
594
595
		if (!is_null($this->migrationOutput)) {
596
			$this->migrationOutput->finishProgress();
597
		}
598
599
		$this->configService->setAppValue(ConfigService::MIGRATION_22, '1');
600
	}
601
602
603
	/**
604
	 *
605
	 * @throws RequestBuilderException
606
	 */
607
	private function migrationTo22_Circles(): void {
608
		$this->output('Migrating Circles', true);
609
610
		$circles = [];
611
612
		$qb = $this->dbConnection->getQueryBuilder();
613
		$qb->select('*')->from('circle_circles');
614
615
		try {
616
			$cursor = $qb->executeQuery();
617
			while ($row = $cursor->fetch()) {
618
				$data = new SimpleDataStore($row);
619
620
				$circle = new Circle();
621
				$circle->setSingleId($data->g('unique_id'))
622
					   ->setName($data->g('name'))
623
					   ->setDisplayName($data->g('display_name'))
624
					   ->setSettings($data->gArray('settings'))
625
					   ->setDescription($data->g('description'))
626
					   ->setContactAddressBook($data->gInt('contact_addressbook'))
627
					   ->setContactGroupName($data->g('contact_groupname'))
628
					   ->setSource(Member::TYPE_CIRCLE);
629
630
				$dTime = $this->timezoneService->getDateTime($data->g('creation'));
631
				$circle->setCreation($dTime->getTimestamp());
632
633
				if ($circle->getDisplayName() === '') {
634
					$circle->setDisplayName($circle->getName());
635
				}
636
637
				$this->circleService->generateSanitizedName($circle);
638
				switch ($data->gInt('type')) {
639
					case 1: // personal
640
						$config = Circle::CFG_PERSONAL;
0 ignored issues
show
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
641
						break;
642
643
					case 2: // secret
644
						$config = Circle::CFG_CIRCLE;
0 ignored issues
show
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
645
						break;
646
647
					case 4: // closed
648
						$config = Circle::CFG_OPEN + Circle::CFG_REQUEST;
0 ignored issues
show
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
649
						break;
650
651
					case 8: // public
652
						$config = Circle::CFG_OPEN;
0 ignored issues
show
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
653
						break;
654
				}
655
656
				$circles[] = $circle;
657
			}
658
659
			$cursor->closeCursor();
660
		} catch (\OCP\DB\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class OCP\DB\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
661
		}
662
663
		foreach ($circles as $circle) {
664
			/** @var Circle $circle */
665
			try {
666
				try {
667
					$this->circleRequest->getCircle($circle->getSingleId());
668
				} catch (CircleNotFoundException $e) {
669
					$this->circleRequest->save($circle);
670
					usleep(50);
671
				}
672
			} catch (InvalidIdException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
673
			}
674
		}
675
	}
676
677
678
	/**
679
	 * @throws ContactAddressBookNotFoundException
680
	 * @throws ContactFormatException
681
	 * @throws ContactNotFoundException
682
	 * @throws FederatedUserException
683
	 * @throws InvalidIdException
684
	 * @throws RequestBuilderException
685
	 * @throws SingleCircleNotFoundException
686
	 */
687
	private function migrationTo22_Members(): void {
688
		$this->output('Migrating Members', true);
689
690
		$members = [];
691
692
		$qb = $this->dbConnection->getQueryBuilder();
693
		$qb->select('*')->from('circle_members');
694
695
		$appCircle = $this->federatedUserService->getAppInitiator(
696
			Application::APP_ID,
697
			Member::APP_CIRCLES
698
		);
699
700
		try {
701
			$cursor = $qb->executeQuery();
702
			while ($row = $cursor->fetch()) {
703
				$data = new SimpleDataStore($row);
704
705
				$member = new Member();
706
707
				$member->setCircleId($data->g('circle_id'))
708
					   ->setId($data->g('member_id'))
709
					   ->setUserId($data->g('user_id'))
710
					   ->setInstance($data->g('instance'))
711
					   ->setDisplayName($data->g('cached_name'))
712
					   ->setLevel($data->gInt('level'))
713
					   ->setStatus($data->g('status'))
714
					   ->setContactMeta($data->g('contact_meta'))
715
					   ->setContactId($data->g('contact_id'))
716
					   ->setInvitedBy($appCircle);
717
718
				switch ($data->gInt('user_type')) {
719
					case 1:
720
						$member->setUserType(1);
721
						break;
722
					case 2:
723
						$member->setUserType(2);
724
						break;
725
					case 3:
726
						$member->setUserType(4);
727
						break;
728
					case 4:
729
						$member->setUserType(8);
730
						break;
731
				}
732
733
				try {
734
					$singleMember = $this->federatedUserService->getFederatedUser(
735
						$member->getUserId(),
736
						$member->getUserType()
737
					);
738
				} catch (ContactFormatException $e) {
739
					continue;
740
				}
741
				$member->setSingleId($singleMember->getSingleId());
742
743
//					"cached_update":"2021-05-02 12:13:22",
744
//					"joined":"2021-05-02 12:13:22",
745
//					"contact_checked":null,"
746
//					single_id":"wt6WQYYCry3EOud",
747
//					"circle_source":null}
748
749
				$members[] = $member;
750
			}
751
			$cursor->closeCursor();
752
		} catch (\OCP\DB\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class OCP\DB\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
753
		}
754
755
		foreach ($members as $member) {
756
			try {
757
				$this->memberRequest->getMemberById($member->getId());
758
			} catch (MemberNotFoundException $e) {
759
				$this->memberRequest->save($member);
760
			}
761
		}
762
	}
763
764
	/**
765
	 * @param string $message
766
	 * @param bool $advance
767
	 */
768
	private function output(string $message, bool $advance = false): void {
769
		if (!is_null($this->occOutput)) {
770
			$this->occOutput->writeln((($advance) ? '+' : '-') . ' ' . $message);
771
		}
772
773
		if (!is_null($this->migrationOutput)) {
774
			if ($advance) {
775
				$this->migrationOutput->advance(1, '(Circles) ' . $message);
776
			} else {
777
				$this->migrationOutput->info('(Circles) ' . $message);
778
			}
779
		}
780
	}
781
782
}
783
784