Completed
Pull Request — master (#2276)
by Björn
08:39
created

Hooks::changeUserHook()   C

Complexity

Conditions 11
Paths 48

Size

Total Lines 32
Code Lines 20

Duplication

Lines 12
Ratio 37.5 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 20
nc 48
nop 1
dl 12
loc 32
rs 5.2653
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Bjoern Schiessle <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
23
namespace OC\Accounts;
24
25
use OCP\ILogger;
26
use OCP\IUser;
27
28
class Hooks {
29
30
	/** @var  AccountManager */
31
	private $accountManager = null;
32
33
	/** @var ILogger */
34
	private $logger;
35
36
	/**
37
	 * Hooks constructor.
38
	 *
39
	 * @param ILogger $logger
40
	 */
41
	public function __construct(ILogger $logger) {
42
		$this->logger = $logger;
43
	}
44
45
	/**
46
	 * update accounts table if email address or display name was changed from outside
47
	 *
48
	 * @param array $params
49
	 */
50
	public function changeUserHook($params) {
51
52
		$accountManager = $this->getAccountManager();
53
54
		/** @var IUser $user */
55
		$user = isset($params['user']) ? $params['user'] : null;
56
		$feature = isset($params['feature']) ? $params['feature'] : null;
57
		$newValue = isset($params['value']) ? $params['value'] : null;
58
59
		if (is_null($user) || is_null($feature) || is_null($newValue)) {
60
			$this->logger->warning('Missing expected parameters in change user hook');
61
			return;
62
		}
63
64
		$accountData = $accountManager->getUser($user);
65
66
		switch ($feature) {
67 View Code Duplication
			case 'eMailAddress':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
68
				if ($accountData[AccountManager::PROPERTY_EMAIL]['value'] !== $newValue) {
69
					$accountData[AccountManager::PROPERTY_EMAIL]['value'] = $newValue;
70
					$accountManager->updateUser($user, $accountData);
71
				}
72
				break;
73 View Code Duplication
			case 'displayName':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
74
				if ($accountData[AccountManager::PROPERTY_DISPLAYNAME]['value'] !== $newValue) {
75
					$accountData[AccountManager::PROPERTY_DISPLAYNAME]['value'] = $newValue;
76
					$accountManager->updateUser($user, $accountData);
77
				}
78
				break;
79
		}
80
81
	}
82
83
	/**
84
	 * return instance of accountManager
85
	 *
86
	 * @return AccountManager
87
	 */
88
	protected function getAccountManager() {
89
		if (is_null($this->accountManager)) {
90
			$this->accountManager = new AccountManager(
91
				\OC::$server->getDatabaseConnection(),
92
				\OC::$server->getEventDispatcher()
93
			);
94
		}
95
		return $this->accountManager;
96
	}
97
98
}
99