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

Hooks   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 71
Duplicated Lines 16.9 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 12
loc 71
rs 10
c 1
b 0
f 0
wmc 14
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
C changeUserHook() 12 32 11
A getAccountManager() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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