Passed
Push — master ( 62ff4f...9124c6 )
by John
74:39 queued 12s
created

Hooks::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Bjoern Schiessle <[email protected]>
4
 *
5
 * @author Bjoern Schiessle <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
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
namespace OC\Accounts;
26
27
use OCP\Accounts\IAccountManager;
28
use OCP\Accounts\PropertyDoesNotExistException;
29
use OCP\EventDispatcher\Event;
30
use OCP\EventDispatcher\IEventListener;
31
use OCP\IUser;
32
use OCP\User\Events\UserChangedEvent;
33
use Psr\Log\LoggerInterface;
34
35
class Hooks implements IEventListener {
36
37
	/** @var IAccountManager */
38
	private $accountManager;
39
	/** @var LoggerInterface */
40
	private $logger;
41
42
	public function __construct(LoggerInterface $logger, IAccountManager $accountManager) {
43
		$this->logger = $logger;
44
		$this->accountManager = $accountManager;
45
	}
46
47
	/**
48
	 * update accounts table if email address or display name was changed from outside
49
	 */
50
	public function changeUserHook(IUser $user, string $feature, $newValue): void {
51
		$account = $this->accountManager->getAccount($user);
52
53
		try {
54
			switch ($feature) {
55
				case 'eMailAddress':
56
					$property = $account->getProperty(IAccountManager::PROPERTY_EMAIL);
57
					break;
58
				case 'displayName':
59
					$property = $account->getProperty(IAccountManager::PROPERTY_DISPLAYNAME);
60
					break;
61
			}
62
		} catch (PropertyDoesNotExistException $e) {
63
			$this->logger->debug($e->getMessage(), ['exception' => $e]);
64
			return;
65
		}
66
67
		if (isset($property) && $property->getValue() !== (string)$newValue) {
68
			$property->setValue($newValue);
69
			$this->accountManager->updateAccount($account);
70
		}
71
	}
72
73
	public function handle(Event $event): void {
74
		if (!$event instanceof UserChangedEvent) {
75
			return;
76
		}
77
		$this->changeUserHook($event->getUser(), $event->getFeature(), $event->getValue());
78
	}
79
}
80