Completed
Push — master ( 7146d7...8dcd63 )
by Maxence
02:55
created

MiscService::getDisplayName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
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 function getDisplayName($userId, $noException = false) {
72
		$user = $this->userManager->get($userId);
73
		if ($user === null) {
74
			if ($noException) {
75
				return $userId;
76
			} else {
77
				throw new NoUserException();
78
			}
79
		}
80
81
		return $user->getDisplayName();
82
	}
83
}