Passed
Push — master ( 9b3625...a92321 )
by Blizzz
16:05 queued 11s
created

ValidatePhoneNumber::shouldRun()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 2
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2020 Joas Schilling <[email protected]>
7
 *
8
 * @author Joas Schilling <[email protected]>
9
 *
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
namespace OC\Repair\NC21;
28
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 IAccountManager */
43
	private $accountManager;
44
45
	public function __construct(IUserManager $userManager,
46
								IAccountManager $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
	public function run(IOutput $output): void {
58
		if ($this->config->getSystemValueString('default_phone_region', '') === '') {
59
			throw new \Exception('Can not validate phone numbers without `default_phone_region` being set in the config file');
60
		}
61
62
		$numUpdated = 0;
63
		$numRemoved = 0;
64
65
		$this->userManager->callForSeenUsers(function (IUser $user) use (&$numUpdated, &$numRemoved) {
66
			$account = $this->accountManager->getAccount($user);
67
			$property = $account->getProperty(IAccountManager::PROPERTY_PHONE);
68
69
			if ($property->getValue() !== '') {
70
				$this->accountManager->updateAccount($account);
71
				$updatedAccount = $this->accountManager->getAccount($user);
72
				$updatedProperty = $updatedAccount->getProperty(IAccountManager::PROPERTY_PHONE);
73
74
				if ($property->getValue() !== $updatedProperty->getValue()) {
75
					if ($updatedProperty->getValue() === '') {
76
						$numRemoved++;
77
					} else {
78
						$numUpdated++;
79
					}
80
				}
81
			}
82
		});
83
84
		if ($numRemoved > 0 || $numUpdated > 0) {
85
			$output->info('Updated ' . $numUpdated . ' entries and cleaned ' . $numRemoved . ' invalid phone numbers');
86
		}
87
	}
88
}
89