Completed
Push — master ( 060fe4...9de15f )
by Maxence
02:41
created

MiscService   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 2
cbo 0
dl 0
loc 99
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A log() 0 8 1
A getRealUserId() 0 8 2
A staticGetDisplayName() 0 13 3
A getDisplayName() 0 12 3
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
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 OCA\Circles\Service;
28
29
use OC\User\NoUserException;
30
use OCP\ILogger;
31
use OCP\IUserManager;
32
33
class MiscService {
34
35
	/** @var ILogger */
36
	private $logger;
37
38
	/** @var string */
39
	private $appName;
40
41
	/** @var IUserManager */
42
	private $userManager;
43
44
	public function __construct(ILogger $logger, $appName, IUserManager $userManager) {
45
		$this->logger = $logger;
46
		$this->appName = $appName;
47
		$this->userManager = $userManager;
48
	}
49
50
	public function log($message, $level = 2) {
51
		$data = array(
52
			'app'   => $this->appName,
53
			'level' => $level
54
		);
55
56
		$this->logger->log($level, $message, $data);
57
	}
58
59
60
61
	/**
62
	 * return the real userId, with its real case
63
	 *
64
	 * @param $userId
65
	 *
66
	 * @return string
67
	 * @throws NoUserException
68
	 */
69
	public function getRealUserId($userId) {
70
		if (!$this->userManager->userExists($userId)) {
71
			throw new NoUserException();
72
		}
73
74
		return $this->userManager->get($userId)
75
								 ->getUID();
76
	}
77
78
79
80
	/**
81
	 * return Display Name if user exists and display name exists.
82
	 * returns Exception if user does not exist.
83
	 *
84
	 * However, with noException set to true, will return userId even if user does not exist
85
	 *
86
	 * @param $userId
87
	 * @param bool $noException
88
	 *
89
	 * @return string
90
	 * @throws NoUserException
91
	 */
92
	public static function staticGetDisplayName($userId, $noException = false) {
93
		$user = \OC::$server->getUserManager()
94
							->get($userId);
95
		if ($user === null) {
96
			if ($noException) {
97
				return $userId;
98
			} else {
99
				throw new NoUserException();
100
			}
101
		}
102
103
		return $user->getDisplayName();
104
	}
105
106
107
	/**
108
	 * return Display Name if user exists and display name exists.
109
	 * returns Exception if user does not exist.
110
	 *
111
	 * However, with noException set to true, will return userId even if user does not exist
112
	 *
113
	 * @param $userId
114
	 * @param bool $noException
115
	 *
116
	 * @return string
117
	 * @throws NoUserException
118
	 */
119
	public function getDisplayName($userId, $noException = false) {
120
		$user = $this->userManager->get($userId);
121
		if ($user === null) {
122
			if ($noException) {
123
				return $userId;
124
			} else {
125
				throw new NoUserException();
126
			}
127
		}
128
129
		return $user->getDisplayName();
130
	}
131
}
132
133