Passed
Push — master ( fda6ff...86a3b7 )
by Joas
16:22 queued 12s
created

ValidatePhoneNumber::run()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 15
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 26
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2020 Joas Schilling <[email protected]>
6
 *
7
 * @author Joas Schilling <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
namespace OC\Repair\NC21;
27
28
use OC\Accounts\AccountManager;
29
use OCP\Accounts\IAccountManager;
30
use OCP\IConfig;
31
use OCP\IUser;
32
use OCP\IUserManager;
33
use OCP\Migration\IOutput;
34
use OCP\Migration\IRepairStep;
35
36
class ValidatePhoneNumber implements IRepairStep {
37
38
	/** @var IConfig */
39
	protected $config;
40
	/** @var IUserManager */
41
	protected $userManager;
42
	/** @var AccountManager */
43
	private $accountManager;
44
45
	public function __construct(IUserManager $userManager,
46
								AccountManager $accountManager,
47
								IConfig $config) {
48
		$this->config = $config;
49
		$this->userManager = $userManager;
50
		$this->accountManager = $accountManager;
51
	}
52
53
	public function getName(): string {
54
		return 'Validate the phone number and store it in a known format for search';
55
	}
56
57
	private function shouldRun(): bool {
0 ignored issues
show
Unused Code introduced by
The method shouldRun() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
58
		return true;
59
	}
60
61
	public function run(IOutput $output): void {
62
		if ($this->config->getSystemValueString('default_phone_region', '') === '') {
63
			throw new \Exception('Can not validate phone numbers without `default_phone_region` being set in the config file');
64
		}
65
66
		$numUpdated = 0;
67
		$numRemoved = 0;
68
69
		$this->userManager->callForSeenUsers(function (IUser $user) use (&$numUpdated, &$numRemoved) {
70
			$account = $this->accountManager->getUser($user);
71
72
			if ($account[IAccountManager::PROPERTY_PHONE]['value'] !== '') {
73
				$updated = $this->accountManager->updateUser($user, $account);
74
75
				if ($account[IAccountManager::PROPERTY_PHONE]['value'] !== $updated[IAccountManager::PROPERTY_PHONE]['value']) {
76
					if ($updated[IAccountManager::PROPERTY_PHONE]['value'] === '') {
77
						$numRemoved++;
78
					} else {
79
						$numUpdated++;
80
					}
81
				}
82
			}
83
		});
84
85
		if ($numRemoved > 0 || $numUpdated > 0) {
86
			$output->info('Updated ' . $numUpdated . ' entries and cleaned ' . $numRemoved . ' invalid phone numbers');
87
		}
88
	}
89
}
90