Completed
Pull Request — master (#120)
by Maxence
03:22
created

MiscService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 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 Exception;
30
use OC\User\NoUserException;
31
use OCA\Circles\Model\Member;
32
use OCP\ILogger;
33
use OCP\IUserManager;
34
35
class MiscService {
36
37
	/** @var ILogger */
38
	private $logger;
39
40
	/** @var string */
41
	private $appName;
42
43
	/** @var IUserManager */
44
	private $userManager;
45
46
	public function __construct(ILogger $logger, $appName, IUserManager $userManager) {
47
		$this->logger = $logger;
48
		$this->appName = $appName;
49
		$this->userManager = $userManager;
50
	}
51
52
	public function log($message, $level = 2) {
53
		$data = array(
54
			'app'   => $this->appName,
55
			'level' => $level
56
		);
57
58
		$this->logger->log($level, $message, $data);
59
	}
60
61
62
	/**
63
	 * return the real userId, with its real case
64
	 *
65
	 * @param $userId
66
	 *
67
	 * @return string
68
	 * @throws NoUserException
69
	 */
70
	public function getRealUserId($userId) {
71
		if (!$this->userManager->userExists($userId)) {
72
			throw new NoUserException();
73
		}
74
75
		return $this->userManager->get($userId)
76
								 ->getUID();
77
	}
78
79
80
	/**
81
	 * @param string $ident
82
	 * @param int $type
83
	 *
84
	 * @return string
85
	 */
86
	public static function getDisplay($ident, $type) {
87
		$display = $ident;
88
89
		self::getDisplayMember($display, $ident, $type);
90
		self::getDisplayContact($display, $ident, $type);
91
92
		return $display;
93
	}
94
95
96
	/**
97
	 * @param string $display
98
	 * @param string $ident
99
	 * @param int $type
100
	 */
101
	private static function getDisplayMember(&$display, $ident, $type) {
102
		if ($type !== Member::TYPE_USER) {
103
			return;
104
		}
105
106
		$user = \OC::$server->getUserManager()
107
							->get($ident);
108
		if ($user !== null) {
109
			$display = $user->getDisplayName();
110
		}
111
	}
112
113
114
	/**
115
	 * @param string $display
116
	 * @param string $ident
117
	 * @param int $type
118
	 */
119
	private static function getDisplayContact(&$display, $ident, $type) {
120
		if ($type !== Member::TYPE_CONTACT) {
121
			return;
122
		}
123
124
		$contact = self::getContactData($ident);
125
		self::getDisplayContactFromArray($display, $contact);
126
	}
127
128
129
	/**
130
	 * @param $ident
131
	 *
132
	 * @return mixed|string
133
	 */
134
	public static function getContactData($ident) {
135
		list($userId, $contactId) = explode(':', $ident);
136
137
		try {
138
			/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
139
			$contactApp = new \OCA\DAV\AppInfo\Application();
140
			$cm = \OC::$server->getContactsManager();
141
			$contactApp->setupContactsProvider($cm, $userId);
142
			$contact = $cm->search($contactId, ['UID']);
143
144
			return array_shift($contact);
145
		} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
146
		}
147
148
		return null;
149
	}
150
151
152
	/**
153
	 * @param string $display
154
	 * @param array $contact
155
	 */
156
	private static function getDisplayContactFromArray(&$display, $contact) {
157
		if (key_exists('FN', $contact) && $contact['FN'] !== '') {
158
			$display = $contact['FN'];
159
160
			return;
161
		}
162
163 View Code Duplication
		if (key_exists('EMAIL', $contact) && $contact['EMAIL'] !== '') {
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...
164
			$display = $contact['EMAIL'];
165
166
			return;
167
		}
168
	}
169
170
	/**
171
	 * return Display Name if user exists and display name exists.
172
	 * returns Exception if user does not exist.
173
	 *
174
	 * However, with noException set to true, will return userId even if user does not exist
175
	 *
176
	 * @param $userId
177
	 * @param bool $noException
178
	 *
179
	 * @return string
180
	 * @throws NoUserException
181
	 */
182
	public function getDisplayName($userId, $noException = false) {
183
		$user = $this->userManager->get($userId);
184
		if ($user === null) {
185
			if ($noException) {
186
				return $userId;
187
			} else {
188
				throw new NoUserException();
189
			}
190
		}
191
192
		return $user->getDisplayName();
193
	}
194
195
196
	/**
197
	 * Hacky way to async the rest of the process without keeping client on hold.
198
	 *
199
	 * @param string $result
200
	 */
201
	public function asyncAndLeaveClientOutOfThis($result = '') {
202
		if (ob_get_contents() !== false) {
203
			ob_end_clean();
204
		}
205
206
		header('Connection: close');
207
		ignore_user_abort();
208
		ob_start();
209
		echo($result);
210
		$size = ob_get_length();
211
		header('Content-Length: ' . $size);
212
		ob_end_flush();
213
		flush();
214
	}
215
216
}
217
218