Completed
Pull Request — master (#106)
by Maxence
02:33
created

MiscService::staticGetDisplayName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 9
nc 3
nop 2
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
	 * return Display Name if user exists and display name exists.
61
	 * returns Exception if user does not exist.
62
	 *
63
	 * However, with noException set to true, will return userId even if user does not exist
64
	 *
65
	 * @param $userId
66
	 * @param bool $noException
67
	 *
68
	 * @return string
69
	 * @throws NoUserException
70
	 */
71
	public static function staticGetDisplayName($userId, $noException = false) {
72
		$user = \OC::$server->getUserManager()
73
							->get($userId);
74
		if ($user === null) {
75
			if ($noException) {
76
				return $userId;
77
			} else {
78
				throw new NoUserException();
79
			}
80
		}
81
82
		return $user->getDisplayName();
83
	}
84
85
86
	/**
87
	 * return Display Name if user exists and display name exists.
88
	 * returns Exception if user does not exist.
89
	 *
90
	 * However, with noException set to true, will return userId even if user does not exist
91
	 *
92
	 * @param $userId
93
	 * @param bool $noException
94
	 *
95
	 * @return string
96
	 * @throws NoUserException
97
	 */
98
	public function getDisplayName($userId, $noException = false) {
99
		$user = $this->userManager->get($userId);
100
		if ($user === null) {
101
			if ($noException) {
102
				return $userId;
103
			} else {
104
				throw new NoUserException();
105
			}
106
		}
107
108
		return $user->getDisplayName();
109
	}
110
}
111
112