Completed
Pull Request — master (#634)
by Maxence
04:45 queued 02:03
created

SyncService::migration()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
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\Exceptions\UserTypeNotFoundException;
63
use OCA\Circles\FederatedItems\SingleMemberAdd;
64
use OCA\Circles\Model\Circle;
65
use OCA\Circles\Model\Federated\FederatedEvent;
66
use OCA\Circles\Model\FederatedUser;
67
use OCA\Circles\Model\ManagedModel;
68
use OCA\Circles\Model\Member;
69
use OCP\IDBConnection;
70
use OCP\IGroupManager;
71
use OCP\IUserManager;
72
use OCP\Migration\IOutput;
73
use Symfony\Component\Console\Output\OutputInterface;
74
75
76
/**
77
 * Class SyncService
78
 *
79
 * @package OCA\Circles\Service
80
 */
81
class SyncService {
82
83
84
	use TStringTools;
85
	use TNC22Logger;
86
87
88
	const SYNC_APPS = 1;
89
	const SYNC_USERS = 2;
90
	const SYNC_GROUPS = 4;
91
	const SYNC_GLOBALSCALE = 8;
92
	const SYNC_REMOTES = 16;
93
	const SYNC_CONTACTS = 32;
94
	const SYNC_ALL = 63;
95
96
97
	/** @var IUserManager */
98
	private $userManager;
99
100
	/** @var IGroupManager */
101
	private $groupManager;
102
103
	/** @var IDBConnection */
104
	private $dbConnection;
105
106
	/** @var CircleRequest */
107
	private $circleRequest;
108
109
	/** @var MemberRequest */
110
	private $memberRequest;
111
112
	/** @var FederatedUserService */
113
	private $federatedUserService;
114
115
	/** @var federatedEventService */
116
	private $federatedEventService;
117
118
	/** @var CircleService */
119
	private $circleService;
120
121
	/** @var MemberService */
122
	private $memberService;
123
124
	/** @var MembershipService */
125
	private $membershipService;
126
127
	/** @var TimezoneService */
128
	private $timezoneService;
129
130
	/** @var ConfigService */
131
	private $configService;
132
133
134
	/** @var IOutput */
135
	private $migrationOutput;
136
137
	/** @var OutputInterface */
138
	private $occOutput;
139
140
141
	/**
142
	 * SyncService constructor.
143
	 *
144
	 * @param IUserManager $userManager
145
	 * @param IGroupManager $groupManager
146
	 * @param IDBConnection $dbConnection
147
	 * @param CircleRequest $circleRequest
148
	 * @param MemberRequest $memberRequest
149
	 * @param FederatedUserService $federatedUserService
150
	 * @param federatedEventService $federatedEventService
151
	 * @param CircleService $circleService
152
	 * @param MemberService $memberService
153
	 * @param MembershipService $membershipService
154
	 * @param TimezoneService $timezoneService
155
	 * @param ConfigService $configService
156
	 */
157
	public function __construct(
158
		IUserManager $userManager,
159
		IGroupManager $groupManager,
160
		IDBConnection $dbConnection,
161
		CircleRequest $circleRequest,
162
		MemberRequest $memberRequest,
163
		FederatedUserService $federatedUserService,
164
		federatedEventService $federatedEventService,
165
		CircleService $circleService,
166
		MemberService $memberService,
167
		MembershipService $membershipService,
168
		TimezoneService $timezoneService,
169
		ConfigService $configService
170
	) {
171
		$this->userManager = $userManager;
172
		$this->groupManager = $groupManager;
173
		$this->dbConnection = $dbConnection;
174
		$this->circleRequest = $circleRequest;
175
		$this->memberRequest = $memberRequest;
176
		$this->federatedUserService = $federatedUserService;
177
		$this->federatedEventService = $federatedEventService;
178
		$this->circleService = $circleService;
179
		$this->memberService = $memberService;
180
		$this->membershipService = $membershipService;
181
		$this->timezoneService = $timezoneService;
182
		$this->configService = $configService;
183
184
		$this->setup('app', Application::APP_ID);
185
	}
186
187
188
	/**
189
	 * @param OutputInterface $output
190
	 */
191
	public function setOccOutput(OutputInterface $output): void {
192
		$this->occOutput = $output;
193
	}
194
195
	/**
196
	 * @param IOutput $output
197
	 */
198
	public function setMigrationOutput(IOutput $output): void {
199
		$this->migrationOutput = $output;
200
	}
201
202
203
	/**
204
	 * @param int $sync
205
	 *
206
	 * @return void
207
	 */
208
	public function sync(int $sync = self::SYNC_ALL): void {
209
		if (!is_null($this->migrationOutput)) {
210
			$this->migrationOutput->startProgress(7);
211
		}
212
213
		if ($this->shouldSync(self::SYNC_APPS, $sync)) {
214
			$this->syncApps();
215
		}
216
217
		if ($this->shouldSync(self::SYNC_USERS, $sync)) {
218
			$this->syncNextcloudUsers();
219
		}
220
221
		if ($this->shouldSync(self::SYNC_GROUPS, $sync)) {
222
			$this->syncNextcloudGroups();
223
		}
224
225
		if ($this->shouldSync(self::SYNC_GLOBALSCALE, $sync)) {
226
			$this->syncGlobalScale();
227
		}
228
229
		if ($this->shouldSync(self::SYNC_REMOTES, $sync)) {
230
			$this->syncRemote();
231
		}
232
233
		if ($this->shouldSync(self::SYNC_CONTACTS, $sync)) {
234
			$this->syncContacts();
235
		}
236
237
		if (!is_null($this->migrationOutput)) {
238
			$this->migrationOutput->finishProgress();
239
		}
240
	}
241
242
243
	/**
244
	 * @param int $item
245
	 * @param int $all
246
	 *
247
	 * @return bool
248
	 */
249
	private function shouldSync(int $item, int $all): bool {
250
		return (($item & $all) !== 0);
251
	}
252
253
254
	/**
255
	 */
256
	public function syncApps(): void {
257
		$this->output('Syncing Nextcloud Apps', true);
258
259
		try {
260
			$this->federatedUserService->getAppInitiator('circles', Member::APP_CIRCLES);
261
			$this->federatedUserService->getAppInitiator('occ', Member::APP_OCC);
262
		} catch (Exception $e) {
263
			$this->e($e);
264
		}
265
	}
266
267
268
	/**
269
	 * @return void
270
	 */
271
	public function syncNextcloudUsers(): void {
272
		$this->output('Syncing Nextcloud Users', true);
273
274
		foreach ($this->userManager->search('') as $user) {
275
			try {
276
				$this->syncNextcloudUser($user->getUID());
277
			} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
278
			}
279
		}
280
	}
281
282
	/**
283
	 * @param string $userId
284
	 *
285
	 * @return FederatedUser
286
	 * @throws ContactAddressBookNotFoundException
287
	 * @throws ContactFormatException
288
	 * @throws ContactNotFoundException
289
	 * @throws FederatedUserException
290
	 * @throws FederatedUserNotFoundException
291
	 * @throws InvalidIdException
292
	 * @throws RequestBuilderException
293
	 * @throws SingleCircleNotFoundException
294
	 */
295
	public function syncNextcloudUser(string $userId): FederatedUser {
296
		return $this->federatedUserService->getLocalFederatedUser($userId);
297
	}
298
299
300
	/**
301
	 * @return void
302
	 */
303
	public function syncNextcloudGroups(): void {
304
		$this->output('Syncing Nextcloud Groups', true);
305
306
		foreach ($this->groupManager->search('') as $group) {
307
			try {
308
				$this->syncNextcloudGroup($group->getGID());
309
			} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
310
			}
311
		}
312
	}
313
314
	/**
315
	 * @param string $groupId
316
	 *
317
	 * @return Circle
318
	 * @throws FederatedUserException
319
	 * @throws FederatedUserNotFoundException
320
	 * @throws GroupNotFoundException
321
	 * @throws InvalidIdException
322
	 * @throws SingleCircleNotFoundException
323
	 * @throws FederatedEventException
324
	 * @throws FederatedItemException
325
	 * @throws InitiatorNotConfirmedException
326
	 * @throws OwnerNotFoundException
327
	 * @throws RemoteInstanceException
328
	 * @throws RemoteNotFoundException
329
	 * @throws RemoteResourceNotFoundException
330
	 * @throws UnknownRemoteException
331
	 * @throws RequestBuilderException
332
	 */
333
	public function syncNextcloudGroup(string $groupId): Circle {
334
		$circle = $this->federatedUserService->getGroupCircle($groupId);
335
		$group = $this->groupManager->get($groupId);
336
		foreach ($group->getUsers() as $user) {
337
			$member = $this->generateGroupMember($circle, $user->getUID());
338
			$event = new FederatedEvent(SingleMemberAdd::class);
339
			$event->setCircle($circle);
340
			$event->setMember($member);
341
			$this->federatedEventService->newEvent($event);
342
343
//			$this->memberRequest->insertOrUpdate($member);
344
		}
345
346
//		$this->membershipService->onUpdate($circle->getSingleId());
347
348
		return $circle;
349
	}
350
351
352
	/**
353
	 * @param string $userId
354
	 *
355
	 * @throws ContactAddressBookNotFoundException
356
	 * @throws ContactFormatException
357
	 * @throws ContactNotFoundException
358
	 * @throws FederatedUserException
359
	 * @throws FederatedUserNotFoundException
360
	 * @throws InvalidIdException
361
	 * @throws RequestBuilderException
362
	 */
363
	public function userDeleted(string $userId): void {
364
		try {
365
			$federatedUser = $this->federatedUserService->getLocalFederatedUser($userId, false);
366
		} catch (SingleCircleNotFoundException $e) {
367
			return;
368
		}
369
370
		$this->federatedUserService->setCurrentUser($federatedUser);
371
372
		$memberships = $federatedUser->getMemberships();
373
		foreach ($memberships as $membership) {
374
			if ($membership->getInheritanceDepth() > 1) {
375
				continue;
376
			}
377
378
			try {
379
				$this->circleService->circleLeave($membership->getCircleId(), true);
380
			} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
381
			}
382
		}
383
384
		$this->federatedUserService->deleteFederatedUser($federatedUser);
385
	}
386
387
388
	/**
389
	 * @param string $groupId
390
	 *
391
	 * @throws ContactAddressBookNotFoundException
392
	 * @throws ContactFormatException
393
	 * @throws ContactNotFoundException
394
	 * @throws FederatedUserException
395
	 * @throws InvalidIdException
396
	 * @throws RequestBuilderException
397
	 * @throws SingleCircleNotFoundException
398
	 */
399
	public function groupDeleted(string $groupId): void {
400
		$circle = new Circle();
401
		$circle->setName('group:' . $groupId)
402
			   ->setConfig(Circle::CFG_SYSTEM | Circle::CFG_NO_OWNER | Circle::CFG_HIDDEN)
403
			   ->setSource(Member::TYPE_GROUP);
404
405
		$owner = $this->federatedUserService->getAppInitiator(
406
			Application::APP_ID,
407
			Member::APP_CIRCLES,
408
			Application::APP_NAME
409
		);
410
411
		$member = new Member();
412
		$member->importFromIFederatedUser($owner);
413
		$member->setLevel(Member::LEVEL_OWNER)
414
			   ->setStatus(Member::STATUS_MEMBER);
415
		$circle->setOwner($member);
416
417
		try {
418
			$circle = $this->circleRequest->searchCircle($circle);
419
		} catch (CircleNotFoundException $e) {
420
			return;
421
		}
422
423
		$this->circleRequest->delete($circle);
424
		$this->memberRequest->deleteAllFromCircle($circle);
425
426
		$this->membershipService->onUpdate($circle->getSingleId());
427
	}
428
429
430
	/**
431
	 * @param Circle $circle
432
	 * @param string $userId
433
	 *
434
	 * @return Member
435
	 * @throws ContactAddressBookNotFoundException
436
	 * @throws ContactFormatException
437
	 * @throws ContactNotFoundException
438
	 * @throws FederatedUserException
439
	 * @throws FederatedUserNotFoundException
440
	 * @throws InvalidIdException
441
	 * @throws RequestBuilderException
442
	 * @throws SingleCircleNotFoundException
443
	 */
444
	private function generateGroupMember(Circle $circle, string $userId): Member {
445
		$federatedUser = $this->federatedUserService->getLocalFederatedUser($userId);
446
447
		$member = new Member();
448
		$member->importFromIFederatedUser($federatedUser);
449
		$member->setId($this->token(ManagedModel::ID_LENGTH));
450
		$member->setCircleId($circle->getSingleId());
451
		$member->setLevel(Member::LEVEL_MEMBER);
452
		$member->setStatus(Member::STATUS_MEMBER);
453
		$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...
454
455
		return $member;
456
	}
457
458
459
	/**
460
	 * @param string $groupId
461
	 * @param string $userId
462
	 *
463
	 * @return Member
464
	 * @throws FederatedEventException
465
	 * @throws FederatedItemException
466
	 * @throws FederatedUserException
467
	 * @throws FederatedUserNotFoundException
468
	 * @throws GroupNotFoundException
469
	 * @throws InitiatorNotConfirmedException
470
	 * @throws InvalidIdException
471
	 * @throws OwnerNotFoundException
472
	 * @throws RemoteInstanceException
473
	 * @throws RemoteNotFoundException
474
	 * @throws RemoteResourceNotFoundException
475
	 * @throws RequestBuilderException
476
	 * @throws SingleCircleNotFoundException
477
	 * @throws UnknownRemoteException
478
	 */
479
	public function groupMemberAdded(string $groupId, string $userId): void {
480
		$circle = $this->federatedUserService->getGroupCircle($groupId);
481
		$member = $this->generateGroupMember($circle, $userId);
482
483
		$event = new FederatedEvent(SingleMemberAdd::class);
484
		$event->setCircle($circle);
485
		$event->setMember($member);
486
		$this->federatedEventService->newEvent($event);
487
488
//		$this->memberRequest->insertOrUpdate($member);
489
490
//		$this->membershipService->onUpdate($member->getSingleId());
491
	}
492
493
494
	/**
495
	 * @param string $groupId
496
	 * @param string $userId
497
	 *
498
	 * @throws FederatedEventException
499
	 * @throws FederatedItemException
500
	 * @throws FederatedUserException
501
	 * @throws FederatedUserNotFoundException
502
	 * @throws GroupNotFoundException
503
	 * @throws InitiatorNotConfirmedException
504
	 * @throws InvalidIdException
505
	 * @throws OwnerNotFoundException
506
	 * @throws RemoteInstanceException
507
	 * @throws RemoteNotFoundException
508
	 * @throws RemoteResourceNotFoundException
509
	 * @throws RequestBuilderException
510
	 * @throws SingleCircleNotFoundException
511
	 * @throws UnknownRemoteException
512
	 */
513
	public function groupMemberRemoved(string $groupId, string $userId): void {
514
		$circle = $this->federatedUserService->getGroupCircle($groupId);
515
		$federatedUser = $this->federatedUserService->getLocalFederatedUser($userId);
516
517
		$this->memberRequest->deleteFederatedUserFromCircle($federatedUser, $circle);
518
		$this->membershipService->onUpdate($federatedUser->getSingleId());
519
	}
520
521
522
	/**
523
	 * @return void
524
	 */
525
	public function syncContacts(): void {
526
		$this->output('Syncing Contacts', true);
527
	}
528
529
530
	/**
531
	 * @return void
532
	 */
533
	public function syncGlobalScale(): void {
534
		$this->output('Syncing GlobalScale', true);
535
	}
536
537
538
	/**
539
	 * @return void
540
	 */
541
	public function syncRemote(): void {
542
		$this->output('Syncing Remote Instance', true);
543
	}
544
545
546
	/**
547
	 * @param string $circleId
548
	 *
549
	 * @return void
550
	 */
551
	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...
552
	}
553
554
555
	/**
556
	 * @param bool $force
557
	 *
558
	 * @throws MigrationException
559
	 * @throws RequestBuilderException
560
	 */
561
	public function migration(bool $force = false): void {
562
		if ($this->configService->getAppValueBool(ConfigService::MIGRATION_RUN)) {
563
			throw new MigrationException('A migration process is already running');
564
		}
565
		$this->configService->setAppValue(ConfigService::MIGRATION_RUN, '1');
566
567
		if ($force) {
568
			$this->configService->setAppValue(ConfigService::MIGRATION_22, '0');
569
//			$this->configService->setAppValue(ConfigService::MIGRATION_23, '0');
570
		}
571
572
		$this->migrationTo22();
573
		//	$this->migrationTo23();
574
575
		$this->configService->setAppValue(ConfigService::MIGRATION_RUN, '0');
576
	}
577
578
	/**
579
	 * @return void
580
	 * @throws RequestBuilderException
581
	 */
582
	private function migrationTo22(): void {
583
		if ($this->configService->getAppValueBool(ConfigService::MIGRATION_22)) {
584
			return;
585
		}
586
587
		$this->output('Migrating to 22');
588
589
		if (!is_null($this->migrationOutput)) {
590
			$this->migrationOutput->startProgress(2);
591
		}
592
593
		$this->migrationTo22_Circles();
594
		$this->migrationTo22_Members();
595
596
		if (!is_null($this->migrationOutput)) {
597
			$this->migrationOutput->finishProgress();
598
		}
599
600
		$this->configService->setAppValue(ConfigService::MIGRATION_22, '1');
601
	}
602
603
604
	/**
605
	 *
606
	 * @throws RequestBuilderException
607
	 */
608
	private function migrationTo22_Circles(): void {
609
		$this->output('Migrating Circles', true);
610
611
		$circles = [];
612
613
		$qb = $this->dbConnection->getQueryBuilder();
614
		$qb->select('*')->from('circle_circles');
615
616
		try {
617
			$cursor = $qb->executeQuery();
618
			while ($row = $cursor->fetch()) {
619
				$data = new SimpleDataStore($row);
620
621
				$circle = new Circle();
622
				$circle->setSingleId($data->g('unique_id'))
623
					   ->setName($data->g('name'))
624
					   ->setDisplayName($data->g('display_name'))
625
					   ->setSettings($data->gArray('settings'))
626
					   ->setDescription($data->g('description'))
627
					   ->setContactAddressBook($data->gInt('contact_addressbook'))
628
					   ->setContactGroupName($data->g('contact_groupname'))
629
					   ->setSource(Member::TYPE_CIRCLE);
630
631
				$dTime = $this->timezoneService->getDateTime($data->g('creation'));
632
				$circle->setCreation($dTime->getTimestamp());
633
634
				if ($circle->getDisplayName() === '') {
635
					$circle->setDisplayName($circle->getName());
636
				}
637
638
				$this->circleService->generateSanitizedName($circle);
639
				switch ($data->gInt('type')) {
640
					case 1: // personal
641
						$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...
642
						break;
643
644
					case 2: // secret
645
						$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...
646
						break;
647
648
					case 4: // closed
649
						$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...
650
						break;
651
652
					case 8: // public
653
						$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...
654
						break;
655
				}
656
657
				$circles[] = $circle;
658
			}
659
660
			$cursor->closeCursor();
661
		} 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...
662
		}
663
664
		foreach ($circles as $circle) {
665
			/** @var Circle $circle */
666
			try {
667
				try {
668
					$this->circleRequest->getCircle($circle->getSingleId());
669
				} catch (CircleNotFoundException $e) {
670
					$this->circleRequest->save($circle);
671
					usleep(50);
672
				}
673
			} catch (InvalidIdException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
674
			}
675
		}
676
	}
677
678
679
	/**
680
	 * @throws ContactAddressBookNotFoundException
681
	 * @throws ContactFormatException
682
	 * @throws ContactNotFoundException
683
	 * @throws FederatedUserException
684
	 * @throws InvalidIdException
685
	 * @throws RequestBuilderException
686
	 * @throws SingleCircleNotFoundException
687
	 */
688
	private function migrationTo22_Members(): void {
689
		$this->output('Migrating Members', true);
690
691
		$members = [];
692
693
		$qb = $this->dbConnection->getQueryBuilder();
694
		$qb->select('*')->from('circle_members');
695
696
		$appCircle = $this->federatedUserService->getAppInitiator(
697
			Application::APP_ID,
698
			Member::APP_CIRCLES
699
		);
700
701
		try {
702
			$cursor = $qb->executeQuery();
703
			while ($row = $cursor->fetch()) {
704
				$data = new SimpleDataStore($row);
705
706
				echo json_encode($data) . "\n";
707
				$member = new Member();
708
709
				$member->setCircleId($data->g('circle_id'))
710
					   ->setId($data->g('member_id'))
711
					   ->setUserId($data->g('user_id'))
712
					   ->setInstance($data->g('instance'))
713
					   ->setDisplayName($data->g('cached_name'))
714
					   ->setLevel($data->gInt('level'))
715
					   ->setStatus($data->g('status'))
716
					   ->setContactMeta($data->g('contact_meta'))
717
					   ->setContactId($data->g('contact_id'))
718
					   ->setInvitedBy($appCircle);
719
720
				switch ($data->gInt('user_type')) {
721
					case 1:
722
						$member->setUserType(1);
723
						break;
724
					case 2:
725
						$member->setUserType(2);
726
						break;
727
					case 3:
728
						$member->setUserType(4);
729
						break;
730
					case 4:
731
						$member->setUserType(8);
732
						break;
733
				}
734
735
				try {
736
					$singleMember = $this->federatedUserService->getFederatedUser(
737
						$member->getUserId(),
738
						$member->getUserType()
739
					);
740
				} catch (ContactFormatException $e) {
741
					continue;
742
				}
743
				$member->setSingleId($singleMember->getSingleId());
744
745
//					"cached_update":"2021-05-02 12:13:22",
746
//					"joined":"2021-05-02 12:13:22",
747
//					"contact_checked":null,"
748
//					single_id":"wt6WQYYCry3EOud",
749
//					"circle_source":null}
750
751
				$members[] = $member;
752
			}
753
			$cursor->closeCursor();
754
		} 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...
755
		}
756
757
		foreach ($members as $member) {
758
			$this->memberRequest->save($member);
759
		}
760
	}
761
762
	/**
763
	 * @param string $message
764
	 * @param bool $advance
765
	 */
766
	private function output(string $message, bool $advance = false): void {
767
		if (!is_null($this->occOutput)) {
768
			$this->occOutput->writeln((($advance) ? '+' : '-') . ' ' . $message);
769
		}
770
771
		if (!is_null($this->migrationOutput)) {
772
			if ($advance) {
773
				$this->migrationOutput->advance(1, '(Circles) ' . $message);
774
			} else {
775
				$this->migrationOutput->info('(Circles) ' . $message);
776
			}
777
		}
778
	}
779
780
}
781
782